Twilio handling calls - twilio

I am trying to achive is whenever I receive an text message with certain keywords, I will get a call saying that I have received a text message and press 1 to talk to the person who sent the text message.
I have done it by creating three files, these are below
Twilio.php
This receives the post request on a text message and call me
$name = $_POST['name'];
$phone = $_POST['phone'];
$client = new Services_Twilio($AccountSid, $AuthToken);
try {
// make call
$call = $client->account->calls->create(
$caller,
$number,
array("url" => "http://somewebsite.net/twilio/twiml.php?phone=$phone&name=$name")
);
} catch (Exception $e) {
echo 'Error starting phone call: ' . $e->getMessage();
}
The next file if twiml.php which handle the call and ask me if I want to speak and press 1 to speak
twiml.php
$nm = $_GET['name'];
$ph = $_GET['phone'];
$name = "Deepak";
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Say>Hello <?php echo $name ?>.</Say>
<Gather numDigits="1" action="http://somewebsite.net/twilio/call.php?phone=<?php echo $ph ?>" method="POST">
<Say>You have a text message, press 1 to speak.</Say>
</Gather>
</Response>
The third file is called if I press 1 to speak and then it dials that number : below is the code:
Call.php
<?php
if($_REQUEST['Digits'] != '1') {
header("Location: twiml.php");
die;
}
$ph = $_GET['phone'];
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Dial>+1 <?php echo $ph ?></Dial>
</Response>
I am trying to achieve is to merge these three files into one so I need not to make a Post request and I could handle the whole call in a single file or at least I can merge twiml.php and call.php
Is there a way to combine these?

Twilio developer evangelist here.
You could put it all into one file and use the URI that was requested ($_SERVER['REQUEST_URI']) to switch on the code you then run. Or, it might be more interesting to try a small PHP framework that abstracts that away. Something like Slim or Silex might work for you.
I would ask why you need to run this from one file when your feature looks like it works?

Related

Twilio PHP Web Hooks?

I have a webhook in Twilio using Studio and most of it is working fine right up to getting the response back from the web server.
Looking in the Twilio log I can see the response coming back like so:
"widgets": {
"ReplyWithJobDetail": {},
"AskServer": {
"status_code": 200,
"content_type": "text/xml",
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Message>Thank you for accepting this position.<From>From=MCC</From><Body>Body=Here is the body content</Body></Message></Response>"
},
But I am getting an error in Twilio stating:
Body:
Failure sending message: Message body is required.
Which is odd because the body is obviously there.
My webhook is:
function trigger_receive_sms($from) {
header ('Content-Type:text/xml');
echo ('<?xml version="1.0" encoding="UTF-8"?>');
echo ('<Response>');
echo ('<Message>Thank you for accepting this position.');
echo ('<From>From=MCC</From>');
echo ('<Body>Body=Here is the body content</Body>');
echo ('</Message>');
echo ('</Response>');
die();
}
This is in WordPress and also have the Twilio.Core plugin installed.
I have been trying to get this working for 4 days...Where am I going wrong?
Turns out I just needed to convert the output to JSON...
Did that with the following:
function trigger_receive_sms($from) {
header ('Content-Type:application/json');
$message = '<?xml version="1.0" encoding="UTF-8"?>';
$message .= '<Response>';
$message .= '<Message>Thank you for accepting this position.';
$message .= '</Message>';
$message .= '</Response>';
$simplexml = simplexml_load_string($message);
$json = json_encode($simplexml);
return $json;
die();
}
If I didn't get your question right I apologize. I'm not clear how your setup is, what your Studio flow contains, what's with WordPress, etc...
Here is the TwiML to respond to an incoming message:
<Response>
<Message>Hello from Twilio!</Message>
</Response>
There is no need for From because it will be sent from your Twilio's number, and the body of the message is right between the <Messsage> tags, so there is also no need for <Body> tags.
As such, try with:
function trigger_receive_sms($from) {
header ('Content-Type:text/xml');
echo ('<?xml version="1.0" encoding="UTF-8"?>');
echo ('<Response>');
echo ('<Message>Thank you for accepting this position.');
echo ('</Message>');
echo ('</Response>');
die();
}
Let us know how it goes. If I got it wrong, please give more details, I'll try to understand better.

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.

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.

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