Services_Twilio_HttpException for sending SMS using twilio - twilio

I am trying to run twilio using WAMP:
I just picked up the code from the tutorial:
https://www.twilio.com/docs/quickstart/php/sms/sending-via-rest
<?php
/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* - Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* - Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
* - Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require "twilio-php-latest/Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$AuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+1412xxxxxxx" => "Friend"
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"33x-xxx-xxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, Monkey Party at 6PM. Bring Bananas! From Twilio team "
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
The error I am getting is Fatal error: Uncaught exception 'Services_Twilio_HttpException' with message 'The OpenSSL extension is required but not currently enabled. For more information, see http://php.net/manual/en/book.openssl.php' in D:\Ankita\wamp\www\twilio-php-latest\Services\Twilio.php on line 64
Can anyone tell me why this is happening?

Ok i managed to solve it by uncommenting the line extension=php_openssl.dll inside php.ini file

Related

Twilio Not doing anything when receiving SMS

I'm creating a sample application that will post alerts to the website in the event of a hurricane or service outage. I'm not using Laravel.
I set the URL of the page in my account settings. The first time I sent a message I received a HTTP error that it had timed out without being given a reponse. I edited the XML and tried again.
I'm not getting anything in the database and I'm not getting the response. I also wrote a sample page that posts a value to see if it would work and it did. It posted it into the database and showed correctly formatted XML.
<?php
$response = 'This number cannot handle automated replies...';
$twiml1 = '<response><sms>';
$twiml2 = '</sms></response>';
require_once '../settings/db.php';
if (isset($_POST['body'])) {
$body = strip_tags($_POST['body']);
$sql = "INSERT INTO alerts (message) VALUES ('$body')";
$result = $db->query($sql);
if ($result) {
$response = 'Thanks. Your message was posted on the website.';
} else {
$response = 'There was a query error.';
}
}
header('Content-type: application/xml');
echo $twiml1;
echo $response;
echo $twiml2;
Twilio developer evangelist here.
Parameters that are sent via webhooks from Twilio are case sensitive and start with a capital letter. The text for an incoming message is sent as the Body parameter so checking for $_POST['body'] won't work.
I'd update your conditional to:
if (isset($_POST['Body'])) {
$body = strip_tags($_POST['Body']);
// The rest
}
Also, just to note, the <Sms> element has been deprecated. I'd use the <Message> element instead. The tags are case sensitive too, so I'd update the TwiML section to this:
$twiml1 = '<Response><Message>';
$twiml2 = '</Message></Response>';
Let me know if that helps at all.

Dial a Number Twilio Number to Trigger TWILM Bin

We are attempting to create a workflow that will ultimately connect a lead from a contact form, with a business owner.
The workflow is as follows:
1) Lead fills in contact form
2) Using Stamplay integration with Unbounce, the lead receives a text asking them if they wanted to be contacted "Now", or "Later".
Let's go with lead says "Now"
3) Lead says "Now", which will access a webhook URL to decide on what to do next.
In this particular case, saying "Now", will trigger a TWIML bin to dial the business owner. If the business owner doesn't pick up/busy, then we send a text to the lead asking them to send a follow-up text with a 'name' and 'date/time'.
4) The lead replies with a text with this information, and then both the business owner and lead receive separate notifications about the appointment.
I have been able to successfully go through this whole workflow when a user directly dials the Twilio number (not programatically with keywords yet, which is where I need help).
When a call comes in -> TWIML bin
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Pause length="4"/>
<Say>Please hold, while I connect your call.</Say>
<Pause length="4"/>
<Dial timeout="10"> business owner number </Dial>
<Pause length="4"/>
<Sms>I am currently unavailable. If you'd like me to get in touch, pls reply back with your name, and a time that would work best for you. Thanks, Adam</Sms>
</Response>
When a SMS is received -> webhook URL
<?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'xyz';
$token = 'xyz';
$client = new Client($sid, $token);
$number = $_POST['From'];
$body = $_POST['Body'];
//Sends a message to the owner
$sms = $client->account->messages->create(
// Cell of owner
'12345',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => "78900",
// Lead's reply sent to owner asNotification
'body' => "Hi <name>. You have a new lead. The information for this lead is: $body. You can contact them at $number"
)
);
//Sends a message to the lead
$sms = $client->account->messages->create(
// Cell of Lead
$number,
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => "78900",
// Notification Message sent to Lead
'body' => "This is a confirmation that I have received your information, and will be in contact with you soon. Thanks, <name>."
)
);
Where I am encountering issues is having the lead text "Now", to trigger a phone call between the business owner and lead.
This is the code that I have been attempting to use, except that I have been receiving 11200- HTTP retrieval failure non-stop. I have also attempted to use $client->account->calls->create, as that's what I used to successfully send messages.
// Read TwiML at this URL when a call connects (attempt to connect to owner)
$call = $client->calls->create(
'lead-number', // Call this number
'78900', // From a valid Twilio number
array(
'url' => TWIML Bin of Interest
)
);
Anyone have any idea what I could do?
Check out the example of creating a dynamic response:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Twiml;
$response = new Twiml;
$body = $_REQUEST['Body'];
if( $body == 'hello' ){
$response->message('Hi!');
}else if( $body == 'bye' ){
$response->message('Goodbye');
}
print $response;
In your case you'll modify for if "now" is in the $body you can create the call which your code looks fine for.
$call = $client->calls->create(
"+1415XXXXXXX", "+1415XXXXXXX",
array("url" => "link_to_twiml_bin")
);
In regards to the 11200 HTTP retrieval error, take a look at the possible solutions here especially:
Make sure your web server allows HTTP POST requests to static
resources (if the URL refers to .xml or .html files)

Twilio blacklist rule fatal error

I am using twilio to send bulk sms messages. Let's say some customer decided that they don't want to receive messages anymore so they reply with "stop" and that will add them to the black list. I am hard coding the phone numbers because I am still testing on my own cell phones. I noticed that when I do not remove the numbers on the black list from my code, I am getting an error message and my script stops at that point.
In the future, I will probably be using numbers stored in a database or a file. In that case, how do I overcome this problem if it happened. Basically what I want to do is: If a number is in the black list, move on to the next number and avoid that error using an exception or something.
The error message and code is below.
Thanks,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Send SMS</title>
<?php
/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* 1. Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* 2. Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
*
* 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/
// Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php,
// following the instructions to install it with Composer.
//require_once "vendor/autoload.php";
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;
// Step 2: set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "something";
$AuthToken = "something";
// Step 3: instantiate a new Twilio Rest Client
$client = new Client($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+17570123456" => "Chris",
"+17571234568" => "Hussam"
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->create(
// the number we are sending to - Any phone number
$number,
array(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased
'from' => "+184444444444",
// the sms body
'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
)
);
// Display a confirmation message on the screen
echo "Sent message to $name.\n";
}
?>
( ! ) Fatal error: Uncaught exception 'Twilio\Exceptions\RestException' with message '[HTTP 400] Unable to create record: The message From/To pair violates a blacklist rule.' in C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php on line 86 ( ! ) Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: The message From/To pair violates a blacklist rule. in C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php on line 86 Call Stack
Time Memory Function Location 1 0.0000 239280 {main}( ) ...\send.php:0 2 0.0156 799016 Twilio\Rest\Api\V2010\Account\MessageList->create( ) ...\send.php:56 3 0.0156 814688 Twilio\Version->create( ) ...\MessageList.php:63
Twilio developer evangelist here.
You need to catch the exception that is thrown from the request to send a message to the blacklisted number. You can do so with try and catch like this:
foreach ($people as $number => $name) {
try {
$sms = $client->account->messages->create(
$number,
array(
'from' => "+18443949780",
'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
)
);
echo "Sent message to $name.\n";
} catch (\Twilio\Exceptions\RestException $e) {
echo "Couldn't send message to $number\n";
}
}
When you hook this up to a database, you'll want to use the catch to update a field to mark the number as blocked so that you don't try to send to it again.
Let me know if that helps at all.
This worked for me with Laravel 5. Notice the use of \Twilio\Exceptions\RestException.
try {
$sms = $client->account->messages->create(
$number,
array(
'from' => "+16136543180",
'body' => "Hey $name, Are you still mad at us about your cat!"
)
);
echo "Sent message to $name.\n";
} catch (\Twilio\Exceptions\RestException $e) {
if ($e->getCode() == 20404) {
//this will be false condition
dd('False Result 404');
} else {
//some other exception code
dd($e->getMessage());
}
}

How can I record incoming calls on Twilio?

I have a website that uses Twilio to allow people to use our temporary numbers to receive SMS messages received during verification processes etc. It is becomming more common that companies are switching to audio verification instead so I want to start recording all calls received and displaying them in the existing HTML table using the HTML5 <audio> tag.
Here is the existing code:
<tbody>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Get the PHP helper library from twilio.com/docs/php/install
require_once('twilio/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);
$messages = $client->account->messages->getIterator(0, 50, array(
'To' => $_SERVER['QUERY_STRING'] // this is the number
));
foreach ($messages as $message) {
echo "<tr><td>" . $message->from . "</td><td>" . $message->date_sent . "</td><td>" . $message->body . "</td></tr>";
}
?>
</tbody>
</table>
How can I build in to that the recorded calls received? I want to keep it in date/time order within the eixsting SMS messages, if that makes sense.
Twilio developer evangelist here.
You can absolutely record calls with Twilio.
When you create a call, you just need to include the parameter Record=true in the REST API request to create a call. Then, if you include a statusCallback parameter that points to a URL on your server, then you will receive a webhook to that URL when the call is complete that includes a link to the recording.
You can also fetch the latest recordings from the API. You can get recordings in wav or mp3 format, which you can then use in the HTML <audio> element.
I'm not sure how you have set up your date ordered SMS table, but hopefully this helps. Let me know if there is anything else I can help with.

twilio Uncaught exception error

Twilio newbie using test account. I followed the instructions listed here for installing Twilio php:
https://www.twilio.com/docs/quickstart/php/sms
Because I was getting a certificate error, my host provider suggested I changed the CURLOPT_SSL_VERIFYPEER => false (from true). But now I'm getting this error. How to fix?:
Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The requested resource /2010-04-01/Accounts//Messages.json was not found' in
require "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACbxxxxxxx";
$AuthToken = "0cfxxxxxxx";
// Step 3: instantiate a new Twilio Rest Client
//$client = new Services_Twilio($AccountSid, $AuthToken);
$http = new Services_Twilio_TinyHttp(
'https://api.twilio.com',
array('curlopts' => array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
))
);
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$people = array(
"+13121111111" => "Curious George",
// "+14158675311" => "Virgil",
);
// Step 5: Loop over all our friends. $number is a phone number above, and
// $name is the name next to it
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"929-xxx-xxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, Monkey Party at 6PM. Bring Bananas!"
);
// Display a confirmation message on the screen
echo "Sent message to $name";
}
Twilio developer evangelist here.
Firstly, you should never set CURLOPT_SSL_VERIFYPEER to false in production. From the curl manual:
WARNING: disabling verification of the certificate allows bad guys to man-in-the-middle the communication without you knowing it. Disabling verification makes the communication insecure. Just having encryption on a transfer is not enough as you cannot be sure that you are communicating with the correct end-point.
For the sake of getting you going with Twilio though your issue is in the variable names you are using.
At the top of the file you set:
$AccountSid = "ACbxxxxxxx";
$AuthToken = "0cfxxxxxxx";
But when you create a Twilio you use $sid and $token.
$client = new Services_Twilio($sid, $token, "2010-04-01", $http);
If you change those to $AccountSid and $AuthToken it should work as you expected.

Resources