Last active
August 29, 2015 14:18
-
-
Save jparreira/c621a615843d155c87a3 to your computer and use it in GitHub Desktop.
Realtime Code Hosting function to send SMS using Twilio when a new comment is added to a Trello card
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
function trelloComment(res,req,modules){ | |
// send a log to the remote console | |
modules.storageMule.log("trelloComment: ", JSON.stringify(req)); | |
// find out the trello action triggered | |
var trelloAction = req.body.action.type; | |
// it's a comment | |
if(trelloAction == "commentCard") { | |
// get the comment text | |
var comment = req.body.action.data.text; | |
// send sms with the comment text | |
sendSms(comment, res, modules); | |
} else { | |
// it's not a comment, just reply OK | |
res.send(200); | |
} | |
} | |
// This function will send the sms | |
// Please refer to the Twilio API docs for more details | |
function sendSms(comment, res, modules) { | |
var encodedText = encodeURIComponent(comment); | |
var sms = "From=[YOUR_TWILIO_NUMBER]&To=[DESTINATION_PHONE_NUMBER]&Body=" + encodedText; | |
var headers = { | |
'Authorization': 'Basic [YOUR_TWILIO_AUTHENTICATION]', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': sms.length | |
}; | |
var options = { | |
host: 'api.twilio.com', | |
port: 443, | |
path: '/2010-04-01/Accounts/[YOUR_TWILIO_ACCOUNT_SID]/Messages.json', | |
method: 'POST', | |
headers: headers | |
}; | |
// prepare the request | |
var request = modules.https.request(options, function(response, body) { | |
// send success log to the remote console | |
modules.storageMule.log("trelloComment: SMS SENT"); | |
// reply with ok status code to let Trello know the webhook was successfull | |
res.send(200); | |
}); | |
request.on('error', function(e) { | |
// Ooops, we have a problem. Send it to the remote console | |
modules.storageMule.log("trelloComment: ERROR SENDING SMS : ", JSON.stringify(e)); | |
res.send(400); | |
}); | |
request.write(sms); | |
// send the request to the Twilio API | |
request.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment