twilio - 如何使用 Twilio 拨打电话并检测是否已接听
问题描述
我正在尝试使用 Twilio 开发一个简单的应用程序,但遇到了一些问题。我有用 Python 编写的代码来进行调用。
def makeCall(self, phoneNumber, from_num, message):
call = self.client.calls.create(
method='POST',
machine_detection='Enable',
to= phoneNumber,
from_= from_num,
url= self.url + urlencode({'Message' : message}),
status_callback= self.url_callback,
status_callback_method = 'POST'
)
return call.sid
而且我还有一个带有 Twiml 响应的 Node.js 应用程序。
const express = require('express');
const VoiceResponse = require('twilio').twiml.VoiceResponse;
const urlencoded = require('body-parser').urlencoded;
const app = express();
app.post('/start', (request, response) => {
const twiml = new VoiceResponse();
const gather = twiml.gather({
numDigits: 1,
action: '/confirmation'
});
gather.say({
language:'en-EN'
}, 'Hello, are you ok?' );
gather.pause({
length: 1
});
gather.say({
language:'en-EN'
}, 'If no press one, if yes press two');
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/confirmation', (request, response) => {
const twiml = new VoiceResponse();
if (request.body.Digits) {
switch (request.body.Digits) {
case '2':
twiml.say({
language:'en-EN'
}, 'I am sorry to hear that');
case '1':
twiml.say({
language:'en-EN'
}, 'Perfect!');
default:
twiml.say({
language:'en-EN'
}, 'Sorry, I don't understand you.');
twiml.pause({
length: 1
});
twiml.say({
language: 'en-EN'
}, 'Repeat please');
}
}
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/callback', (request, response) => {
console.log(request.body.CallStatus);
console.log('--------------------');
console.log(request.body.AnsweredBy);
console.log('--------------------');
response.type('text/xml')
response.send(request.AnsweredBy)
});
app.listen(3000);
问题是当我执行python函数时。如果用户拒绝来电或不接听,它会向答录机发送语音消息,我想避免它。我还想在 python 代码中检测呼叫是否被拒绝或未应答。
提前致谢
解决方案
Twilio 开发人员布道者在这里。
您无法检测到在创建呼叫的 python 代码中是否应答了呼叫。这将使 Twilio 调度的调用排队,因此所有进一步的事件都将与该 API 调用异步发生。
对于接收 webhook 的 Node.js 应用程序,您可以通过检查作为请求正文的一部分发送的CallStatus
参数来检查调用的当前状态。CallStatus
可以是以下之一:“queued”、“ringing”、“in-progress”、“completed”、“busy”、“failed”或“no-answer”,您可以在文档中查看有关参数的CallStatus
更多信息。
要读取CallStatus
参数,您需要确保正确使用 body-parser 中间件,urlencoded
它是一个函数,您需要设置 express 应用程序以使用它。
const urlencoded = require('body-parser').urlencoded;
const app = express();
app.use(urlencoded({ extended: false });
然后,您可以在响应函数中获取呼叫状态。
app.post('/start', (request, response) => {
console.log(request.body.CallStatus);
// and so on
然后你可以从那里处理它。
推荐阅读
- javascript - 如何设置 vue 和 core ui?
- java - JavaFX ListChangeListener 删除触发添加更改
- javascript - 使用 JavaScript 在 URL 中添加 GET 参数
- c++ - 将对象插入 std::map 时构造函数中的变量地址
- realm - Realm db 的撤消管理
- .net - 如何为 ARM 平台构建 Tizen C# .NET Wearable TPK 并发布到三星卖家门户?
- javascript - 在 dataLoader amchart v3 中循环
- java - 有没有办法根据给定的字符串设置实例的成员变量?
- firebase - Flutter Cloud Firestore 仅在调试时获取最新数据
- c - 如何订购相互依赖的功能?