In this guide, we'll show you how to gather user input during a phone call through the phone's keypad (using DTMF tones) in your Node.js application. By applying this technique, you can create interactive voice response (IVR) 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!
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.
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.
1const express = require('express');2const VoiceResponse = require('twilio').twiml.VoiceResponse;3const urlencoded = require('body-parser').urlencoded;45const app = express();67// Parse incoming POST params with Express middleware8app.use(urlencoded({ extended: false }));910// Create a route that will handle Twilio webhook requests, sent as an11// HTTP POST to /voice in our application12app.post('/voice', (request, response) => {13// Use the Twilio Node.js SDK to build an XML response14const twiml = new VoiceResponse();1516// Use the <Gather> verb to collect user input17const gather = twiml.gather({ numDigits: 1 });18gather.say('For sales, press 1. For support, press 2.');1920// If the user doesn't enter input, loop21twiml.redirect('/voice');2223// Render the response as XML in reply to the webhook request24response.type('text/xml');25response.send(twiml.toString());26});2728// Create an HTTP server and listen for requests on port 300029console.log('Twilio Client app HTTP server running at http://127.0.0.1:3000');30app.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.
1app.post('/voice', (request, response) => {2// Use the Twilio Node.js SDK to build an XML response3const twiml = new VoiceResponse();45/** helper function to set up a <Gather> */6function gather() {7const gatherNode = twiml.gather({ numDigits: 1 });8gatherNode.say('For sales, press 1. For support, press 2.');910// If the user doesn't enter input, loop11twiml.redirect('/voice');12}1314// If the user entered digits, process their request15if (request.body.Digits) {16switch (request.body.Digits) {17case '1':18twiml.say('You selected sales. Good for you!');19break;20case '2':21twiml.say('You need support. We will help!');22break;23default:24twiml.say("Sorry, I don't understand that choice.");25twiml.pause();26gather();27break;28}29} else {30// If no input was sent, use the <Gather> verb to collect user input31gather();32}3334// Render the response as XML in reply to the webhook request35response.type('text/xml');36response.send(twiml.toString());37});
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.
1// Create a route that will handle Twilio webhook requests, sent as an2// HTTP POST to /voice in our application3app.post('/voice', (request, response) => {4// Use the Twilio Node.js SDK to build an XML response5const twiml = new VoiceResponse();67const gather = twiml.gather({8numDigits: 1,9action: '/gather',10});11gather.say('For sales, press 1. For support, press 2.');1213// If the user doesn't enter input, loop14twiml.redirect('/voice');1516// Render the response as XML in reply to the webhook request17response.type('text/xml');18response.send(twiml.toString());19});2021// Create a route that will handle <Gather> input22app.post('/gather', (request, response) => {23// Use the Twilio Node.js SDK to build an XML response24const twiml = new VoiceResponse();2526// If the user entered digits, process their request27if (request.body.Digits) {28switch (request.body.Digits) {29case '1':30twiml.say('You selected sales. Good for you!');31break;32case '2':33twiml.say('You need support. We will help!');34break;35default:36twiml.say("Sorry, I don't understand that choice.");37twiml.pause();38twiml.redirect('/voice');39break;40}41} else {42// If no input was sent, redirect to the /voice route43twiml.redirect('/voice');44}4546// Render the response as XML in reply to the webhook request47response.type('text/xml');48response.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.