我在twilio文档上检查了此样本(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')
。我不确定该代码如何正常工作?
ricky来自此处的Twilio。
此代码不会创建无限循环,而是出于与阻止非阻滞代码的原因有所不同。您控制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>