Twilio REST api PHP calling a user and reading SMS data
First I have a sequential dialing app which will ca开发者_开发知识库ll an array of phone numbers ino rder until one of them picks up.
Now what I need to do but am stuck at is when someone SMS's Twilio, the data or information in the body of the SMS should be read to the person that picks up the phone call using Say verb. I am not sure how I can get the SMS body and have it read in my app. Any ideas on that?
<?php
print_r(error_reporting(-1));
session_start();
require 'Services/Twilio.php';
$version = "2010-04-01";
$arr = array('416..','647...');
$sid = '...';
$token = '..';
$from = '..';
$callback = 'site.com/Twilio/twilio-twilio-php-a0e9f92/dial.php';
$client = new Services_Twilio($sid, $token, $version);
if (isset($_REQUEST['index'])) {
$index = $_REQUEST['index'];
} else {
$index = 0;
}
$next = $index + 1;
if ($index==0 && $_REQUEST['CallStatus']=='completed')
{
//Read SMS to the called party
}
else
{
if ($index>=1 && $_REQUEST['CallStatus']=='completed')
{
//Read SMS
}
else
{
try {
$call = $client->account->calls->create(
$from,
$arr[$index],
'http://site.com/Twilio/twilio-twilio-php-a0e9f92/dial.php?index=' . $next,
array('Timeout' => 5,'StatusCallback' => $callback)
);
} catch (Exception $e) {
var_dump($e);
}
}
}
This can actually be accomplished with very similar code to what you already have, just simplified and separated into a PHP file for handling the incoming SMS and then a callback PHP file for handling the phone call that you make.
First, we'll create a file (e.g. smshandler.php) and point our phone number to it in the Twilio dashboard. In it, we will create a call based on the phone number of the SMS user.
For example:
<?php
$number = $_REQUEST['From'];
$body = $_REQUEST['Body'];
$call = $client->account->calls->create(
$from,
$number,
'http://path/to/your/callback.php?body=' . $body
);
?>
Then we'll use create another file (e.g. callback.php) and pass the body of the message to it and then render that body in TwiML to read the contents back to the user we called.
For example:
<?php
$message = $_REQUEST['body'];
?>
<Response>
<Say><?php echo message; ?></Say>
</Response>
精彩评论