Twillo,making call with REST API,call gets disconnect after receiving - twilio

In outgoing call,when make call using REST API then call gets disconnected after reading the twiml on the my_url that we pass when creating call.
I want to know what TWIML or what i do on that my_url to keep the call go as normal call(means talk to the person who I have called).
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);
$call = $client->calls->create(
"+14155551212", "+14158675309",
array("url" => "my_url")
);
echo $call->sid;
The twiml is
<Response>
<Say>Hello </Say>
</Response>
I want to connect the same caller.Like if agent A dail a user number then I want to connect the agent A with user.and I don't want to dail another number to connect with user

Related

System Initiated IVR Call to trap Voice Response

I need to make an IVR app in php where the syetm initiates the outbound call and traps a voice response from user.
How may i go about doing this?
This is my code (does not trap response from user)
voice.xml contents
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/complex_gather.xml -->
<Response>
</Response>
completed.php contents
file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);
Main Code
<?php
require_once 'vendor/autoload.php';
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 = "xxx";
$token = "xxx";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+1310XXXXXXX", // to
"+15676777774", // from
[
"method" => "GET",
"statusCallbackMethod" => "POST",
"url" => "http://xxxx.com/voice.xml"
]
);
$response = new VoiceResponse();
$gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
$gather->say('Enter something, or not');
echo $response;
Twilio developer evangelist here.
When you create a call through the Twilio API, as you are doing, you pass a URL parameter.
When the call connects, Twilio will make a webhook (HTTP) request to that URL and will follow the instructions that are returned in the form of TwiML.
Currently your /voice.xml endpoint returns an empty <Response/> which will cause the call to hang up as there is no TwiML to execute.
Instead of returning an empty <Response/> from /voice.xml you should return the TwiML you generate at the end of your script.
So, perhaps voice.xml should be voice.php and look a bit like this:
<?php
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
header('Content-Type: application/xml');
$response = new VoiceResponse();
$gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
$gather->say('Enter something, or not');
echo $response;
You should also return TwiML from your completed.php endpoint too.
<?php
file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);
header('Content-Type: application/xml');
?>
<Response/>

Calling and connecting

I am trying to call two person at same time and want to join the calls. For example, call A and B, once one of them picks up say A , I want to play a message and connect the other person in this case B. So A and B communicates twilio is making the call and connect both of them. Is this possible?
Assuming we are using PHP for this implementation, it would look something like this.
Code to call person A:
<?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 = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Services_Twilio($sid, $token);
$person_a_phone = "+15555555555";
$verified_twilio_number = "+14444444444";
$person_b_phone = "+13333333333";
$call = $client->account->calls->create($verified_twilio_number, $person_a_phone, "http://callback.php?person_b_phone=$person_b_phone", array());
Code To Call person B (ie. callback.php):
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play>http://url-to-play-your-message.com</Play>
<Dial timeout="10" record="true"><?php echo $_REQUEST['person_b_phone'] ?></Dial>
</Response>
References:
https://www.twilio.com/docs/api/rest/making-calls

Twilio: programmatically join conference and play <Say> command or <Play> sound file?

I have two users and I joined them both into a <Conference>.
I would like to have a robot join the <Conference> and then make an announcement.
There are two approaches I'm considering:
Take everyone in the conference, redirect them to a TwiML that plays a sound, and then move them back into the Conference.
Create a bot that somehow joins the Conference and plays TwiML, but it's not clear for me, from the documentation, how to do that.
Twilio developer evangelist here.
Either of those approaches will work, though will have slightly different effects. Redirecting will cut the conference regardless of who is speaking at the time, but a bot joining in may get spoken over. It depends on which will work better for your use case.
To do the redirect, you'll need to run through the list of Conference participants, redirect them by updating their call to a new URL and return TwiML from that URL that plays the sound and redirects back to your original Conference URL. Something like:
$sid = "{{ account_sid }}";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
// Loop over the list of participants and redirect ($client->account->conferences->get(CONFERENCE_SID)->participants as $participant) {
$call = $client->account->calls->get($participant->call_sid);
$call->update(array(
"Url" => "http://example.com/conference_message"
));
}
Then your /conference_message endpoint would need TwiML like this:
<Response>
<Play>http://example.com/message.mp3</Play>
<Redirect>http://example.com/conference</Redirect>
</Response>
On the other hand, having a bot enter the room requires you to create a call to the conference number and supply a URL which points to the TwiML to play the message and then hangup. Like this:
$sid = "{{ account_sid }}";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create(A_TWILIO_NUMBER, THE_CONFERENCE_NUMBER, "http://example.com/conference_message");
Then your /conference_message endpoint would return TwiML like this:
<Response>
<Play>http://example.com/message.mp3</Play>
<Hangup/>
</Response>
Let me know if this helps at all.

Forwarding live Calls to a new Twiml from the browser

I am following the tutorial on https://www.twilio.com/docs/api/rest/change-call-state#post I am coding in php the portion that allows you to forward a current inbound call to a new Twiml URL. How do I get the inbound call Sid? Currently, the call Sid that I am retrieving forwards my browser to the new Twiml URL and hangs up the inbound caller. I think that I may have the wrong call Sid since I want to forward the current inbound caller to the new Twiml URL. Not The Browser. Can someone please give me some advice on retrieving the inbound call Sid to use in this php script? Thanks
Twilio.Device.incoming(function (conn) {
callSid = conn.parameters.CallSid;
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
This is how I am getting the Call Sid. If I input this Call Sid into
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('TwilioAPI/twilio-php-master/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = '';
$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("THE CALL SID I GOT FROM THE JS GOES HERE");
$call->update(array(
"Url" => "http://twimlets.com/message?Message%5B0%5D=I%20finally%20did%20it&",
"Method" => "POST"
));
echo $call->to;
?>
This code forwards the browser which receives the call to the new Twiml URL. Not the inbound caller.

Pass Inbound call between twilio clients

i have several twilio client users
Sky - Kevin - Paul
A twilio registered number connects to callsky.xml by default / that code looking like.
<Response>
<Dial>
<Agent>sky</Agent>
</Dial>
</Response>
a call comes in and goes to sky, she then wants to pass that call over to Kevin or Paul.
how is this possible ?
tried:
$caid = $_REQUEST["CallSID"];
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->get($caid);
$url = 'http://thesite.com/twiml.xml';
$call->update(
'Url' => $url,
'Method' => "GET",
);
This is executed through Ajax, when sky presses a button with the destination users name on it.
<button onclick="divert('kevin')">Kevin</button>
Also: Oddly, the update command shows as a syntax error in my IDE.
Twilio evangelist here.
The update method takes an array, so that might be causing the syntax error:
https://www.twilio.com/docs/api/rest/change-call-state
I'd also recommened checking out the AppMonitor (https://www.twilio.com/user/account/developer-tools/app-monitor) to see if Twilio is logging and errors when trying to retreive the TwiML from your URL.
The URL you point the call at can return static XML, or you ca ndynamically generate it using the PHP helper library. Here is an example of using the <Dial> verb to dial a Client instance:
$response = new Services_Twilio_Twiml;
$dial = $response->dial(NULL, array(
'callerId' => '+14152223333'
));
$dial->client('client-id');
print $response;
This will output TwiML that looks like:
<Response>
<Dial callerId="+14152223333">
<Client>client-id</Client>
</Dial>
</Response>
Hope that helps.

Resources