Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created November 24, 2015 00:53
Show Gist options
  • Save dwelch2344/a6413ed0fd3f23f0418f to your computer and use it in GitHub Desktop.
Save dwelch2344/a6413ed0fd3f23f0418f to your computer and use it in GitHub Desktop.
var data = [
{}, // blank so we can index by 1 (matching the IDs)
{id: 1, verb: 'Say', value: 'Thanks for calling. Press 1 to leave a voicemail or 2 Customer Service'
options: {
'1': 5,
'2': 6
}
},
{id: 2, verb: 'Gather', children: [1]},
{id: 3, verb: 'Say', value: 'Please leave a voicemail'}
{id: 4, verb: 'Record'}
{id: 5, multistep: true, children: [3, 4]},
{id: 6, verb: 'Enqueue', value: 'CustomerServiceQueue'}
];
app.get('/twilio/incoming', handleIncoming);
function handleIncoming(req, res){
var prev = req.params.previousStep;
if( prev ){
var digits = req.params.DIGITS; // Whatever they pressed on our previous prompt
return handleSubsequent(prev, digits, req, res);
}else{
return handleFirstCall(req, res);
}
}
function handleFirstCall(req, res){
// send our first instruction, which is the gather
// statement with a child "Say" command
return res.send(200, toXml(data[2]));
}
function handleSubsequent(prev, digits, req, res){
var from = data[prev];
var next = from.options[digits];
return res.send(200, toXml(next));
}
function toXml(step){
var xml;
// whatever you do to generate XML. The key here is to make your gather steps point back to this url
switch(step.verb){
case 'Gather':
var url = '/twilio/incoming?previousStep=' + step.id;
xml = someGatherXmlVerb(url, step.children);
break;
// .. handle other verbs
default:
throw new Error('Unhandled verb: ', step.verb);
}
return res.send(200, xml);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment