Reputation: 33
I have managed to work Twilio’s API on the client side, sending out a SMS but can’t seem to bring it all together. I want to create a simple HTML form has some text prompting the user to enter a phone number, have a text field to accept the numeric string and a button to post the string as an input variable, aka, the number to be texted. Not sure where to put my javascript function (send-sms.js) below for the button click. Couldnt find anything on the web, any ideas or examples?
/* send-sms.js */
// Twilio Credentials
var accountSid = 'ACd8a0efd010e09e09ce506fe1d4612e11';
var authToken = 'AUTH_TOKEN';
//require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken);
client.messages.create({
to: "+1646456789",
from: "+19171234567",
body: "Hellow from Twilio?",
}, function(err, message) {
console.log(message.sid);
});
Upvotes: 3
Views: 14008
Reputation: 651
You need a server side script (Like PHP or Node.js) in order to connect to the Twilio API Currently your script won't allow it due to Cross Origin restrictions and it is also not advised as well since this exposes your account credentials.
Best thing to do is to create a server side script that connects to Twilio and add this to your form submit.
Upvotes: 0
Reputation: 73029
Twilio developer evangelist here.
As Scott said, you have to be careful with your account credentials and if you try to use the Twilio Node.js API in the front end you will lose you account SID and auth token and a malicious user will run your account out of money.
So, you should create a server from which to send your messages. As guavacat pointed out, there are several tutorials available on sending SMS messages with Twilio. There are also some more basic guides on sending SMS with Twilio.
I recommend reading up on those which will take you through setting up a server and sending a message from it.
Upvotes: 2
Reputation: 867
Why not just create a function, and then call it? This sounds like what you are wanting, anyway. I could be wrong. However, you want to be VERY careful using Twilio API's client-side. This opens up a large security risk. Someone can easily get the account credentials to send tons of text messages with your money.
function sendTextMessage() {
var phoneNumber = $("#phoneNumberField).val();
var accountSid = 'ACOUNTSID';
var authToken = 'AUTHTOKEN';
var client = require('twilio')(accountSid, authToken);
client.messages.create({
to: phoneNumber,
from: "+19171234567",
body: "Hellow from Twilio?",
}, function(err, message) {
console.log(message.sid);
});
}
Then have a form...
<input type="text" id="phoneNumberField" />
<button type="button" onclick="sendTextMessage()">Send Text Message</button>
The better alternative is to run it server-side, and send it via an html form to your server-side application via POST/GET and HTTPS/HTTP. Client-side scripting creates a great interface, and quick. But, you can also submit forms using jQuery for the same experience, or use AngularJS, for example with a backend API to do your data manipulation and logic.
Others have mentioned it, but creating a Node.js server and running it there would be preferred over running it on the client's browser.
Upvotes: 3