Created
September 29, 2015 01:52
-
-
Save jinman/2fc74aa15ac2cedb2dd3 to your computer and use it in GitHub Desktop.
LambdaToTwilio
This file contains hidden or 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
console.log('Loading event'); | |
// Twilio Credentials | |
var accountSid = '<your twilio Sid>'; | |
var authToken = '<your twilio authtoken>'; | |
var fromNumber = '<your from number>'; | |
var toNumber = '<your to number>' | |
var https = require('https'); | |
var queryString = require('querystring'); | |
// Lambda function: | |
exports.handler = function (event, context) { | |
console.log('Running event'); | |
// Send an SMS message to the number provided in the event data. | |
// End the lambda function when the send function completes. | |
//SendSMS(toNumber, 'Hello from Lambda Functions!', | |
// function (status) { context.done(null, status); }); | |
MakeCall(toNumber, | |
function (status) { context.done(null, status); }); | |
}; | |
// Sends an SMS message using the Twilio API | |
// to: Phone number to send to | |
// body: Message body | |
// completedCallback(status) : Callback with status message when the function completes. | |
function MakeCall(to, completedCallback) { | |
var message = { | |
To: to, | |
From: fromNumber, | |
Url: '<your twimlet.com url>' //Your twimlet that calls the number | |
//Body: body | |
}; | |
var messageString = queryString.stringify(message); | |
// Options and headers for the HTTP request | |
var options = { | |
host: 'api.twilio.com', | |
port: 443, | |
path: '/2010-04-01/Accounts/' + accountSid + '/Calls', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(messageString), | |
'Authorization': 'Basic ' + new Buffer(accountSid + ':' + authToken).toString('base64') | |
} | |
}; | |
// Setup the HTTP request | |
var req = https.request(options, function (res) { | |
res.setEncoding('utf-8'); | |
// Collect response data as it comes back. | |
var responseString = ''; | |
res.on('data', function (data) { | |
responseString += data; | |
}); | |
// Log the responce received from Twilio. | |
// Or could use JSON.parse(responseString) here to get at individual properties. | |
res.on('end', function () { | |
console.log('Twilio Response: ' + responseString); | |
completedCallback('API request sent successfully.'); | |
}); | |
}); | |
// Handler for HTTP request errors. | |
req.on('error', function (e) { | |
console.error('HTTP error: ' + e.message); | |
completedCallback('API request completed with error(s).'); | |
}); | |
// Send the HTTP request to the Twilio API. | |
// Log the message we are sending to Twilio. | |
console.log('Twilio API call: ' + messageString); | |
req.write(messageString); | |
req.end(); | |
} | |
// Sends an SMS message using the Twilio API | |
// to: Phone number to send to | |
// body: Message body | |
// completedCallback(status) : Callback with status message when the function completes. | |
function SendSMS(to, body, completedCallback) { | |
// The SMS message to send | |
var message = { | |
To: to, | |
From: fromNumber, | |
Body: body | |
}; | |
var messageString = queryString.stringify(message); | |
// Options and headers for the HTTP request | |
var options = { | |
host: 'api.twilio.com', | |
port: 443, | |
path: '/2010-04-01/Accounts/' + accountSid + '/Messages.json', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(messageString), | |
'Authorization': 'Basic ' + new Buffer(accountSid + ':' + authToken).toString('base64') | |
} | |
}; | |
// Setup the HTTP request | |
var req = https.request(options, function (res) { | |
res.setEncoding('utf-8'); | |
// Collect response data as it comes back. | |
var responseString = ''; | |
res.on('data', function (data) { | |
responseString += data; | |
}); | |
// Log the responce received from Twilio. | |
// Or could use JSON.parse(responseString) here to get at individual properties. | |
res.on('end', function () { | |
console.log('Twilio Response: ' + responseString); | |
completedCallback('API request sent successfully.'); | |
}); | |
}); | |
// Handler for HTTP request errors. | |
req.on('error', function (e) { | |
console.error('HTTP error: ' + e.message); | |
completedCallback('API request completed with error(s).'); | |
}); | |
// Send the HTTP request to the Twilio API. | |
// Log the message we are sending to Twilio. | |
console.log('Twilio API call: ' + messageString); | |
req.write(messageString); | |
req.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment