arjun
arjun

Reputation: 21

Modify live call status twilio

I want to modify the live callStatus of twilio using it's REST API. I have listed all the available queue phone number and want to modify callStatus from queued to call in-progress or ringing.How do i get this?.

Upvotes: 0

Views: 1216

Answers (1)

Devin Rader
Devin Rader

Reputation: 10366

Twilio evangelist here.

You can use the REST API to dequeue a specific call waiting in a queue. To do this you make a POST request to the CallSid you want to redirect, but you have to do it through the Members resource. As part of that HTTP request, you pass in a Url parameter. Doing that tells Twilio to redirect that call out of the queue, make an request to that Url and then start executing the TwiML returned.

$member = $client->account->queues->get('QU5ef8732a3c49700934481addd5ce1659')->members->get("CA5ef8732a3c49700934481addd5ce1659");
$member->update(array(
    "Url" => "http://demo.twilio.com/docs/voice.xml",
    "Method" => "POST"
));

Now, to make this request you need to know the SID of the Queue you want to work with and the CallSid of the Queue Member you want to redirect.

Lets walk through getting those two values.

Locating a Queue SID

There are any number of ways to get a Queues SID. If you use the REST API to create Queues then your app could save the Queue SID each time it creates a new Queue:

$queue = $client->account->queues->create("newqueue", array());
echo $queue->sid;  //save the sid in a db or some other store

or you can use the REST API to list all of your Queues. For example, here I am using REST API to get the list of Queue resources, loop over them and print the current average wait time:

// Loop over the list of queues and echo a property for each one
foreach ($client->account->queues as $queue) {
    echo $queue->average_wait_time;
}

If you wanted to find a specific Queue by using its Friendly Name, you could modify this to loop to look at the *friendly_name* parameter of each Queue.

// Loop over the list of queues and echo a property for each one
foreach ($client->account->queues as $queue) {
    echo $queue->friendly_name;
}

In this case I'm simply printing out the Friendly Name of each Queue.

Locating the CallSid

Once you have the Queue SID you next need to get the CallSid of the call you want to redirect. There are different ways to do this.

You could use the Members resource to list all of the calls waiting in that Queue:

foreach ($client->account->queues->get('QU5ef8732a3c49700934481addd5ce1659')->members as $member) {
    echo $member->CallSid;
}

Each member resource gives you some information about that member, including the CallSid.

If you didn't know the specific calls CallSid and wanted to locate the call based on something like the callers phone number, you can use the Calls resource:

$call = $client->account->calls->getIterator(0, 50, array(
    "Status" => "in-progress",
    "From" => "+15555555555"));
$callsid = $call->CallSid;

Here I am asking Twilio to return me a Call resource whose From phone number is +15555555555" and whose status is "in-progress", and then grabbing that Calls CallSid.

Redirecting the Queued Caller

Now that I have both the Queue SID and the CallSid I can redirect the caller out of the Queue and to another experience:

$member = $client->account->queues->get('QU5ef8732a3c49700934481addd5ce1659')->members->get("CA5ef8732a3c49700934481addd5ce1659");
$member->update(array(
    "Url" => "http://demo.twilio.com/docs/voice.xml",
    "Method" => "POST"
));

Hope that helps.

Upvotes: 1

Related Questions