Twilio Gather params undefined

编程入门 行业动态 更新时间:2024-10-07 06:38:35

Twilio <a href=https://www.elefans.com/category/jswz/34/1686461.html style=Gather params undefined"/>

Twilio Gather params undefined

我正在本教程中收集数字输入:

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');
回答如下:

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

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

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

更多推荐

Twilio Gather params undefined

本文发布于:2024-05-07 10:28:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Gather   Twilio   undefined   params

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!