Twilio, the cloud communications platform, empowers developers to build rich and interactive voice and messaging applications. In this blog, we'll explore the Twilio Gather and Say verbs, two essential tools for creating dynamic phone systems. We'll walk through a step-by-step demo that showcases how to gather user input and respond with custom voice messages using Node.js.
Prerequisites
Before we dive into the demo, ensure that you have the following prerequisites in place:
Twilio Account: Sign up for a Twilio account if you don't have one. You'll need your Account SID and Auth Token, which can be found on the Twilio Dashboard.
Phone Number: A Twilio phone number with voice capabilities. You can purchase one in your Twilio account.
Node.js: Make sure you have Node.js installed on your system. You can download it from the official website.
NPM: The Node Package Manager (NPM) comes bundled with Node.js. It's required for installing the Twilio Node.js package.
Setting up the Project
Let's create a new Node.js project and install the Twilio package. Open your terminal and run the following commands:
# Create a new directory for your project
mkdir twilio-gather-say-demo
cd twilio-gather-say-demo
# Initialize a Node.js project
npm init -y
# Install the Twilio Node.js library
npm install twilio
Writing the Code
Now, let's write the code to set up a simple phone system that gathers user input and responds with customized messages. Create a file named app.js
and open it in your code editor.
const accountSid = 'YOUR_TWILIO_ACCOUNT_SID';
const authToken = 'YOUR_TWILIO_AUTH_TOKEN';
const client = require('twilio')(accountSid, authToken);
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/voice', (req, res) => {
const twiml = new Twilio.twiml.VoiceResponse();
twiml.gather({
input: 'dtmf',
numDigits: 1,
action: '/gather',
}, gatherNode => {
gatherNode.say('Press 1 for sales. Press 2 for support.');
});
res.type('text/xml');
res.send(twiml.toString());
});
app.post('/gather', (req, res) => {
const twiml = new Twilio.twiml.VoiceResponse();
const choice = req.body.Digits;
if (choice === '1') {
twiml.say('Connecting to the sales department.');
} else if (choice === '2') {
twiml.say('Connecting to the support department.');
} else {
twiml.say('Sorry, I donβt understand that choice.');
twiml.redirect('/voice');
}
res.type('text/xml');
res.send(twiml.toString());
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Make sure to replace 'YOUR_TWILIO_ACCOUNT_SID'
and 'YOUR_TWILIO_AUTH_TOKEN'
with your actual Twilio Account SID and Auth Token.
Understanding the Code
Here's an overview of the code:
We import the Twilio package and initialize it with our Account SID and Auth Token.
We create an Express.js web server to handle incoming POST requests from Twilio.
The
/voice
endpoint initiates a phone call and uses the Gather verb to collect user input.Inside the Gather verb, we specify:
input: 'dtmf'
to gather DTMF (Dual-Tone Multi-Frequency) input (i.e., numeric keypad input).numDigits: 1
to collect a single digit.action: '/gather'
to redirect the user to the/gather
endpoint after input.
In the
/gather
endpoint, we check the user's input (1 for sales, 2 for support) and respond with custom messages.
Running the Application
To run the application, execute the following command in your project directory:
node app.js
The code will start an Express server on the specified port (default is 3000). You can configure your Twilio phone number to point to the /voice
endpoint, and it will initiate the Gather and Say demo when you call the number.
Call logs
To check the call logs , go to monitor section and then click on logs and click on the call sid
Conclusion
Twilio's Gather and Say verbs empower you to create interactive phone systems that can gather user input and respond with personalized messages. This opens up possibilities for creating IVR systems, phone menu options, and much more.
With the example in this blog, you've taken your first steps in building dynamic phone systems with Twilio and Node.js. Explore Twilio's extensive documentation and features to create even more interactive and engaging voice applications.
Happy coding and creating! ππ