Skip to contentSkip to navigationSkip to topbar
On this page

Gather User Input via Keypad (DTMF Tones) in Node.js


In this guide, we'll show you how to gather user input during a phone call through the phone's keypad (using DTMF(link takes you to an external page) tones) in your Node.js application. By applying this technique, you can create interactive voice response (IVR(link takes you to an external page)) systems and other phone based interfaces for your users. The code snippets in this guide are written using modern JavaScript language features in Node.js version 6 or higher, and make use of the following node modules:

Let's get started!


Set up your Node.js Express application to receive incoming phone calls

set-up-your-nodejs-express-application-to-receive-incoming-phone-calls page anchor

This guide assumes you have already set up your web application to receive incoming phone calls. If you still need to complete this step, check out this guide. It should walk you through the process of buying a Twilio number and configuring your app to receive incoming calls.


Collect user input with the <Gather> TwiML verb

collect-user-input-with-the-gather-twiml-verb page anchor

The <Gather> TwiML verb allows us to collect input from the user during a phone call. Gathering user input through the keypad is a core mechanism of Interactive Voice Response (IVR) systems where users can press "1" to connect to one menu of options and press "2" to reach another. These prompts can be accompanied by voice prompts to the caller, using the TwiML <Say> and <Play> verbs. In this example, we will prompt the user to enter a number to connect to a certain department within our little IVR system.

Use <Gather> to collect user input via the keypad (DTMF tones)Link to code sample: Use <Gather> to collect user input via the keypad (DTMF tones)
1
const express = require('express');
2
const VoiceResponse = require('twilio').twiml.VoiceResponse;
3
const urlencoded = require('body-parser').urlencoded;
4
5
const app = express();
6
7
// Parse incoming POST params with Express middleware
8
app.use(urlencoded({ extended: false }));
9
10
// Create a route that will handle Twilio webhook requests, sent as an
11
// HTTP POST to /voice in our application
12
app.post('/voice', (request, response) => {
13
// Use the Twilio Node.js SDK to build an XML response
14
const twiml = new VoiceResponse();
15
16
// Use the <Gather> verb to collect user input
17
const gather = twiml.gather({ numDigits: 1 });
18
gather.say('For sales, press 1. For support, press 2.');
19
20
// If the user doesn't enter input, loop
21
twiml.redirect('/voice');
22
23
// Render the response as XML in reply to the webhook request
24
response.type('text/xml');
25
response.send(twiml.toString());
26
});
27
28
// Create an HTTP server and listen for requests on port 3000
29
console.log('Twilio Client app HTTP server running at http://127.0.0.1:3000');
30
app.listen(3000);

If the user doesn't enter any input after a configurable timeout, Twilio will continue processing the TwiML in the document to determine what should happen next in the call. When the end of the document is reached, Twilio will hang up the call. In the above example, we use the <Redirect> verb to have Twilio request the same URL again, repeating the prompt for the user

If a user were to enter input with the example above, the user would hear the same prompt over and over again regardless of what button you pressed. By default, if the user does enter input in the <Gather>, Twilio will send another HTTP request to the current webhook URL with a POST parameter containing the Digits entered by the user. In the sample above, we weren't handling this input at all. Let's update that logic to also process user input if it is present.

Branch your call logic based on the digits sent by the userLink to code sample: Branch your call logic based on the digits sent by the user
1
app.post('/voice', (request, response) => {
2
// Use the Twilio Node.js SDK to build an XML response
3
const twiml = new VoiceResponse();
4
5
/** helper function to set up a <Gather> */
6
function gather() {
7
const gatherNode = twiml.gather({ numDigits: 1 });
8
gatherNode.say('For sales, press 1. For support, press 2.');
9
10
// If the user doesn't enter input, loop
11
twiml.redirect('/voice');
12
}
13
14
// If the user entered digits, process their request
15
if (request.body.Digits) {
16
switch (request.body.Digits) {
17
case '1':
18
twiml.say('You selected sales. Good for you!');
19
break;
20
case '2':
21
twiml.say('You need support. We will help!');
22
break;
23
default:
24
twiml.say("Sorry, I don't understand that choice.");
25
twiml.pause();
26
gather();
27
break;
28
}
29
} else {
30
// If no input was sent, use the <Gather> verb to collect user input
31
gather();
32
}
33
34
// Render the response as XML in reply to the webhook request
35
response.type('text/xml');
36
response.send(twiml.toString());
37
});

Specify an action to take after user input is collected

specify-an-action-to-take-after-user-input-is-collected page anchor

You may want to have an entirely different endpoint in your application handle the processing of user input. This is possible using the "action" attribute of the <Gather> verb. Let's update our example to add a second endpoint that will be responsible for handling user input.

Add another route to handle the input from the userLink to code sample: Add another route to handle the input from the user
1
// Create a route that will handle Twilio webhook requests, sent as an
2
// HTTP POST to /voice in our application
3
app.post('/voice', (request, response) => {
4
// Use the Twilio Node.js SDK to build an XML response
5
const twiml = new VoiceResponse();
6
7
const gather = twiml.gather({
8
numDigits: 1,
9
action: '/gather',
10
});
11
gather.say('For sales, press 1. For support, press 2.');
12
13
// If the user doesn't enter input, loop
14
twiml.redirect('/voice');
15
16
// Render the response as XML in reply to the webhook request
17
response.type('text/xml');
18
response.send(twiml.toString());
19
});
20
21
// Create a route that will handle <Gather> input
22
app.post('/gather', (request, response) => {
23
// Use the Twilio Node.js SDK to build an XML response
24
const twiml = new VoiceResponse();
25
26
// If the user entered digits, process their request
27
if (request.body.Digits) {
28
switch (request.body.Digits) {
29
case '1':
30
twiml.say('You selected sales. Good for you!');
31
break;
32
case '2':
33
twiml.say('You need support. We will help!');
34
break;
35
default:
36
twiml.say("Sorry, I don't understand that choice.");
37
twiml.pause();
38
twiml.redirect('/voice');
39
break;
40
}
41
} else {
42
// If no input was sent, redirect to the /voice route
43
twiml.redirect('/voice');
44
}
45
46
// Render the response as XML in reply to the webhook request
47
response.type('text/xml');
48
response.send(twiml.toString());
49
});

The action attribute takes a relative URL which would point to another route your server is capable of handling. Now, instead of conditional logic in a single route, we use actions and redirects to handle our call logic with separate code paths.


If you're building call center type applications in Node.js, you might enjoy stepping through full sample applications written in Node.js.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.