【问题标题】:Twilio TWIML nodejs gather sample code control flowTwilio TWIML nodejs 收集示例代码控制流
【发布时间】:2018-04-04 17:44:56
【问题描述】:

我正在检查 twilio 文档上的 this 示例(v2.x 但 v3.x 也类似,我的问题不会改变)。

// This example uses JavaScript language features present in Node.js 6+

'use strict';
const express = require('express');
const twilio = require('twilio');
const urlencoded = require('body-parser').urlencoded;

let app = express();

// Parse incoming POST params with Express middleware
app.use(urlencoded({ extended: false }));

// Create a route that will handle Twilio webhook requests, sent as an 
// HTTP POST to /voice in our application
app.post('/voice', (request, response) => {
  // Use the Twilio Node.js SDK to build an XML response
  let twiml = new twilio.TwimlResponse();

  // Use the <Gather> verb to collect user input 
  twiml.gather({ numDigits: 1 }, (gatherNode) => {
    gatherNode.say('For sales, press 1. For support, press 2.');
  });

  // If the user doesn't enter input, loop
  twiml.redirect('/voice');

  // 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);

那么下面是阻塞的片段吗?

twiml.gather({ numDigits: 1 }, (gatherNode) => { gatherNode.say('For sales, press 1. For support, press 2.'); }); 如果是,那么假设用户输入了一些东西,然后我们移动到 twiml.redirect('/voice');

其他语句依次执行。

但是如果它是非阻塞的,那么/voice 端点会立即被调用,并且这会在一个无限循环中继续。

我想知道流程是如何工作的。

编辑:

混乱似乎是由这条评论引起的 // If the user doesn't enter input, loop

如果用户输入了一些东西,那么twiml.redirect('/voice') 也会被调用。我不确定该代码如何正常工作?

【问题讨论】:

    标签: node.js twilio twilio-twiml


    【解决方案1】:

    来自 Twilio 的 Ricky 在这里。

    此代码不会创建无限循环,但与阻塞代码与非阻塞代码的原因有点不同。控制 Twilio 呼叫流的方式是通过 TwiML,它是一种 XML,其中包含一组关于如何处理传入呼叫的​​指令。 /voice 路由中的节点代码本身并不处理控制流,而是生成如下所示的 XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
      <Gather numDigits="1">
        <Say>For sales, press 1. For support, press 2.</Say>
      </Gather>
      <Redirect>/voice</Redirect>
    </Response>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-19
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多