首页 > 解决方案 > 如何使用 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

解决方案


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

然后你可以从那里处理它。


推荐阅读