如何在触发 hangup() 时让 Twilio 停止拨号? [英] How to make Twilio stop dialing numbers when hangup() is fired?

查看:21
本文介绍了如何在触发 hangup() 时让 Twilio 停止拨号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 twilio-ruby gem 开发 Twilio 应用程序,但我被困在一个部分:

为呼叫构建 TwiML 响应,我拨打了一些号码,以便他们也可以接听电话:

def handle_gather......response = Twilio::TwiML::Response.new do |r|r.拨号做|d|numbers.each do |number|d.号码号码结尾d.客户端user_id结尾r.Say 'A ligação falhou ou o attendente desligou.Obrigado.', :voice =>爱丽丝",语言:pt-BR"结尾render_twiml 响应结尾

我可以使用 Twilio.Device.disconnectAll() 终止连接,但应用程序不断拨打 numbers 变量中的号码.我怎样才能停止拨打他们的电话?

添加更多详细信息:

基本上,我正在关注这两个示例:https://www.twilio.com/docs/quickstart/ruby/client/呼出电话https://www.twilio.com/blog/2014/02/twilio-on-rails-integrating-twilio-with-your-rails-4-app.html

我有一个有 2 个动作的控制器:voicehandle_gather.在 handle_gather 操作中,我得到呼叫者按下的选项(使用 params['Digits'])并拨打一些号码,如上所示.接下来,我设置了一个客户端,以便我也可以在应用程序中接听电话.

在应用程序中,用户可以点击接听"或挂断".当用户单击挂断"时,我会在处理程序中获取此事件并调用 Twilio.Device.disconnectAll() 以结束连接.但是,应用程序仍然拨打我上面提到的那些号码,这​​是我想要改变的行为.

编辑 2:尝试将 CallSid 发送到服务器并在那里取消呼叫. Coffescript:

$('.hangup').click ->Twilio.Device.disconnectAll()铃声.暂停()# 发送 call_sid 到服务器并取消呼叫$.ajax 'twilio/cancel_call/' + conn.parameters.CallSid + '.json',类型:'获取'数据类型:'json'错误: (jqXHR, textStatus, errorThrown) ->console.log "#{textStatus} - #{errorThrown}"成功:(结果,textStatus,jqXHR)->console.log 结果

操作:

@client = Twilio::REST::Client.new current_user.sub_account_sid, current_user.sub_account_auth_token@call = @client.account.calls.get(params[:call_sid])@call.update(:status => "取消")response_to do |格式|format.json { 渲染 json: @call.status.to_json }结尾

作为响应,我被取消"了,但应用程序一直在拨号.

解决方案

当您通过 动词同时拨打多个号码/客户时,所有号码都会响铃,直到有人接听为止.

在您的情况下,您正在挂断客户端,这意味着其他号码将继续响铃,直到有人接听为止.

如果您想通过客户端的挂断按钮结束对所有号码的呼叫,有几个选项.首先,您的挂断按钮可以在未接电话的情况下接听电话然后挂断.不过这有点hacky.

可能更可取的是从 参数中获取 callSid 传递给客户端并将其发送到您的服务器,服务器将依次通过 REST api 和 从那里完成呼叫.

希望这会有所帮助!

我的错误是,您实际上需要完成"通话,而不是取消通话.来自 API 文档:

<块引用>

请注意,从 Twilio 的角度来看,当前在动词内振铃的任何呼叫都在进行中,因此您必须使用Status=completed"来取消它.

I've been developing a Twilio application using the twilio-ruby gem, but I'm stuck in one part:

Building the TwiML response for a call, I dial some numbers so they can answer the call as well:

def handle_gather

  ...

  ...

  response = Twilio::TwiML::Response.new do |r|
    r.Dial do |d|
      numbers.each do |number|
        d.Number number 
      end
      d.Client user_id
    end
    r.Say 'A ligação falhou ou o atendente desligou. Obrigado.', :voice => 'alice', language: "pt-BR"
  end

  render_twiml response
end

I can terminate the connection using Twilio.Device.disconnectAll(), but the application keeps dialing the numbers in numbers variable. How can I stop dialing them ?

Edit: Adding more details:

Basically, I'm following this 2 examples: https://www.twilio.com/docs/quickstart/ruby/client/outgoing-calls https://www.twilio.com/blog/2014/02/twilio-on-rails-integrating-twilio-with-your-rails-4-app.html

I have a controller with 2 actions: voice and handle_gather. At the handle_gather action, I get the option pressed by the caller (with params['Digits']) and dial some numbers, as shown above. Next, I set up a client so I can answer the call in the application as well.

In the application, the user can click on "answer" or "hangup". When the user click on "hangup", I get this event in a handler and call Twilio.Device.disconnectAll() to end the connection. However, the application still dialing those numbers I've mentioned above, which is the behaviour I want to change.

Edit 2: Trying to send CallSid to server and cancel the call there. Coffescript:

$('.hangup').click ->
  Twilio.Device.disconnectAll()
  ringtone.pause()

  # Send call_sid to server and cancel the call
  $.ajax 'twilio/cancel_call/' + conn.parameters.CallSid + '.json',
    type: 'GET'
    dataType: 'json'
    error: (jqXHR, textStatus, errorThrown) ->
      console.log "#{textStatus} - #{errorThrown}"
    success: (result, textStatus, jqXHR) ->
      console.log result

Action:

@client = Twilio::REST::Client.new current_user.sub_account_sid, current_user.sub_account_auth_token

@call = @client.account.calls.get(params[:call_sid])
@call.update(:status => "canceled")    

respond_to do |format|
  format.json { render json: @call.status.to_json }
end

I got "canceled" as a response from the action but the application keeps dialing.

解决方案

When you dial many numbers/clients simultaneously through the <Dial> verb all numbers will ring until one is answered.

In your case, you are hanging up in the client, which will mean that the other numbers will continue to ring until one is answered.

If you want to end the call to all the numbers from the hangup button in the client there are a couple of options. Firstly, your hangup button could, in the case of an unanswered call, answer the call and then hang up. That is a bit hacky though.

Probably more preferable is to get the callSid from the parameters passed to the client and send that to your server, which would in turn look up the call via the REST api and complete the call from there.

Hope this helps!

Edit:

My mistake here, you actually need to "complete" the call, not cancel it. From the API docs:

Note that any call which is currently ringing within a verb is in-progress from the point of view of Twilio, and thus you must use 'Status=completed' to cancel it.

这篇关于如何在触发 hangup() 时让 Twilio 停止拨号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆