【问题标题】:Twilio-Client always returns connection status as open on outbound call even if the recipient has not accepted the call yet即使接收方尚未接受呼叫,Twilio-Client 也始终将连接状态返回为出站呼叫打开
【发布时间】:2020-08-05 16:47:33
【问题描述】:

我正在尝试获取通过Twilio Client(js) v 1.10.1 拨打的外拨电话的状态。但是即使呼叫处于振铃状态,.status() 方法也会将值返回为“open”。我尝试在初始化 Twilio.Device 时添加 enableRingingState: true 作为参数,并在服务器端创建 TwiMl Dial 动词时设置 answerOnBridge="true"

根据官方documentation,js sdk 应该返回这些状态“pending”、“connecting”、“ringing”、“open”、“close”。

这是显示“正在呼叫...”所必需的,直到接听电话并在接听电话后立即在浏览器中的拨号器中启动计时器(mm:ss)。

注意:也尝试过使用 Twilio 的快速入门指南代码。

/**** 伪代码如下 ***/ // 前端代码

let device = require('twilio-client').Device;
let outboundCall = '';
const twilioInit = () => {

    // axios call to get capability {token} from service

    device = device.setup(token,
                    {
                        allowIncomingWhileBusy: true,
                        enableRingingState: true
                    });

    device.ready(function () {
                    console.log("Twilio ready");                    
                    device.identity = "Ratan";  
                    console.log("Twilio Device ", device);
                });

    device.error(function (error) {
                    console.log("error in twilio ", error);
                    if (error.message == "JWT Token Expired") {
                        alert("token expired");
                    }
                    // If token has expired, dispatch an action to get new token.
                });

}

// on phone icon click i am calling the below method
const makeOutboundCall = () => {
    outboundCall = device.connect({        
        To: '+911234567890' // This is not the real phone number, I use number from list of Twilio verified numbers
      });

    outboundCall.on('ringing', function (hasEarlyMedia) {
        console.log("ringing");
      });

    // Then tracking the status of this outboundCall object in setInterval function with a interval of one second by outboundCall.status() and clearing the.. 
    // ..setInterval once the call disconnects

}

// 后端代码 - 服务器端(WEBHOOK)

@RequestMapping(value="/callCustomers", produces= "text/xml")
public String callByBrowserToMobile(@RequestParam String ApplicationSid, @RequestParam String ApiVersion, 
                                        @RequestParam String Called, @RequestParam String Caller, 
                                        @RequestParam String CallStatus, @RequestParam String To,
                                        @RequestParam String From, @RequestParam String CallSid,
                                        @RequestParam String Direction, @RequestParam String AccountSid) {

      Number number = new Number.Builder(To).build();
      Dial dial = new Dial.Builder().answerOnBridge(true).number(number).callerId(myTwilioNumber).build();
      VoiceResponse response = new VoiceResponse.Builder().dial(dial).build();
      logger.info(response.toXml());              
      return response.toXml();    
}     

呼叫已正确拨打,但问题仅在于使用 twilio js 库跟踪出站呼叫状态,以便一旦收件人接听呼叫,我就可以在 UI 中启动呼叫计时器。

【问题讨论】:

  • 如果有人能在这里帮助我,那就太好了。我期待 philnash Twilio Developer Evangelist 回答我的问题。
  • 你能分享你用来设置 Twilio 设备和监听事件变化的代码吗?
  • 我添加了伪代码。请指导。

标签: twilio


【解决方案1】:

这里是 Twilio 开发者宣传员。

编辑:找出问题所在

当使用<Dial answerOnBridge="true"> 使响铃事件可用时,<Dial> 需要是 Twilio 在启动呼叫时看到的第一个 TwiML。当您使用免费试用帐户时,Twilio 会在通话开始时播放一条消息,该消息充当看到的第一个 TwiML,因此 answerOnBridge 属性不会生效,并且您不会收到振铃事件会期待的。

要解决此特定问题,您需要升级您的帐户,以便 Twilio 在通话开始时不再读出试用帐户消息。

原答案:

我不会使用setInterval 来轮询通话​​状态。相反,我会监听连接对象上的事件。所以你的makeOutboundCall 函数应该看起来像这样:

const makeOutboundCall = () => {
    outboundCall = device.connect({        
        To: TO_NUMBER
    });

    outboundCall.on("ringing", (hasEarlyMedia) => {
        console.log("The call has started and the other phone is ringing.");
    });

    outboundCall.on("accept", (connection) => {
        console.log("The other person answered the phone!");
    });

    outboundCall.on("disconnect", () => {
      console.log("The other person hung up.");
    });
}

让我知道这是否有帮助。

【讨论】:

  • 我尝试了您建议的方法,但没有帮助。即使呼叫在接收方的电话上响铃(尚未接听),它也已打印“呼叫已开始,另一部电话正在响铃”。和“另一个人接了电话!”在控制台中。您可以尝试使用 Twilio 的快速入门指南代码(github.com/TwilioDevEd/client-quickstart-js)
  • @RatanDeep 我不确定你发生了什么。我刚刚克隆了该快速入门,按照说明设置我需要的 Twilio 函数,将它们插入我的 TwiML 应用程序,然后打电话给我自己和I got the 'ringing' event as the call started, and the 'connect' event as the call was answered。我没有改变任何东西,它按照你要求的方式工作。你发现了什么?
  • @Philansh,感谢您试用。就我而言,我没有使用 twilio 函数,而是在我的服务器上创建了 webhook 方法,并在我的 TwiML 应用程序的 Twilio Programmable Voice 中配置了相同的方法。让我尝试使用 Twilio 函数并分享结果。您认为我用于创建 TwiML 的服务器代码(发布在问题中)是好的还是需要任何修改?
  • 您能分享一下makeOutboundCall 函数的最新代码吗?你试过我分享的代码了吗?
  • 嗨@philnash,感谢您的指导,升级后的帐户运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多