Get phone number with twilio conference participant sid - twilio

How can I get a conference participant's phone number using the Twilio API? I am using the official Twilio PHP Library.

Hello Twilio Developer Evangelist here. 👋
What you can do is to fetch all active participants with a snippet like this (you can find more details in the docs for the conference participants.
<?php
// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$participants = $twilio->conferences("CFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->participants
->read(array(), 20);
foreach ($participants as $record) {
print($record->callSid);
// "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
Now, when you did this you'll see that the participant record does not include the phone number of the participant. But what it includes is the call_sid. The call sid helps you to identify the caller and retrieve call details.
There you have two options (I described something similar just recently here).
1. Persist the call information when you route callers into the conference
What you could do when you route people into the conference is to persist their numbers and call sids in your application. This way you could have a registry with all the people that entered your conference and access their phone numbers quickly when you need them.
2. Make another API call to get the call details and caller number
When you have the call SID what you can always do is to fetch the call details one by one.
<?php
$call = $twilio->calls("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->fetch();
print($call->from);
The first approach saves you API calls with the cost of maintaining your own persistence layer. The second approach makes more API calls but it is easier to set up.
As always, it depends on the situation.
I hope that helps. :)

Related

Conf call: Wait for first person to respond, then connect second person

Basic case: System will call person A. If person A picks up the phone it will call person B and the 2 will be connected.
I read several answers here such as https://stackoverflow.com/a/20976996/1907888 but it's still unclear.
Would the following work? It would call PERSON_A if person responds it will connect to conference then call PERSON_B and connect to same conference? Do I need to start the conference first?
$response = new VoiceResponse();
$dial = $response->dial('PERSON_A');
if($dial->conference('Room 1234')) {
$dial = $response->dial('PERSON_B');
$dial->conference('Room 1234');
}
Twilio developer evangelist here.
When you control calls with Twilio there are two mechanisms by which it works. There is the Twilio REST API which your application can use to make things happen, like start or change a call. Then there are webhooks, which are HTTP requests that Twilio makes to your application when things change in a call, like someone dialling your Twilio number, entering data on the phone, or an person answering an outbound call. You respond to webhooks with TwiML, a subset of XML, with instructions for what to do with the call next.
In this case, you want to place a call to person A to start with. For this, you will need the REST API to make that call. When person A answers the phone, Twilio will then make a webhook request to your application to find out what to do next. It is at this point that you can both call person B, again using the REST API, and place person A into a conference call, by responding with TwiML.
So, your initial outbound REST API call should look a bit like this:
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create($personANumber, // to
$yourTwilioNumber, // from
["url" => "http://example.com/conference.php"]
);
The URL you send when you place the call will be where Twilio sends the webhook request. So, in response to example.com/conference.php in this case, you will need to dial another person and respond with TwiML to direct person A into the conference call.
This time, instead of sending a URL, you can actually send TwiML in the REST API response. Something like this:
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$twiml = new VoiceResponse();
$dial = $twiml->dial();
$dial->conference("Conference Name");
$call = $twilio->calls
->create($personBNumber, // to
$yourTwilioNumber, // from
["twiml" => $twiml->toString()]
);
echo $twiml.toString();
In this case, I have used the same TwiML for both legs of the call, because they are both entering the same conference. You could respond with different TwiML based on what's happening.
Let me know if that helps at all.

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.

Put caller on hold twilio version=1.5

I'm trying to implement hold calls functionality with Twilio api.
What is the best way in 2018 without TaskRouter to put caller on hold?
I've been tried to use enqueue and conference but failed.
Maybe someone have the example of code for this. Thanks for helping!
Twilio developer evangelist here.
There is actually a direct way to place a caller on hold in a conference call with Twilio. Using the participants resource you can place a participant on hold by updating the resource, setting Hold to true. You can set a URL to play hold music by updating the HoldUrl at the same time.
See below for an example in PHP:
use Twilio\Rest\Client;
$sid = "your_account_sid";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$participant = $twilio->conferences("conference_sid")
->participants("participant_sid")
->update(array(
"hold" => True,
"holdUrl" => "http://www.myapp.com/hold"
)
);
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.

Dynamically set Twilio <Dial> timeLimit

I have an app which lets the users dial number(s) they want to add to a call. Each user is subjected to Balance they have in their account.
The dial is performed by using TwiML <Dial>
So as per my amount per minute rate i calculate the remaining balance in terms of seconds and set that as a timeLimit for <Dial>.
I want to do a simple thing like when the user is in a call and his call timeLimit is about to expire, I would want to charge them using my payment methods and If the charge was a success replenish the timeLimit for the same call.
Can this be done?
Twilio Developer Evangelist here.
There isn't a way to modify the timeLimit on a dial while the call is in progress. But I think I have a solution that could work for you.
Instead of dialing the number directly you could call into a conference with a timeLimit.
<Response>
<Dial timeLimit="30">
<Conference>YourCall</Conference>
</Dial>
</Response>
Then when their account is replenished you could modify the live call to redirect to a TwiML url that rejoins the conference call with the new timeLimit:
<?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 = "{{ sid }}";
$token = "{{ 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://youserver.com/conference.xml",
"Method" => "POST"
));
echo $call->to;
Probably easier is to use the https://www.twilio.com/docs/api/rest/change-call-state function of the Twilio REST API.The REST API is asynchronous.
In your situation you can do it as follows:
Dial timeLimit=[Max] (for an unlimited time, 4 hours is the max)
After a while, try to recharge the account.
On recharge success: Do nothing, the call persists.
On recharge failure: Disconnect by executing the change-call-state function of the Twilio REST API. You could even play an audio file or do other stuff before disconnect. For example ask the caller to verify its account because recharge failed or something.

Resources