Reputation: 23
I'm going through this tutorial to collect digit input: https://www.twilio.com/docs/voice/tutorials/how-to-gather-user-input-via-keypad-node-js
The request.body is undefined so I can't seem to find the input. When I try request.params it is empty though in my Twilio dash board I can see the params are in the Post request from my gather action.
const fs = require('fs');
const botSpeak = require('./bot_speak/scripts.json');
const VoiceResponse = require('twilio').twiml.VoiceResponse;
const app = express();
// Returns TwiML which prompts the caller to record a message
app.post('/welcome', (request, response) => {
// Use the Twilio Node.js SDK to build an XML response
const twiml = new VoiceResponse();
//read scipts from .json
const gather = twiml.gather({
numDigits: 1,
action: '/gather'
});
gather.say(botSpeak.hello + botSpeak.continue);
//if no response
twiml.say(botSpeak.bye);
// Render the response as XML in reply to the webhook request
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/gather', (request, response) => {
// Use the Twilio Node.js SDK to build an XML response
const twiml = new VoiceResponse();
// If the user entered digits, process their request
console.log(request.body);
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();
twiml.redirect('/welcome');
break;
}
} else {
// If no input was sent, redirect to the /voice route
twiml.redirect('/welcome');
}
// Render the response as XML in reply to the webhook request
response.type('text/xml');
response.send(twiml.toString());
});
// Create an HTTP server and listen for requests on port 3000
app.listen(3000);
console.log('Server serving on port 3000');
Upvotes: 0
Views: 265
Reputation: 10781
You need body-parser middleware. Try adding this.
// Body Parser Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
https://expressjs.com/en/4x/api.html#express-json-middleware
Upvotes: 0