最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Twilio Gather params undefined

网站源码admin23浏览0评论

Twilio 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论