Pass Inbound call between twilio clients - twilio

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.

Related

Twilio outgoing call to recipient with call screening (Google voice)

I have a working service, where we do outgoing calls something like this.
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+14155551212", // to
"+14155551212", // from
["url" => "http://demo.twilio.com/docs/classic.mp3"]
);
print($call->sid);
The issue is when the recipient has call screening, the callback to the URL parameter is done as soon as the screening starts (with callbackstatus "in-progress"!), which means we have to add a pause in the response.
<Response>
<Pause length="10"/>
<Say>
This is an important message
</Say>
<Gather action="https://xxx" method="GET" timeout="15" speechTimeout="auto" numDigits="1" input="dtmf speech">
<Say>Please press 1 followed by the pound sign or say confirm to confirm your appointment</Say>
</Gather>
<Redirect method="GET">https://xxx</Redirect>
</Response>
Is there some way to bypass the screening or have the system not start the response in screening?
EDIT:
I've added the AMD options and it seems to be correctly somewhat working. Right now the only issue that remains is when calling in to Google Voice, when there's call screening, my added voice recording start reading before the recipient actually answers
$call = $client->calls->create(
$to, $from,
array(
"url" => $url,
"statusCallback" => $statusURL,
"statusCallbackMethod" => 'POST',
"machineDetection" => "DetectMessageEnd",
"machineDetectionTimeout" => 5
)
);
You are looking for answering machine detection (AMD).
You can make a call with AMD enabled by setting the MachineDetection parameter to Enable or DetectMessageEnd.
$call = $twilio->calls
->create("+14155551212", // to
"+14155551212", // from
[
"url" => "http://demo.twilio.com/docs/classic.mp3",
"machineDetection" => "Enabled"
]
);
When you use the MachineDetection parameter the request to your url will include an AnsweredBy parameter. From the docs:
Use Enable if you would like Twilio to return an AnsweredBy value as soon as it identifies the called party. This is useful if you would like to take a specific action — for example, connect to an agent, play a message) — for a human but hang up on a machine.
If you would like to leave a voicemail on an answering machine, specify DetectMessageEnd. In this case, Twilio will return an AnsweredBy immediately when a human is detected but for an answering machine, AnsweredBy is returned only once the end of the greeting is reached, usually indicated by a beep.
There is also an option for asynchronous AMD. With asynchronous detection your url is called immediately as if a normal call, but once Twilio has performed the detection an asynchronous webhook is made to a different URL and you can use the callback data to decide whether to update the call.

How to redirect call to mobile device for Twilio

I am creating an app that needs to redirect calls to a mobile device using Twilio.
Using the PHP SDK for Twilio I can make calls to mobile devices using this syntax:
$client = new Client($sid,$token);
$call = $client->calls->create(
"client:voice_test", "client:quick_start",
array("url" => "http://demo.twilio.com/docs/voice.xml")
);
I can redirect a call to a new URL like this:
$call = $client
->calls("CAe1644a7eed5088b159577c5802d8be38")
->update(
array(
"url" => "http://demo.twilio.com/docs/voice.xml",
"method" => "POST"
)
);
Can anyone explain to me using the PHP SDK or really in any language how to redirect a call to a mobile device?
You can do this with a TwiML Bin.
Configure your Twilio number voice settings for "A CALL COMES IN" with "TwiML" (by default is configured with a webhook URL). To create a new bin, there is a "+" in a red circle sign, to the right of the "TwiML" option. You can only see it after you change from Webhook to TwiML
The code for the bin for redirect is:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>1-555-555-5555</Dial>
</Response>
Replace 1-555-555-5555 with your cell number. Save the bin.

when we make call using rest api then what twiml we use on that url parameter

I am Creating the call using the rest api-
try{
// Initiate a new outbound call
$call = $this->client->calls->create(
// to call.
"num2",
// Step 5: Change the 'From' number below to be a valid Twilio number
// that you've purchased or verified with Twilio.
"num1",
array("url" => "url-tw",
'IfMachine'=>'Continue')
);
echo "Started call: " . $call->sid;
} catch(Exception $e){
echo "Error: " . $e->getMessage();
}
and on the url-tw what twiml should I use which can't disconnect the call.
Before I was handling the call using the TwiML but now I have to detect the AnsweredBy option which is only available if I make the call using the REST API so.
for now I m using the same twiml I have used before when I was making calls using the twiML like use the <Dial> which let to dial again but if I dont use any twiml it disconnect the call.So any advice where I m going wrong.
Twilio evangelist here.
The value of the url parameter should be a publicly accessible URL that returns TwiML containing the instructions that you want Twilio to execute to the caller.
Your PHP to start the call would look like:
// Initiate a new outbound call
$call = $this->client->calls->create(
"num2",
"num1",
array("url" => "http://example.com/answer.php", 'IfMachine'=>'Continue')
);
Then in answer.php, you can do two things:
Check the AnsweredBy parameter to see if Twilio detected a machine or a human.
Generate the Twiml you want to return based on that value
For example to say something different to a machine v a human, you could do something like this in your PHP:
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<?php if ($_REQUEST['AnsweredBy']=='machine'): ?>
<Response>
<Say>Hello machine</Say>
</Response>
<?php else: ?>
<Response>
<Dial>+15555555555</Dial>
</Response>
<?php endif ?>
Hope that helps.

Unable to transfer twilio call to new number

We have small twilio application which we use to calls any customer number from website. Now we are trying to add transfer call functionality in our application.
But we are unable to transfer call using php api.
Here is what we are using :
We dial number from website, JS returns callid
We have another number on which we need to transfer call after few minutes
So we have "Transfer" button which makes ajax call to php file
which have following code :
<?php
$existing_call_sid = $_REQUEST['CallSid'];
$new_number = $_REQUEST['new_number'];
$call = $client->calls($existing_call_sid)->update(
array(
"url" => "transfer_xml_main.php?new_number=".$new_number,
"method" => "POST"
)
);
echo $call->to;
?>
transfer_xml_main.php contains :
<Response>
<Dial><?php echo $_GET['new_number'];?></Dial>
<Say>Please be on line we are transferring your call</Say>
</Response>
But when this request happens existing call is dropping and no call to new number.
also $call->to is blank
Am i doing anything wrong?
Update
After implementing answer of philnash i am getting following fatal error :
<b>Fatal error</b>: Uncaught exception 'Twilio\Exceptions\RestException' with message '[HTTP 400] Unable to update record: No 'To' number is specified' in /twilo/twillo_php_master_new/Twilio/Version.php:85
Stack trace:
#0 /twilo/twillo_php_master_new/Twilio/Version.php(127): Twilio\Version->exception(Object(Twilio\Http\Response), 'Unable to updat...')
#1 /twilo/twillo_php_master_new/Twilio/Rest/Api/V2010/Account/CallContext.php(109): Twilio\Version->update('POST', '/Accounts/AC618...', Array, Array)
#2 /twilo/twilo_call_transfer.php(26): Twilio\Rest\Api\V2010\Account\CallContext->update(Array)
#3 {main}
thrown in <b>/twilo/twillo_php_master_new/Twilio/Version.php</b> on line <b>85</b><br />
However i am getting parent call id correctly and in $child_calls i am getting to and from correctly which is one who called first and one whom user is called. Still anything wrong ?
And yes we want exactly like you said :
1. User1 (agent) called one number (customer A) from twilio JS Client
2. Now User1 (agent) want to transfer call to another number which can be agent or some other number.
Also there is no errors in debugger
Twilio developer evangelist here.
I'm surprised that no call is happening. Are there any errors in your Twilio debugger?
I can see some issues though.
The CallSid you get from the JS when you dial out is the Sid of the dialling leg, the one owned by your agent. But, I'm guessing you want to transfer the person that is called to a new number. If that is the case then you need to get the Sid of the receiving leg of the call. The dialling Sid is the parent Sid for the calls, so you can look up the other leg by listing child legs, like this:
<?php
$parent_call_sid = $_REQUEST['CallSid'];
$child_calls = $this->client->calls->read(array("ParentCallSid" => $parent_call_sid));
$child_call_sid = $childCalls[0]->sid;
$new_number = $_REQUEST['new_number'];
$call = $client->calls($child_call_sid)->update(
array(
"url" => "transfer_xml_main.php?new_number=".$new_number,
"method" => "POST"
)
);
echo $call->to;
?>
Secondly, your TwiML you return is the wrong way round. If you want the message to come before you start dialling, then you need to put the <Say> first.
<Response>
<Say>Please be on line we are transferring your call</Say>
<Dial><?php echo $_GET['new_number'];?></Dial>
</Response>
When you do all of this, your original dialler will get cut off as the call is transferred. You may want this, though you may find that a warm transfer is a better experience. There is a good tutorial on how to perform a warm transfer using PHP and Laravel which might help.
Let me know if that helps at all.
In your PHP you use the POST method, but in your XML you use $_GET. You're not passing the variables.
<?php
$parent_call_sid = $_REQUEST['CallSid'];
$child_calls = $this->client->calls->read(array("ParentCallSid" => $parent_call_sid));
$child_call_sid = $childCalls[0]->sid;
$new_number = $_REQUEST['new_number'];
$call = $client->calls($child_call_sid)->update(
array(
"url" => "transfer_xml_main.php?new_number=".$new_number,
"method" => "POST" // <---NOTICE THE POST
)
);
echo $call->to;
?>
Change $_GET to $_POST
<Response>
<Dial><?php echo $_POST['new_number'];?></Dial>
<Say>Please be on line we are transferring your call</Say>
</Response>

Make a call between two numbers not registered in twilio

There's some way of make a call between two of my users? I mean... I have a twilio acount with a registered number and I have to make a call to my client "Bill" so when he answer that, the call should be redirected to another client, that Bill choosed, let's say "Joe". So, Bill click's a button and he's phone rings, he answer that and start to call Joe. When some of them hangup, the all call should be over. Have someone ever made that? Help me! And I'm sorry about my bad english
(oh yeah, I'm using php for that)
This is just something simple to get you going, you can also look at connecting to a conference room https://www.twilio.com/docs/api/twiml/conference
You will need to use the Twilio PHP Helper Library (the "Services" folder from there) download from https://www.twilio.com/docs/libraries/php#install-via-zip-file
Project structure
/
/Services
/callBill.php
/callJoe.php
callBill.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<!-- // calling Bill -->
</Response>
<?php
// Include the Twilio PHP library
require 'Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// A phone number you have at Twilio
$phonenumber = '5557779999';
// The URL Twilio will request when the call is answered
$twilioRequestUrl = "http://somewebsite.xyz/callJoe.php";
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
'7779995555', // call Bill at 7779995555
$twilioRequestUrl
);
//echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
//echo 'Error: ' . $e->getMessage();
}
?>
callJoe.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<!-- call and connect Joe at 9995557777 with Bill's caller ID-->
<Response>
<Pause length="2"/>
<Say>Please wait, I'm calling Joe.</Say>
<Dial callerId="7779995555">9995557777</Dial>
</Response>
Request http://somewebsite.xyz/callBill.php with your browser or with a click on a button.

Resources