如何使用Twilio拨打出站电话并检测是否已接听
我正在尝试用Twilio开发一个简单的应用程序,我遇到了一些问题。我用Python编写了代码来进行调用如何使用Twilio拨打出站电话并检测是否已接听,twilio,Twilio,我正在尝试用Twilio开发一个简单的应用程序,我遇到了一些问题。我用Python编写了代码来进行调用 def makeCall(self, phoneNumber, from_num, message): call = self.client.calls.create( method='POST', machine_detection='Enable',
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
我还有一个Node.js应用程序,带有Twiml响应
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
参数,您需要确保正确使用主体解析器中间件,urlencoded
是一个函数,您需要将express app设置为使用它
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
然后你就可以从那里处理了。好的,谢谢你的回答。问题是,如果用户在手机铃声响起时拒绝通话,request.body.CallStatus将返回进行中,然后返回完成。最后它会在电话答录机上发送语音信息。。。如果用户不接电话也会发生同样的情况。听起来可能就是你要找的。看看这是否能解决你的问题,我明白。我已经把
app.post('/start',(request,response)=>{if(request.body.AnsweredBy!='machine\u start'){//和答案}
放在了else里,但是我不知道该放什么来避免这个消息。如果我不放`else``语句,所有的代码都在`if``里面,它就不工作了。是的!这就是我想要的!!非常感谢!