Skip to content

Instantly share code, notes, and snippets.

@ismaelc
Created June 8, 2013 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismaelc/5733329 to your computer and use it in GitHub Desktop.
Save ismaelc/5733329 to your computer and use it in GitHub Desktop.
node.js function snippet showing a REST API call to Twilio to call a phone. Full source code is at https://github.com/ismaelc/twilio-translator
// Builds the call request so Twilio can call the user's phone with the local voice of the translation, using a text to speech API
exports.makeCall = function (req, res) {
// TODO: Check if function is called from same app
var translatedText = req.query.t;
var to = req.query.to;
var lang = req.query.lang;
// The parameters needed by Twilio to make the call. The Url parameter returns a Twiml that dictates what happens when
// the call is picked up.
var postdata = qs.stringify({
'From': config.twilio.number,
'To': to,
'Url': config.app.url + 'getTwimlToCall?t=' + encodeURIComponent(translatedText) + '&lang=' + lang
});
// Setting up the Twilio API endpoint for making calls
var options = {
host: 'api.twilio.com',
path: '/2010-04-01/Accounts/' + config.twilio.sid + '/Calls.xml',
port: 443,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postdata.length
},
auth: config.twilio.sid + ':' + config.twilio.token
};
var request = https.request(options, function (res2) {
res2.setEncoding('utf8');
res2.on('data', function (chunk) {
console.log('Response: ' + chunk);
res.send(chunk, {
'Content-Type': 'text/xml'
}, 200);
})
})
// Make the call
request.write(postdata);
request.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment