Twilio TWIML nodejs 收集示例代码控制流程

Twilio TWIML nodejs gather sample code control flow

我正在检查 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')。我不确定该代码如何正常工作?

来自 Twilio 的瑞奇。

此代码不会创建无限循环,但出于与阻塞 vs non-blocking 代码不同的原因。您控制 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>