Modify live call status twilio - 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?.

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.

Related

Twilio Conference callback

I am not able to get status callback from conference
I use:
PHP as backend Client
TwilioJs as frontend for my softphone.
Call flow looks as follows:
Inbound Call -> IVR -> enqueue Worker (Conference) -> Connect task to relative worker using JS SDK (client:support)
Usually i add statusCallback parameter to $dial->conference class but unfortunately I can't apply same method to this specific use case since conference is auto generated from enqueue class and enqueue doesn't have any callbacks.
what can be done to retrieve conference SID?
One way is to search for it using:
Read the conferences named MyRoom that are in progress
Twilio developer evangelist here.
I think you're saying that you connect your worker to the call using the JS SDK and the reservation.conference function.
If that is the case, then you can set a status callback URL in that function. The final argument to reservation.conference is an object of options and you can include the property ConferenceStatusCallback, like this:
var options = {
"ConferenceStatusCallback": "https://requestb.in/wzfljiwz"
}
reservation.conference(null, null, null, null, null, options);
That will send conference events to the URL you provide.

How do I mark a call as completed once I transfer it to another number using Twilio API

The code below intercepts a call in progress and transfer the call to a new number (This piece works as expected).
The question is:
Should I mark the original call as "completed"
How do I do this?
$call_sid = $_SESSION['CallSid'];
$sid = 'xxxxxxxxxxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxxxxx';
$twilio = new \Twilio\Rest\Client($sid, $token);
$call = $twilio->calls($call_sid)->update(['twiml' => '<Response><Say>Redirecting to Buba</Say><Dial callerId="+18888880592">+14888068886</Dial></Response>']);
//TODO cancel this CALL SID. Status=completed??
print($call->to);
The Twilio call legs are kept up via Twilio Markup Language (TwiML). If you modify one call leg which appears to be what you are doing, if there is another call leg (no conference involved) that is part of that call, that leg will begin processing any TwiML after the <Dial> that originally connected the two parties. If there is no TwiML after the <Dial>, Twilio will hangup that call leg, so no action required on your side.
You should be able to see this behavior in your call logs.

Open inbound calls redirects to another Twilio client when hang up

I have two available Twilio <client> to receive inbound calls on my website (after caller enters a code) using JS Quickstart and Twilio client PHP library, these calls are recorded and registered on my database.
I've noticed when one <client> hangs up an open inbound call , this call goes to the website of the other <client> I have have 2 registers on my database instead of one.
This is an example:
<?php
//...
//Validating connection status
//$estd is the connection status I get via AJAX
if($estd=="open"){
$call = $twilio->calls($callsid)->update(array("status" => "completed"));
$calla = $twilio->calls($callsid)->fetch();
$parentCall = $calla->parentCallSid; //parent call
$calld = $twilio->calls($parentCall)->update(array("status" => "completed"));
} else if($estd=="pending"){
$call = $twilio->calls($callsid)->update(array("status" => "completed"));
$calla = $twilio->calls($callsid)->fetch();
$parentCall = $calla->parentCallSid; //parent call
$calld = $twilio->calls($parentCall)->update(array("twiml" => '<Response><Dial timeout="20" record="record-from-answer" recordingStatusCallback="https://mywebsite.com/record.php" recordingStatusCallbackEvent="in-progress completed absent"><Client><Identity>the_other_client</Identity><Parameter name="numdoc" value="user_code"/></Client></Dial></Response>'));
}
?>
How can I fix it?
I'd like your help.
I believe this could be occurring because a single incoming voice call will trigger both of your $estd if statements.
According to Twilio documentation (https://www.twilio.com/docs/voice/client/javascript/connection#status), an incoming call will first have the status pending, and then the status open. If your statuscallbackURL is set for when the status changes, you may be calling your record-keeping code more than once. Depending on how you have your SQL you may be inserting new records each time.
The way to prevent the double record is to save the resource SID for the call in your database and insert on duplicate key or update to prevent creating new records.
Alternatively, if the code snippet you are displaying is from record.php, when you make your outbound call using Twiml, you use a callbackURL to call record.php -- at which time you may be creating a new record in your database (as this call will have it's own unique SID). If you want to attach it to the current record, then you will need to create a different callbackURL for those outbound dials made from this script.
(But it is difficult from the details you have provided to know what code is being called when).

Agent to see real caller number with Twilio <Queue>

I've built an IVR system and I cannot achieve how agents can see real callers numbers, not the proxy number (aka Twilio's number):
$response->enqueue('commercial', ['waitUrl' => 'https://xxxxxxxion.com/playStory']);
$call = $client->calls->create(
'+407xxxxxxx15',
'+403xxxxxxx56',
array("url" => "https://xxxxxxxx.com/queues.php?type=commercial", 'timeout' => 15, 'record' => true, "statusCallback" => "https://xxxxxxxxx.com/notify.php", "callerId" => $_REQUEST['From'])
);
I have tried an alternative with "callerId" => $_REQUEST['From'] to get the Caller ID at the end of the call, but this parameter doesn't appear nowere.
With SIP calls is simple because I can set From parameter directly in the method call:
$call = $client->calls->create(
'sip:xxxxxxx.sip.us1.twilio.com',
$_REQUEST['From'], //here I set real Caller ID for Agent to see
Tried also with call forwarding, but cannot put the callers into a queue, or, at least to have a waitUrl for on-hold audio - the part I really need in this story.
Twilio developer evangelist here.
With Twilio you can only set the From parameter, the caller ID, of an outbound call, the call you are making using the REST API, to a number you have either bought with Twilio or verified that you own.
The only way to forward on the original caller ID is to use <Dial> to connect the calls, but you say you need wait music.
One alternative you could use is to perform a call whisper that reads out the number that the agent is about to be connected to, using <Say>, and then connecting them to the queue.
Let me know if that helps at all.

Hold, unhold feature for Twilio call which is already running

I've a Twilio call already routed to an agent via TaskRouter(Task, Reservation is already created, Agent has accepted the call) and now an agent needs Hold, unhold feature for the same Twilio call.
Can someone please provide best practice for this?
Can we do this by en-queuing a call to Workflow?if yes then, how should we redirect same call to the same agent for Un-hold feature?
Please let me know if anyone has code snippet available. I'm using PHP for this
You can handle this one of a few ways but the basic process is:
First, provide Twilio some TwiML that places the call into "hold" then
when you're ready, use the CallSid of the call and the REST API to redirect the live call.
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "YOUR_ACCOUNT_SID";
$token = "YOUR_AUTH_TOKEN";
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get("CALL_SID");
$call->update(array(
"Url" => "http://demo.twilio.com/docs/voice.xml",
"Method" => "POST"
));
echo $call->to;
To actually handle the "hold" you can use the <Enqueue> verb as you guessed, placing the call into a call queue. Then when you are ready, redirect that call back out of the queue to a new experience.
Or you can use <Play> and set the loop attribute to zero, which tell Twilio to loop audio indefinitely. And again, redirect the call when you are ready to remove the hold.

Resources