How to make Twilio stop dialing numbers when hangup() is fired?

旧街凉风 提交于 2019-12-11 02:19:21

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/27800173/how-to-make-twilio-stop-dialing-numbers-when-hangup-is-fired

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!