2
votes

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
Hey, I'm a developer evangelist for Twilio. Could you add a little more detail please, I can see you're using Twilio Client as well, but I'm not sure how it's fitting together right now.philnash
Thanks for your response, @philnash. I've added some more details. Please let me know if I wasn't clear enough.Gustavo Costa
Thanks, sorry I have more questions though. How are you initiating the call, from a phone or from a client. And then which client are you sending that disconnect action from?philnash
The call is being initiated from my home phone. I'm not sure I've understood the "client" concept. In the application, my user bought a number from Twilio (I've developed this part already) and, from my home phone, I'm calling that number. So, inside the r.Dial do |d| block, first I'm dialing some numbers (for testing, only my mobile phone) and after I'm dialing the client I've just registered for the user that is logged in, with d.Client user_id. So I believe the client you mean is my user's browser.Gustavo Costa
Ah, ok, I think I know where we are. Are you saying that when you click "hang up" in your Twilio Client application, that is before you have answered the call? That is, you want to reject the call from the web interface without any of the numbers or client picking up?philnash

1 Answers

0
votes

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.