Hello,
I am new to WixCode and i have an issue.
I am trying to use twilio api to send an sms after a form is submitted. I followed Twilio documentation about the process and used a simple code to send sms after a button is clicked. The problem is, the documentation says that i need to use the ‘Require()’ function, and, after some searching i learn that function does not exist, and i couldn’t find anything to substitude without giving me an error. How can i change this to work my needs?
I followed the documentation here: Programmable SMS Quickstarts | Twilio
Never write your API Key or any sensitive information on the page code.
You should create a .jsw file in your backend which will contain this
//twilioSms.jsw
import Twillo from 'twilio';
const accountSid = 'ACa10sddw0ef20ecf46ced49674'; //your accountSid
const authToken = 'your_auth_token'; // your authToken
const client = require('twilio')(accountSid, authToken); //ignore the error
export function sendSms(msg, number) {
client.messages
.create({from: '+15017122661', body: msg, to: number})
.then(message => console.log(message.sid))
.done();
}
Then call the function from your Page code like this
import {sendSms} from 'backend/twilioSms';
export function button1_click(event) {
const msg = 'This is a message'; //replace it with the input or text field key
const number = '+0000000000' //this is the number you want to send it to
sendSms(msg, number);
}
Hey,
First of all, the api key was my mistake and i removed right away, thank you for warning.
Second, the code works great, i assumed that i had to put everything in one place. It was my bad.
Hi,
I followed the script. It worked great if I hard coded the outbound phone number like
const number = ‘+16472092190’ : Example text number
However, if I tried to get the phone number from the input text,
var number = $w(‘#input1’).value; or
var number = ‘+1’ + $w(‘#input1’).value;
It didn’t work. Can you pls provide me some idea how to fix it?? https://zippyitsolutions.wixsite.com/mvp1
Hello,
I followed the same code and tried to send a SMS to my mobile via my Wix website but nothing is happening. I am not sure what I might be doing wrong here. It will be great if someone can help me out with this.
Here is my code:
// Filename: backend/twiliob.jsw import twilio from ‘twilio’ ; const accountSid = ‘abc’ ; //your accountSid const authToken = ‘xyz’ ; // your authToken const client = require (‘twilio’)(accountSid, authToken);
export function sendSms(msg, number) {
client.messages
.create({ from : ‘+10000000000’ , body: msg, to: number})
.then(message => console.log(message.sid))
.done() ;
}
//TODO: write your page related code here…import com.twilio.Twilio; import {sendSms} from ‘backend/twiliob’ ;
export function button10_onClick(event) { const msg = ‘This is a message’ ; const number = ‘+13333333333’
sendSms(msg, number); }
If you are sending the message to an Indian number then you might have issues due to local regulations related to delivery of text messages to numbers who have not consented. Check with Twilio’s help center for more info.