Skip to content

Instantly share code, notes, and snippets.

@zackproser
Created July 21, 2015 05:16
Show Gist options
  • Save zackproser/5b736d71557a2df86d61 to your computer and use it in GitHub Desktop.
Save zackproser/5b736d71557a2df86d61 to your computer and use it in GitHub Desktop.
Demonstrates how to handle an incoming sms POSTed by Twilio.
/**
* Handle incoming sms commands
*
* Verifies that the requestor is authorized - then determines the request type (start / stop attacking)
*
* Finally starts or stops an attack as appropriate
*
* @param {Request} Twilio POST request - generated when a user sends an sms to the associated Twilio number
* @param {Response} Express response
*
*/
app.post('/incoming-sms', function(req, res){
//Get the requestor's phone number from the Twilio POST object
var requestor = req.body.From;
//If target is currently under attack and is not an admin - and they text this number - give them a particular text response
if (isTargetBeingAttacked(requestor) && !isAuthorizedUser(requestor)){
sendResponseSMS(requestor, 'Command not recognized. We will upgrade your CatFacts account to send you facts more frequently. Thanks for choosing CatFacts!');
} else if (!isAuthorizedUser(requestor)){
//Do nothing and do not respond if requestor is unauthorized
return;
} else {
//Get body content of sms sent by requestor
var payload = req.body.Body;
//Check if this is a stop attack request - returns target number if it is
var check = isStopRequest(payload);
if(check){
//isStopRequest returns the target phone number for valid stop requests
var target = check;
//Stop the attack on the supplied number
handleAdminStopRequest(requestor, target);
} else {
//Start an attack on the supplied number
handleAdminAttackRequest(requestor, payload);
}
//Give Twilio a successful response for logging purposes
res.status(200).send('Finished processing POST request to /incoming-sms');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment