Dialing Fallback with Twilo - twilio

I am working on my inbound TwiML. At one point I want to attempt to forward a call to my cell phone number.
So I got 5555555555 but I want to redirect to a different TwiML script if there is no answer or no human answer. For example if I do not answer and Twilo gets my Cell VM, I would instead want to have Twilo try someone else in another TwiML script rather than let them leave a message on my personal VM.
I know I can set it up so I have to press 1 to connect the call, then I am assuming if I don't press 1 it would continue processing the TwiML after is this possible without requiring me to press 1?

Joel from Twilio here.
The best way to do what you want is to use the call screening process that you describe ("press 1 to connect the call"). This is because automatically detecting voice mail is a very error prone process.
We have an in-depth Call Screening HowTo on how to do this. Here's the overview:
Create a file called "attempt_call.php" on your web server that contains the following code (pay particular attention to the $numbers array on line 4):
<?php
// Set the numbers to call
$numbers = array("<number to call 1>", "<number to call 2>", "<number to call n>");
$number_index = isset($_REQUEST['number_index']) ? $_REQUEST['number_index'] : "0";
$DialCallStatus = isset($_REQUEST['DialCallStatus']) ? $_REQUEST['DialCallStatus'] : "";
header("content-type: text/xml");
// Check the status of the call and
// that there is a valid number to call
if($DialCallStatus!="completed" && $number_index<count($numbers)){
?>
<Response>
<Dial action="attempt_call.php?number_index=<?php echo $number_index+1 ?>">
<Number url="screen_for_machine.xml">
<?php echo $numbers[$number_index] ?>
</Number>
</Dial>
</Response>
<?php
} else {
?>
<Response>
<Hangup/>
</Response>
<?php
}
?>
Next, in the same location as the "attempt_call.php" file, create a file called "screen_for_machine.xml" with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="complete_call.xml">
<Say>Press any key to accept this call</Say>
</Gather>
<Hangup/>
</Response>
Then, in the same location as the "attempt_call.php" file, create a file called "complete_call.xml" that contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="complete_call.xml">
<Say>Press any key to accept this call</Say>
</Gather>
<Hangup/>
</Response>
Finally, set the "Voice Request URL" on your Twilio number to the full publicly available URL for the "attempt_call.php" file you created.
For more details, all of this code above is also available with mode in-depth explanation on the Call Screening HowTo which is available on our website.

Related

Twiml discarding voice, loop in SAY verb inside GATHER

I have an API which first creates a call to a number using the C# wrapper, lets say the receiver is +1000000001
var call = CallResource.Create(new PhoneNumber("+1000000001"),
new PhoneNumber("MYVERIFIEDNUMBER"),
url: new Uri("https://api.com/answered"),
method: HttpMethod.Get,
client: _client,
sendDigits: ""
);
When answered the TWIML returned from https://api.com/answered is
<?xml version="1.0" encoding="utf-8"?>
<Response>
<Gather action="https://api.com/connect/6AE3045C0D024F1896BF7ECFCB2FC40A" method="GET">
<Say voice="alice" loop="0" language="en">Press any key to connect to John Doe, , </Say>
</Gather>
</Response>
This should result in an infinite loop in the voice of "alice" for the SAY verb being repeated to the receiver at +1000000001 but it is a male robotic voice and it only repeats once then drops the call. This is first part of the issue.
The second part is the GATHER verb does nothing. I should be able to press a touch tone phone and have the url https://api.com/connect/6AE3045C0D024F1896BF7ECFCB2FC40A return
<?xml version="1.0" encoding="utf-8"?>
<Response>
<Dial>client:6AE3045C0D024F1896BF7ECFCB2FC40A</Dial>
<Hangup></Hangup>
</Response>
which it does on the GET request but I can never get to it because of the GATHER issue
The third part is does this look correct to dial a client app?
<Dial>client:6AE3045C0D024F1896BF7ECFCB2FC40A</Dial>
Thanks for any advice
Looks like Alice defaults to en-US so you can leave the language attribute off. Also, can you make sure you are returning TwiML with the right MIME type, https://www.twilio.com/docs/voice/twiml#twilio-understands-mime-types.
Client is used inorrectly, refer to the TwiML syntax here, https://www.twilio.com/docs/voice/client/twiml.
Let me know if that addresses the issue.

Twilio - create conference if first cal is answered

I'm trying to find the best way how to achieve these:
Call a number, if answered call a second number and join both in a conference
My initial idea is to call number like in below script
$call = $client->calls->create(
$from, $participant,
array("url" => "http://myhost.com/conference.php")
);
The conference.php then would be something like:
<Response>
<Dial>
<Number>+1XXXXXXXXXX</Number>
<Conference>Room 1234</Conference>
</Dial>
</Response>
Do you think this will work? or what's the alternative?
Thanks!
Twilio developer evangelist here.
To do this, you actually want to use a combination of the REST API, to make the second call, and TwiML in your response.
So, your conference.php would look something a little like this:
<?
$call = $client->calls->create(
$from,
$participant,
array("url" => "http://myhost.com/conference2.php")
);
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="utf-8" ?>';
?>
<Response>
<Dial>
<Conference>Room 1234</Conference>
</Dial>
</Response>
And then you can make conference2.php just return the TwiML
<Response>
<Dial>
<Conference>Room 1234</Conference>
</Dial>
</Response>
Let me know if that helps at all.

Twilio Twilio/Rest/Calls.php error

whenever I am trying to make a conference call it says application error and I get an error in error log as :
PHP Warning: strlen() expects parameter 1 to be string, array given in /home/aan/public_html/twilio/twilio-php-4.11.0/Services/Twilio/Rest/Calls.php on line 16
Here is the code
<?php
require("twilio-php-4.11.0/Services/Twilio/Twiml.php");
if($_REQUEST['Digits'] != '1') {
header("Location: twiml.php");
die;
}
$MODERATOR = $_GET['phone'];
$response = new Services_Twilio_Twiml();
$dial = $response->dial($MODERATOR);
$dial->conference('My conference', array(
'startConferenceOnEnter' => True
));
I have already made the call and gathered the digit , but when I dial second number and try to make these as conference I get this error
I usually use TwiML for making a conference call, It's simple to implement.
$my_conference = "My Conference";
$statusCallbackUrl = "https://example.net/Welcome/conference_control"; // call back url
<Response>
<Dial>
<Conference beep="false" statusCallback="<?php echo $statusCallbackUrl; ?>"
statusCallbackEvent="start end join leave mute hold" endConferenceOnExit="true"
startConferenceOnEnter="true">
<?php echo $my_conference; ?>
</Conference>
</Dial>
</Response>
Hopefully, it will help you.
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Dial><?php echo $phone ?></Dial>
</Response>
The snippet here was OP's fix for encountering an error using twilio-php library.

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.

What is the best way to add CNAM (Caller ID Name) to a SMS processed through twilio

My twilio app is just like this sample app
<?php
header('Content-Type: text/html');
?>
<Response>
<Message to="<?=$_REQUEST['PhoneNumber']?>">
<?=htmlspecialchars(substr($_REQUEST['From'] . ": " . $_REQUEST['Body'], 0, 160))?>
</Message>
</Response>
I would like to add the CNAM (Caller ID name) to the message when I send it on to the destination, but twilio support says:
We unfortunately do not expose or provide caller ID name in the API response.
Is there any work around or other way to do this?
Try this :
<?php
header("content-type: text/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Message from="<?php echo $_REQUEST["To"]; ?>" to="+1<?php echo $_REQUEST['FrwdNum']; ?>">
<?php echo htmlspecialchars(substr(substr($_REQUEST['From'],0,10) . ":" . $_REQUEST['Body'], 0, 160)); ?>
</Message>
</Response>
If your script is www.example.com/answer.php, then give below url as sms url for your twilio number
www.example.com/answer.php?FrwdNum=9876543210
You can also use Twilio Lookup to get the CNAM of a number.
Using the REST API, you would specify the CallerName property in your GET request:
https://www.twilio.com/docs/api/lookups#lookups-caller-name
And the output would look like:
{
"caller_name": "Caller Name",
...
...
}
Caller ID/CNAM is only displayed on landlines. Cell phones do NOT support CNAM because carriers defer to your personal contact list to populate names on inbound calls. The terminating carrier (the carrier for the number you are calling) is responsible for displaying CNAM.

Resources