Last active
February 3, 2017 22:56
-
-
Save timsayshey/c76bba0381fb75d632c9b4cd3a83bd9e to your computer and use it in GitHub Desktop.
Slack slash command to Lambda Twilio SMS Message
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
// Credit: Charlie & Tim | |
// Go to AWS API Gateway | |
// Add Resource, then add a GET method with a node js lambda function (paste the code from below in) | |
// Add an integration template to the API Gateway resource to allow the 'text' url param from Slack | |
var https = require( "https" ); | |
var querystring = require( "querystring" ); | |
exports.handler = function( event, context, callback ) { | |
var data = event.text.split(" "); | |
var toPhone = data[ 0 ]; | |
data.shift(); | |
var twilioPayload = querystring.stringify({ | |
"To": toPhone, | |
"From": "+1YOURTWILIONUMBER", | |
"Body": data.join(" ") | |
}); | |
var uname = "xxxxxxx"; // twilio account id | |
var pwd = "xxxxxxxx"; // twilio token | |
var options = { | |
hostname: "api.twilio.com", | |
path: "/2010-04-01/Accounts/"+uname+"/Messages.json", | |
method: "POST", | |
port: 443, | |
headers: { | |
"Authorization": "Basic " + new Buffer( uname + ":" + pwd ).toString( "base64" ), | |
"Content-Type": "application/x-www-form-urlencoded", | |
"Content-Length": Buffer.byteLength( twilioPayload ) | |
} | |
}; | |
var req = https.request( options, function( res ) { | |
res.setEncoding( "utf8" ); | |
res.on( "data", function( body ) { | |
console.log( "body: " + body ); | |
}); | |
res.on( "end", function() { | |
callback( null, "Message sent to " + toPhone ); | |
}) | |
}); | |
req.on( "error", function( e ) { | |
console.log( "Aw snap. This happened: " + e.message ); | |
}); | |
req.write( twilioPayload ); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment