Skip to content

Instantly share code, notes, and snippets.

@zackproser
Created July 21, 2015 05:41
Show Gist options
  • Save zackproser/7892892751a572af338a to your computer and use it in GitHub Desktop.
Save zackproser/7892892751a572af338a to your computer and use it in GitHub Desktop.
An example Twilio-based call menu that accepts and processes user input via touchtone.
/**
* Handle user inputs during the CatFacts Call Center Menu
*
* @param {Request} req Express Request
* @param {Response} res Express Response
*
* @return {[type]} Response containing valid Twiml for Twilio to parse
*/
app.post('/catfacts-call-menu', function(req, res){
//Get the number the user pressed from the Twilio request object
var pressed = req.body.Digits;
var calling_user = req.body.From;
//Set up headers
res.writeHead(200, { 'Content-Type': 'text/xml' });
//Handle whichever number the user pressed
switch(pressed){
case '1':
//User requested a CatFact - pull a random one out of catfacts.json
var fact = require('./data/catfacts.json').random();
//Send a random CatFact to the caller
sendResponseSMS(calling_user, fact);
//Create a twiml response to build up
var twiml = new twilio.TwimlResponse();
twiml.say('One brand spanking new Cat Fact coming right up. We\'re working hard to deliver your fact. Thanks for using CatFacts and please call again!', {
voice: 'man',
language: 'en'
})
//Play a sound that Express is serving as a static file
.play(config.server_root + '/sounds/angryMeow.wav');
//Send the response back for Twilio to parse on the fly - and play for the caller
res.end(twiml.toString());
break;
case '2':
//User wants to know why they were subscribed to CatFacts - Why, because they love cats, of course!
var twiml = new twilio.TwimlResponse();
twiml.say('Please wait one moment while I pull up your account', {
voice: 'man',
language: 'en'
})
.play(config.server_root + '/sounds/longMeow.wav')
.say('Thanks for your patience. You were subscribed to CatFacts because you love fun facts about cats. As a thank you for calling in today, we will increase the frequency of your catfacts account at no extra charge', {
voice: 'man',
language: 'en'
})
.play(config.server_root + '/sounds/angryMeow.wav')
.say('Have a furry and fantastic day', {
voice: 'man',
language: 'en'
});
//Send account explanation response back to Twilio to parse on the fly - and play for the caller
res.end(twiml.toString());
break;
case '3':
//User wants to unsubscribe - but we don't like quitters
var twiml = new twilio.TwimlResponse();
twiml.say('We understand you would like to cancel your CatFacts account. Unfortunately, we are currently experiencing technical difficulties and cannot process your request at this time. To apologize for the inconvenience, we have upgraded you to a Super CatFacts Account for no extra charge', {
voice: 'man',
language: 'en'
})
.play(config.server_root + '/sounds/angryMeow.wav');
res.end(twiml.toString());
break;
default:
var twiml = new twilio.TwimlResponse();
twiml.say('Sorry, we were unable to process your request at this time. Don\'t worry, we will send you complimentary CatFacts as an apology for the inconvenience.', {
voice: 'man',
language: 'en'
})
.play(config.server_root + '/sounds/angryMeow.wav');
res.end(twiml.toString());
break;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment