Twilio Gather params undefined

问题描述 投票:0回答:1

我正在本教程中收集数字输入:https://www.twilio.com/docs/voice/tutorials/how-to-gather-user-input-via-keypad-node-js

request.body未定义,所以我似乎找不到输入。当我尝试request.params时,它是空的,尽管在我的Twilio仪表板中,我可以从我的collect操作中看到这些参数在Post请求中。

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');
node.js post twilio
1个回答
0
投票

您需要身体解析器中间件。尝试添加此。

// Body Parser Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

https://expressjs.com/en/4x/api.html#express-json-middleware

© www.soinside.com 2019 - 2024. All rights reserved.