Reputation: 27
I have used the code from Gather User Input via Keypad (DTMF Tones) in Node.js twilio documentation for getting user input from the call.
app.post('/voice', (request, response) => {
const twiml = new VoiceResponse();
function gather() {
const gatherNode = twiml.gather({ numDigits: 1 });
gatherNode.say('For sales, press 1. For support, press 2.');
twiml.redirect('/voice');
}
if (request.body.Digits) {
switch (request.body.Digits) {
case '1':
twiml.say('You selected sales. Good for you!');
break;
case '2':
twiml.say('You need support. We will help!');
break;
default:
twiml.say("Sorry, I don't understand that choice.").pause();
gather();
break;
}
} else {
gather();
}
response.type('text/xml');
response.send(twiml.toString());
});
When i call to my twilio number i'm getting error like "TypeError: Cannot read property 'Digits' of undefined" at the if statement.I want to get what number user enters during the call.Thanks in advance!!
Upvotes: 2
Views: 1230
Reputation: 1
I was just having the same issue. Adding these two lines fixed it for me:
const urlencoded = require('body-parser').urlencoded;
app.use(urlencoded({ extended: false }));
They were in the Twilio documentation as well. I'm a bit of a newbie at Node, so I'm not sure why the code breaks without them. According to the comment Twilio provided,
// Parse incoming POST params with Express middleware
Upvotes: 0
Reputation: 148
Log the value of request and inspect those properties,
Comment out everything below: if(request.body.Digits)
And then run this in its place:
console.log(request)
Because the error is telling you that body isn't defined in that object. Otherwise if it is there the incapsulate the switch statement in this
if(request.body)
{
//Switch statement
}
Upvotes: 0
Reputation: 954
It seems like your initial request
have no body
propery.
Try added a check to your if
statement:
if (request.body && request.body.Digits) {
// switch / case
} else {
gather();
}
Upvotes: 1