How to make browser-phone call with twilio - php

i am currently stacked with twilio client call where i have to call a user from browser. Lets see the scenario.
start snippet:
include 'twilio/Services/Twilio/Capability.php';
include 'twilio/Services/Twilio.php';
$accountSid = 'ACxxxxxxxxxxxxxxx';
$authToken = 'xxxxxxxxxxxxxxxxx';
$token = new Services_Twilio_Capability($accountSid, $authToken);
$token->allowClientOutgoing('APXXXXXXXXXXX');
$client = new Services_Twilio($accountSid, $authToken);
$call = $client->account->calls->create("twilio Number", "client number", "https://www.mysite.com/twilio/callback", array());
And my call back goes like this:
function callback(){
$xml = "<Response><Say>Placing your call now.</Say><Dial callerId='twilio verified number' timeout='40' record='true'></Dial></Response>";
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $xml;
exit();
}
I dont know what exactly the $from, $to parameters with in the calls->create() function takes. If we are initially initiating the call to $to then, it should work.
My requirement is to call a client to client number from my browser. Yes, i see the referencees here but stacked again.

Twilio evangelist here.
Looks like your current code is using the REST library to initiate the call. This send a message to Twilio to initiate a phone call from Twilio to some phone. All of that happens on the server, not in the browser.
In your case it sounds like you want to actually make a phone call right from the browser. To do that you need to use Twilio Client, which is a JavaScript framework that lets you create an audio connection from a browser to Twilio, and the connection is made using WebRTC (or Flash if needed). Its the job of the Twilio Client SDK to figure out how to create and manage the actual audio connection between your browser and Twilio.
http://www.twilio.com/client
https://www.twilio.com/docs/tutorials/walkthrough/browser-calls/php/laravel
This is different than initiating a call using the REST API. When you initiate an outbound call using the REST API, the API does not actually manage the audio connection. Your using the API simply to tell Twilio to initiate a phone call between Twilio and a phone (or to send a text message), or to ask Twilio for information (like getting list of send SMS messages). The management of the actual audio connection is handled by Twilios internal systems.
The first part of your PHP code is correct you need to create a Twilio Capability token. That token is used by our JavaScript library. This tutorial shows the JavaScript code that you can use to make a call from your browser to Twilio:
https://www.twilio.com/docs/tutorials/walkthrough/browser-calls/php/laravel
Once the browser connects to Twilio, Twilio will make a request to the URL you've set on your TwiML App (the string starting with "AP" that you specified when you created the capability token). You can return whatever TwiML you like from this URL, including using the <Dial> very to tell Twilio to bridge the Twilio Client connection into a <Conference>.
Hope that helps.
Devin

This will be helpful. If you look through the code, there is some api (i think callFromBrowser()) which does the trick.

Related

How to set up twilio taskrouter outbound call?

I am trying to set up outgoing calls through Twilio task router. I am creating tasks through PHP with the all the necessary attributes (instruction, to , from, post_work_activity_sid ) but the created task doesn't set up a call between the twilio client and the external phone number. I was hoping that tasks created by the program would create a conference call between worker(browser) and external client. I keep getting an error that is shown below. I have a assignment php on my application server which de-queues calls to my workers (Browser clients). Currently, incoming calls from external number to browser clients through task router is working as expected. However, outbound calls generates a task and a reservation is assigned but Twilio is not able to dequeue the call to a worker.
Is there a way to create a task for voice call such that the task is created using Twiml Enqueue verb? Or is there a better way of handling outbound calls using the Twilio taskrouter so that calls are assigned successfully to the workers using Browser client ?
As per this thread: Can outbound calls be made through Twilio TaskRouter, I tried using instruction call.I have also gone through the documentation and another stack overflow post about assignment callback URL but it's not clear and am not sure what I could be potentially doing wrong.
Error message:
The dequeue instruction can only be issued on a task that was created using the TwiML verb
<?php
require_once('TwilioVendor/autoload.php'); // Loads the library
use Twilio\Rest\Client;
$sid = "ACxxxxxxxxxxxxxxxxxxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
try{
$twilio = new Client($sid, $token);
$task = $twilio->taskrouter->v1-
>workspaces("WSxxxxxxxxxxxxxxxxxxxxxxxxxxxx")->tasks-
>create(array("attributes" => json_encode(array(
//"instruction"=>"accept",
//"instruction"=>"conference",
"instruction"=>"call",
"to"=> "client:Bob",
"from"=> "+61123456789",
"post_work_activity_sid"=> "WAxxxxxxxxxxxxxxxxxxxx"
)),
"workflowSid" => "WWxxxxxxxxxxxxxxxxxx"
)
);
}catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
print($task->sid);
**Assignment Callback code**
<?php
$assignment_instruction = [
'instruction' => 'call','to'=> 'client:Bob',
'from' => '+61xxxxx','url'=>'CRM REST END POINT'
];
header('Content-Type: application/json');
echo json_encode($assignment_instruction);
**CRM REST END POINT TWIML**
<?php
require __DIR__ . '/vendor/autoload.php';
require_once 'TwilioVendor/autoload.php';
use Twilio\Twiml;
$reservationSid= $_REQUEST['rsid']
header('Content-Type: text/xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="woman">You will now be connected to the customer</Say>
<Dial>
<Queue reservationSid="<?$reservationSid?>"/>
</Dial>
</Response>
Twilio developer evangelist here.
TaskRouter will only generate calls to your workers when a task is created by the <Enqueue> TwiML verb. Creating a task with the REST API, even if you add call attributes, will not generate a call when you use the dequeue or call instruction.
Instead, you will need to manage the task and call yourself. When your worker is sent the reservation and accepts it, you should use the REST API to create the call, connect it to your browser client and then dial out to the end user.

How to put Twilio call on hold

I am trying to implement following on Twilio, but not sure why I am not able to get it done correctly. I have initiated a call using standard Twilio procedures from twilio.device.connect. After a call is initiated, I am updating the call to new url in order to put it on hold.
$client = new Services_Twilio($accountSid, $authToken);
$call = $client->account->calls->get($call_sid);
$call->update(
array(
"Url" => "http://localhost/voice.xml",
"Method" => "POST",
)
);
Now here instead of putting end user on hold it just disconnects the call, and play music on my side. Why it is happening?
Twilio evangelist here.
I'd suggest checking to see if Twilio is logging any errors:
https://www.twilio.com/user/account/monitor/alerts
If you are trying to redirect Twilio to "http://localhost", thats not going to work because Twilio obviously does not know how to reach the localhost running on your own machine.
If you want to expose a web server running on your own local machine to the internet via a public URL check out an awesome tool called ngrok.
the reason is that after the <Play> tag in your
"http://localhost/voice.xml" file.. there is no further TwiML that gets executed.
The solution is to redirect the call back to its original state.

Twilio Multiple Number Dialing, What is Equivalent in REST API of Given TWIML

As we know it is very easy to dial multiple numbers at once via TwiML. Note That, Once one of the dialed numbers picks up. Rest of the numbers are disconnected automatically.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>415-123-4567</Number>
<Number>415-321-7654</Number>
<Number>415-456-7890</Number>
</Dial>
</Response>
But What would be the Equivalent of this REST API? Considering I am using PHP helper libraries. I can make single number call like this.
// 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);
$call = $client->account->calls->create("+14158675309", "+14155551212", "http://demo.twilio.com/docs/voice.xml", array());
My guess is I can loop through numbers to create single calls. But how do disconnect other numbers when one call is picked up?
Twilio evangelist here.
I think what you'd need to do in order to create a simul-dial app using the REST API is create a loop that initiates all of the outbound calls you want to make. Each time you start a new call save the CallSid for that call in an some kind of datastore like a database.
Which ever call answers first it is going to make an HTTP request to the URL you specified when you created the call. In that PHP file you can loop over that list of CallSids you saved earlier and use the REST API to set all but that first calls Status property to "completed". Doing this tells Twilio to hang up on all of the other calls.
Hope that helps.

convert text to voice using twilio in php

I want to convert text to voice message and send to users phone no
currently i am using making call api with TwiMLTM
<?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 = "xxx";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+343443", "+3444", "http://demo.twilio.com/docs/voice.xml", array(
"SendDigits" => "1234#",
"Method" => "GET"
));
echo $call->sid;
it is working,but this make call to user,but we need to voice message
Note : message is coming from textarea
Twilio Evangelist here.
This is a little tricky. Twilio allows you to make phone calls, so if the dialled number is answered by voicemail, you can leave a message. If it is answered by a human being, then you'll need to interact with them. It is possible to use the if_machine parameter when creating a call to detect an answering machine. However you cannot 'send' a voice mail as if sending an SMS or an email. You need to make the call, and decide how to handle that depending on who/what answers.
You could try sending the message as an SMS however, which would deliver the exact text to the user:
$sms = $client->account->messages->sendMessage("+343443", "+3444", $message_text);
However, if the number you are sending to is not able to receive an SMS, then you need to make the call and interact with either a human or answering machine.
Best of luck!

Pass dynamic TwiML when making calls

How can I pass dynamic TwiML to the Twilio API when making calls?
$client = new Services_Twilio($sid, $token);
So instead of passing a URL to fetch the TwiML:
$call = $client->account->calls->create("+14158675309", "+14155551212", "http://demo.twilio.com/docs/voice.xml", array());
Could I dynamically generate the TwiML and pass it to the API?
$twiml = new Services_Twilio_Twiml();
$twiml->say( 'Hello Mark');
$call = $client->account->calls->create("+14158675309", "+14155551212", $twiml);
Twilio evangelist here.
Instead of specifying a static XML file in the create function, you can make this a PHP file and dynamically generate the response.
You still use create() to tell Twilio to initiate the phone call. When the call is answered, Twilio will request the URL you've specified to get the TwiML that tells it how to proceed with the call. So for example you change:
http://demo.twilio.com/docs/voice.xml
to
http://demo.twilio.com/docs/voice.php
And have the PHP generate the TwiML output:
$twiml = new Services_Twilio_Twiml();
$twiml->say( 'Hello Mark');
Here is the documentation for generating TwiML using the PHP helper library:
https://github.com/twilio/twilio-php#generating-twiml
Hope that helps.
Could I dynamically generate the TwiML and pass it to the API?
The answer is no, not without a URL. I realize this a delayed response but I have recently been searching for a way around this. The best method I have found of generating dynamic text for outgoing calls without a web server is here - bouncing it off of the twimlets url.

Categories