Created
June 8, 2013 00:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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