Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twilio: Ruby: Callback status URL for calls running in parallel: Which call completed?

Tags:

ruby

twilio

I have a list of phones which must be called by my Twilio app every so often. I have a cron job which runs every minute which makes a list of all phones scheduled to be called in the next minute. The list comprises calls which are scheduled to run in the next minute along with calls which did not complete properly in the last hour.

For each phone in the list, I have code which looks like this (in Ruby) to start off list of phone calls (the list "phones") which will run in parallel, and this snippet runs every minute.

phones.each do |phone|
    callbackurl="http://myapp.com/twiliocallback?phone=#{phone.id}"
    data={:from=>'16135551234', 
      :to=>phone.number, 
          :url=>callbackurl
         }

    client=Twilio::REST::Client.new(ACCOUNT_SID, ACCOUNT_TOKEN, :ssl_verify_peer => false)
    client.account.calls.create data
end

However, if a phone call takes longer than a minute, I don't want the cron job to trigger a call to the same number while it is already in conversation with Twilio. Also, if a person hangs up the phone in the middle of a call before I can update the status, I want that number to be called again triggered by a subsequent cron.

I know that I need a status attribute for the phone call (e.g. phone.status) with the values NOT_STARTED, IN_PROGRESS, and SUCCESSFULLY_COMPLETED, and a twilio_status attribute (e.g. phone.twilio_status), with the values TWILIO_NOT_STARTED, TWILIO_IN_PROGRESS, and TWILIO_COMPLETED.

Calls start off as

phone.status="NOT_STARTED" 
phone.twilio_status="TWILIO_NOT_STARTED"

and, and as soon as I create the phone call, I can update the status of the call:

phone.status="IN_PROGRESS" 
phone.twilio_status="TWILIO_IN_PROGRESS"

If the call completes properly within the success path, I can set the status

phone.status="SUCCESSFULLY_COMPLETED"

If I can figure out when the call has disconnected from Twilio , then I can tell if a call was aborted by doing this

is_aborted? = twilio.status=="IN_PROGRESS" && twilio.twilio_status=="TWILIO_COMPLETED"

However, I don't know how to run the code

phone.twilio_status="TWLIO_COMPLETED"

for a particular phone call when the phone call hangs up in the middle of a Twilio call workflow or even at the end of a workflow.

Twilio seems to have a callback URL which can be called when a phone call completes, but it is not clear how the callback handler can figure out which of the parallel calls completed. Is there a way to do this so that I can tag the right call with the right status?

like image 606
Jay Godse Avatar asked Dec 01 '25 06:12

Jay Godse


1 Answers

In every request to your server throughout the call and in the status callback URL request Twilio passes the unique CallSid value to your server to identify a call. This is also returned to you in the call XML or JSON data that you get back when you first initiate the call.

https://www.twilio.com/docs/api/twiml/twilio_request
https://www.twilio.com/docs/api/rest/making-calls

You can store this CallSid value to track status throughout the call's lifecycle.

like image 82
John Sheehan Avatar answered Dec 05 '25 06:12

John Sheehan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!