Skip to contentSkip to navigationSkip to topbar
On this page

IVR: Phone Tree with Node.js and Express


ET Phone Home: IVR Node and Express Example.

This Node.js Express(link takes you to an external page) sample application is modeled after a typical call center experience, but with more Reese's Pieces(link takes you to an external page).

Stranded aliens can call a phone number and receive instructions on how to get out of earth safely, or call their home planet(link takes you to an external page) directly. In this tutorial, we'll show you the key bits of code to make this work.

To run this sample app yourself, download the code and follow the instructions on GitHub(link takes you to an external page).

Read how Livestream(link takes you to an external page) and other companies(link takes you to an external page) built phone trees on IVR with Twilio. Find guides for many web languages on our IVR example page.


Responding to a Phone Call

responding-to-a-phone-call page anchor

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(link takes you to an external page) and configure the Voice URL to point to our app. In our code the route will be /ivr/welcome.

IVR Webhook Configuration.

If you don't already have a server configured to use as your webhook, ngrok(link takes you to an external page) 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

respond-to-the-twilio-request-with-twiml page anchor

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

respond-with-twiml-to-gather-an-option-from-the-caller page anchor
1
const VoiceResponse = require('twilio').twiml.VoiceResponse;
2
3
exports.welcome = function welcome() {
4
const voiceResponse = new VoiceResponse();
5
6
const gather = voiceResponse.gather({
7
action: '/ivr/menu',
8
numDigits: '1',
9
method: 'POST',
10
});
11
12
gather.say(
13
'Thanks for calling the E T Phone Home Service. ' +
14
'Please press 1 for directions. ' +
15
'Press 2 for a list of planets to call.',
16
{loop: 3}
17
);
18
19
return voiceResponse.toString();
20
};
21
22
exports.menu = function menu(digit) {
23
const optionActions = {
24
'1': giveExtractionPointInstructions,
25
'2': listPlanets,
26
};
27
28
return (optionActions[digit])
29
? optionActions[digit]()
30
: redirectWelcome();
31
};
32
33
exports.planets = function planets(digit) {
34
const optionActions = {
35
'2': '+19295566487',
36
'3': '+17262043675',
37
'4': '+16513582243',
38
};
39
40
if (optionActions[digit]) {
41
const twiml = new VoiceResponse();
42
twiml.dial(optionActions[digit]);
43
return twiml.toString();
44
}
45
46
return redirectWelcome();
47
};
48
49
/**
50
* Returns Twiml
51
* @return {String}
52
*/
53
function giveExtractionPointInstructions() {
54
const twiml = new VoiceResponse();
55
56
twiml.say(
57
'To get to your extraction point, get on your bike and go down ' +
58
'the street. Then Left down an alley. Avoid the police cars. Turn left ' +
59
'into an unfinished housing development. Fly over the roadblock. Go ' +
60
'passed the moon. Soon after you will see your mother ship.',
61
{voice: 'Polly.Amy', language: 'en-GB'}
62
);
63
64
twiml.say(
65
'Thank you for calling the ET Phone Home Service - the ' +
66
'adventurous alien\'s first choice in intergalactic travel'
67
);
68
69
twiml.hangup();
70
71
return twiml.toString();
72
}
73
74
/**
75
* Returns a TwiML to interact with the client
76
* @return {String}
77
*/
78
function listPlanets() {
79
const twiml = new VoiceResponse();
80
81
const gather = twiml.gather({
82
action: '/ivr/planets',
83
numDigits: '1',
84
method: 'POST',
85
});
86
87
gather.say(
88
'To call the planet Broh doe As O G, press 2. To call the planet DuhGo ' +
89
'bah, press 3. To call an oober asteroid to your location, press 4. To ' +
90
'go back to the main menu, press the star key ',
91
{voice: 'Polly.Amy', language: 'en-GB', loop: 3}
92
);
93
94
return twiml.toString();
95
}
96
97
/**
98
* Returns an xml with the redirect
99
* @return {String}
100
*/
101
function redirectWelcome() {
102
const twiml = new VoiceResponse();
103
104
twiml.say('Returning to the main menu', {
105
voice: 'Polly.Amy',
106
language: 'en-GB',
107
});
108
109
twiml.redirect('/ivr/welcome');
110
111
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

where-to-send-the-callers-input page anchor

The gather's action 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.

Send caller input to the intended route

send-caller-input-to-the-intended-route page anchor
1
const VoiceResponse = require('twilio').twiml.VoiceResponse;
2
3
exports.welcome = function welcome() {
4
const voiceResponse = new VoiceResponse();
5
6
const gather = voiceResponse.gather({
7
action: '/ivr/menu',
8
numDigits: '1',
9
method: 'POST',
10
});
11
12
gather.say(
13
'Thanks for calling the E T Phone Home Service. ' +
14
'Please press 1 for directions. ' +
15
'Press 2 for a list of planets to call.',
16
{loop: 3}
17
);
18
19
return voiceResponse.toString();
20
};
21
22
exports.menu = function menu(digit) {
23
const optionActions = {
24
'1': giveExtractionPointInstructions,
25
'2': listPlanets,
26
};
27
28
return (optionActions[digit])
29
? optionActions[digit]()
30
: redirectWelcome();
31
};
32
33
exports.planets = function planets(digit) {
34
const optionActions = {
35
'2': '+19295566487',
36
'3': '+17262043675',
37
'4': '+16513582243',
38
};
39
40
if (optionActions[digit]) {
41
const twiml = new VoiceResponse();
42
twiml.dial(optionActions[digit]);
43
return twiml.toString();
44
}
45
46
return redirectWelcome();
47
};
48
49
/**
50
* Returns Twiml
51
* @return {String}
52
*/
53
function giveExtractionPointInstructions() {
54
const twiml = new VoiceResponse();
55
56
twiml.say(
57
'To get to your extraction point, get on your bike and go down ' +
58
'the street. Then Left down an alley. Avoid the police cars. Turn left ' +
59
'into an unfinished housing development. Fly over the roadblock. Go ' +
60
'passed the moon. Soon after you will see your mother ship.',
61
{voice: 'Polly.Amy', language: 'en-GB'}
62
);
63
64
twiml.say(
65
'Thank you for calling the ET Phone Home Service - the ' +
66
'adventurous alien\'s first choice in intergalactic travel'
67
);
68
69
twiml.hangup();
70
71
return twiml.toString();
72
}
73
74
/**
75
* Returns a TwiML to interact with the client
76
* @return {String}
77
*/
78
function listPlanets() {
79
const twiml = new VoiceResponse();
80
81
const gather = twiml.gather({
82
action: '/ivr/planets',
83
numDigits: '1',
84
method: 'POST',
85
});
86
87
gather.say(
88
'To call the planet Broh doe As O G, press 2. To call the planet DuhGo ' +
89
'bah, press 3. To call an oober asteroid to your location, press 4. To ' +
90
'go back to the main menu, press the star key ',
91
{voice: 'Polly.Amy', language: 'en-GB', loop: 3}
92
);
93
94
return twiml.toString();
95
}
96
97
/**
98
* Returns an xml with the redirect
99
* @return {String}
100
*/
101
function redirectWelcome() {
102
const twiml = new VoiceResponse();
103
104
twiml.say('Returning to the main menu', {
105
voice: 'Polly.Amy',
106
language: 'en-GB',
107
});
108
109
twiml.redirect('/ivr/welcome');
110
111
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

the-main-menu-process-the-callers-selection page anchor

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.


The Planet Directory: Connect the caller to another number

the-planet-directory-connect-the-caller-to-another-number page anchor

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

read-planet-directory-and-connect-to-another-number-based-on-caller-input page anchor
1
const VoiceResponse = require('twilio').twiml.VoiceResponse;
2
3
exports.welcome = function welcome() {
4
const voiceResponse = new VoiceResponse();
5
6
const gather = voiceResponse.gather({
7
action: '/ivr/menu',
8
numDigits: '1',
9
method: 'POST',
10
});
11
12
gather.say(
13
'Thanks for calling the E T Phone Home Service. ' +
14
'Please press 1 for directions. ' +
15
'Press 2 for a list of planets to call.',
16
{loop: 3}
17
);
18
19
return voiceResponse.toString();
20
};
21
22
exports.menu = function menu(digit) {
23
const optionActions = {
24
'1': giveExtractionPointInstructions,
25
'2': listPlanets,
26
};
27
28
return (optionActions[digit])
29
? optionActions[digit]()
30
: redirectWelcome();
31
};
32
33
exports.planets = function planets(digit) {
34
const optionActions = {
35
'2': '+19295566487',
36
'3': '+17262043675',
37
'4': '+16513582243',
38
};
39
40
if (optionActions[digit]) {
41
const twiml = new VoiceResponse();
42
twiml.dial(optionActions[digit]);
43
return twiml.toString();
44
}
45
46
return redirectWelcome();
47
};
48
49
/**
50
* Returns Twiml
51
* @return {String}
52
*/
53
function giveExtractionPointInstructions() {
54
const twiml = new VoiceResponse();
55
56
twiml.say(
57
'To get to your extraction point, get on your bike and go down ' +
58
'the street. Then Left down an alley. Avoid the police cars. Turn left ' +
59
'into an unfinished housing development. Fly over the roadblock. Go ' +
60
'passed the moon. Soon after you will see your mother ship.',
61
{voice: 'Polly.Amy', language: 'en-GB'}
62
);
63
64
twiml.say(
65
'Thank you for calling the ET Phone Home Service - the ' +
66
'adventurous alien\'s first choice in intergalactic travel'
67
);
68
69
twiml.hangup();
70
71
return twiml.toString();
72
}
73
74
/**
75
* Returns a TwiML to interact with the client
76
* @return {String}
77
*/
78
function listPlanets() {
79
const twiml = new VoiceResponse();
80
81
const gather = twiml.gather({
82
action: '/ivr/planets',
83
numDigits: '1',
84
method: 'POST',
85
});
86
87
gather.say(
88
'To call the planet Broh doe As O G, press 2. To call the planet DuhGo ' +
89
'bah, press 3. To call an oober asteroid to your location, press 4. To ' +
90
'go back to the main menu, press the star key ',
91
{voice: 'Polly.Amy', language: 'en-GB', loop: 3}
92
);
93
94
return twiml.toString();
95
}
96
97
/**
98
* Returns an xml with the redirect
99
* @return {String}
100
*/
101
function redirectWelcome() {
102
const twiml = new VoiceResponse();
103
104
twiml.say('Returning to the main menu', {
105
voice: 'Polly.Amy',
106
language: 'en-GB',
107
});
108
109
twiml.redirect('/ivr/welcome');
110
111
return twiml.toString();
112
}

That's it! We've just implemented an IVR phone tree that will delight and serve your customers.


If you're a Node.js developer working with Twilio, you might want to check out these other tutorials:

Account Verification

Use Twilio and Twilio-powered Authy to implement account verification at the point of registration.

Two-Factor Authentication with Authy

Use Twilio and Twilio-powered Authy OneTouch to implement two-factor authentication (2FA) in your web app.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.