This Node.js Express sample application is modeled after a typical call center experience, but with more Reese's Pieces.
Stranded aliens can call a phone number and receive instructions on how to get out of earth safely, or call their home planet directly. In this tutorial, we'll show you the key bits of code to make this work.
To initiate the phone tree, we need to configure one of our Twilio numbers to send our web application an HTTP request when we get an incoming call.
Click on one of your numbers and configure the Voice URL to point to our app. In our code the route will be /ivr/welcome.
If you don't already have a server configured to use as your webhook, ngrok is a great tool for testing webhooks locally.
With our Twilio number configured, we are prepared to respond to the Twilio request.
Respond to the Twilio request with TwiML
Our Twilio number is now configured to send HTTP requests to this route method on any incoming voice calls. Our app responds with TwiML to tell Twilio what to do in response to the message.
In this case we tell Twilio to Gather the input from the caller and we Say a welcome message.
Respond with TwiML to gather an option from the caller
'Thanks for calling the E T Phone Home Service. ' +
_112
'Please press 1 for directions. ' +
_112
'Press 2 for a list of planets to call.',
_112
{loop: 3}
_112
);
_112
_112
return voiceResponse.toString();
_112
};
_112
_112
exports.menu = function menu(digit) {
_112
const optionActions = {
_112
'1': giveExtractionPointInstructions,
_112
'2': listPlanets,
_112
};
_112
_112
return (optionActions[digit])
_112
? optionActions[digit]()
_112
: redirectWelcome();
_112
};
_112
_112
exports.planets = function planets(digit) {
_112
const optionActions = {
_112
'2': '+19295566487',
_112
'3': '+17262043675',
_112
'4': '+16513582243',
_112
};
_112
_112
if (optionActions[digit]) {
_112
const twiml = new VoiceResponse();
_112
twiml.dial(optionActions[digit]);
_112
return twiml.toString();
_112
}
_112
_112
return redirectWelcome();
_112
};
_112
_112
/**
_112
* Returns Twiml
_112
* @return {String}
_112
*/
_112
function giveExtractionPointInstructions() {
_112
const twiml = new VoiceResponse();
_112
_112
twiml.say(
_112
'To get to your extraction point, get on your bike and go down ' +
_112
'the street. Then Left down an alley. Avoid the police cars. Turn left ' +
_112
'into an unfinished housing development. Fly over the roadblock. Go ' +
_112
'passed the moon. Soon after you will see your mother ship.',
_112
{voice: 'Polly.Amy', language: 'en-GB'}
_112
);
_112
_112
twiml.say(
_112
'Thank you for calling the ET Phone Home Service - the ' +
_112
'adventurous alien\'s first choice in intergalactic travel'
_112
);
_112
_112
twiml.hangup();
_112
_112
return twiml.toString();
_112
}
_112
_112
/**
_112
* Returns a TwiML to interact with the client
_112
* @return {String}
_112
*/
_112
function listPlanets() {
_112
const twiml = new VoiceResponse();
_112
_112
const gather = twiml.gather({
_112
action: '/ivr/planets',
_112
numDigits: '1',
_112
method: 'POST',
_112
});
_112
_112
gather.say(
_112
'To call the planet Broh doe As O G, press 2. To call the planet DuhGo ' +
_112
'bah, press 3. To call an oober asteroid to your location, press 4. To ' +
_112
'go back to the main menu, press the star key ',
_112
{voice: 'Polly.Amy', language: 'en-GB', loop: 3}
_112
);
_112
_112
return twiml.toString();
_112
}
_112
_112
/**
_112
* Returns an xml with the redirect
_112
* @return {String}
_112
*/
_112
function redirectWelcome() {
_112
const twiml = new VoiceResponse();
_112
_112
twiml.say('Returning to the main menu', {
_112
voice: 'Polly.Amy',
_112
language: 'en-GB',
_112
});
_112
_112
twiml.redirect('/ivr/welcome');
_112
_112
return twiml.toString();
_112
}
After saying the text to the caller and retrieving their input, Twilio will send this input to our application.
Where to send the caller's input
The gather'saction parameter takes an absolute or relative URL as a value - in our case, the /ivr/menu route.
When the caller has finished entering digits, Twilio will make a GET or POST request to this URL including a Digits parameter with the number our caller chose.
After making this request, Twilio will continue the current call using the TwiML received in your response. Any TwiML verbs occurring after a <Gather> are unreachable, unless the caller enters no digits.
'Thanks for calling the E T Phone Home Service. ' +
_112
'Please press 1 for directions. ' +
_112
'Press 2 for a list of planets to call.',
_112
{loop: 3}
_112
);
_112
_112
return voiceResponse.toString();
_112
};
_112
_112
exports.menu = function menu(digit) {
_112
const optionActions = {
_112
'1': giveExtractionPointInstructions,
_112
'2': listPlanets,
_112
};
_112
_112
return (optionActions[digit])
_112
? optionActions[digit]()
_112
: redirectWelcome();
_112
};
_112
_112
exports.planets = function planets(digit) {
_112
const optionActions = {
_112
'2': '+19295566487',
_112
'3': '+17262043675',
_112
'4': '+16513582243',
_112
};
_112
_112
if (optionActions[digit]) {
_112
const twiml = new VoiceResponse();
_112
twiml.dial(optionActions[digit]);
_112
return twiml.toString();
_112
}
_112
_112
return redirectWelcome();
_112
};
_112
_112
/**
_112
* Returns Twiml
_112
* @return {String}
_112
*/
_112
function giveExtractionPointInstructions() {
_112
const twiml = new VoiceResponse();
_112
_112
twiml.say(
_112
'To get to your extraction point, get on your bike and go down ' +
_112
'the street. Then Left down an alley. Avoid the police cars. Turn left ' +
_112
'into an unfinished housing development. Fly over the roadblock. Go ' +
_112
'passed the moon. Soon after you will see your mother ship.',
_112
{voice: 'Polly.Amy', language: 'en-GB'}
_112
);
_112
_112
twiml.say(
_112
'Thank you for calling the ET Phone Home Service - the ' +
_112
'adventurous alien\'s first choice in intergalactic travel'
_112
);
_112
_112
twiml.hangup();
_112
_112
return twiml.toString();
_112
}
_112
_112
/**
_112
* Returns a TwiML to interact with the client
_112
* @return {String}
_112
*/
_112
function listPlanets() {
_112
const twiml = new VoiceResponse();
_112
_112
const gather = twiml.gather({
_112
action: '/ivr/planets',
_112
numDigits: '1',
_112
method: 'POST',
_112
});
_112
_112
gather.say(
_112
'To call the planet Broh doe As O G, press 2. To call the planet DuhGo ' +
_112
'bah, press 3. To call an oober asteroid to your location, press 4. To ' +
_112
'go back to the main menu, press the star key ',
_112
{voice: 'Polly.Amy', language: 'en-GB', loop: 3}
_112
);
_112
_112
return twiml.toString();
_112
}
_112
_112
/**
_112
* Returns an xml with the redirect
_112
* @return {String}
_112
*/
_112
function redirectWelcome() {
_112
const twiml = new VoiceResponse();
_112
_112
twiml.say('Returning to the main menu', {
_112
voice: 'Polly.Amy',
_112
language: 'en-GB',
_112
});
_112
_112
twiml.redirect('/ivr/welcome');
_112
_112
return twiml.toString();
_112
}
Now that we have told Twilio where to send the caller's input, we can look at how to process that input.
The Main Menu: Process the caller's selection
This route handles processing the caller's input.
If our caller chooses '1' for directions, we use the helper method giveExtractionPointInstructions to respond with TwiML that will Say directions to our caller's extraction point.
If the caller chooses '2' to call their home planet, then we need to gather more input from them. We'll cover this in the next step.
If the caller enters anything else, we respond with a TwiML Redirect to the main menu.
'Thanks for calling the E T Phone Home Service. ' +
_112
'Please press 1 for directions. ' +
_112
'Press 2 for a list of planets to call.',
_112
{loop: 3}
_112
);
_112
_112
return voiceResponse.toString();
_112
};
_112
_112
exports.menu = function menu(digit) {
_112
const optionActions = {
_112
'1': giveExtractionPointInstructions,
_112
'2': listPlanets,
_112
};
_112
_112
return (optionActions[digit])
_112
? optionActions[digit]()
_112
: redirectWelcome();
_112
};
_112
_112
exports.planets = function planets(digit) {
_112
const optionActions = {
_112
'2': '+19295566487',
_112
'3': '+17262043675',
_112
'4': '+16513582243',
_112
};
_112
_112
if (optionActions[digit]) {
_112
const twiml = new VoiceResponse();
_112
twiml.dial(optionActions[digit]);
_112
return twiml.toString();
_112
}
_112
_112
return redirectWelcome();
_112
};
_112
_112
/**
_112
* Returns Twiml
_112
* @return {String}
_112
*/
_112
function giveExtractionPointInstructions() {
_112
const twiml = new VoiceResponse();
_112
_112
twiml.say(
_112
'To get to your extraction point, get on your bike and go down ' +
_112
'the street. Then Left down an alley. Avoid the police cars. Turn left ' +
_112
'into an unfinished housing development. Fly over the roadblock. Go ' +
_112
'passed the moon. Soon after you will see your mother ship.',
_112
{voice: 'Polly.Amy', language: 'en-GB'}
_112
);
_112
_112
twiml.say(
_112
'Thank you for calling the ET Phone Home Service - the ' +
_112
'adventurous alien\'s first choice in intergalactic travel'
_112
);
_112
_112
twiml.hangup();
_112
_112
return twiml.toString();
_112
}
_112
_112
/**
_112
* Returns a TwiML to interact with the client
_112
* @return {String}
_112
*/
_112
function listPlanets() {
_112
const twiml = new VoiceResponse();
_112
_112
const gather = twiml.gather({
_112
action: '/ivr/planets',
_112
numDigits: '1',
_112
method: 'POST',
_112
});
_112
_112
gather.say(
_112
'To call the planet Broh doe As O G, press 2. To call the planet DuhGo ' +
_112
'bah, press 3. To call an oober asteroid to your location, press 4. To ' +
_112
'go back to the main menu, press the star key ',
_112
{voice: 'Polly.Amy', language: 'en-GB', loop: 3}
_112
);
_112
_112
return twiml.toString();
_112
}
_112
_112
/**
_112
* Returns an xml with the redirect
_112
* @return {String}
_112
*/
_112
function redirectWelcome() {
_112
const twiml = new VoiceResponse();
_112
_112
twiml.say('Returning to the main menu', {
_112
voice: 'Polly.Amy',
_112
language: 'en-GB',
_112
});
_112
_112
twiml.redirect('/ivr/welcome');
_112
_112
return twiml.toString();
_112
}
If the caller chooses '2', we will take them to the Planet Directory where we need to collect more input.
The Planet Directory: Connect the caller to another number
If our callers choose to call their home planet we will read them the planet directory. This is akin to a typical "company directory" feature of most IVRs.
In this route, we grab the caller's selection from the request and store it in a variable called selectedOption. We then use a Dial verb with the appropriate phone number to connect our caller to their home planet.
The current numbers are hardcoded, but they could also be read from a database or from a file.
Read planet directory and connect to another number based on caller input