twilio Uncaught exception error - twilio

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.

Related

RestException is not a valid phone number on Twilio trial account

I use twilio/sdk package, with the following code.
$sid = "AC3###############";
$token = "100################";
$from = $to = "+628********99";
$otp = rand(1111111, 9999999);
$client = new Twilio\Rest\Client($sid, $token);
$message = $client
->messages
->create($to, [
'from' => $to,
'body' => 'OTP code : ' . $otp
])
This is the error that I got
Twilio\Exceptions\RestException [HTTP 400] Unable to create record: The 'To' number +628********99 is not a valid phone number.
What did I do wrong ?
How can I use my trial account to send trial sms?
FYI the number I used is a verified number in Twilio and country origin is Indonesia +62.
The line $token = '100################; is missing it's closing single quote. Could that be the reason?
PS: It's hard to build SMS OTP right because of fraud schemes like SMS Traffic Pumping Fraud. Twilio Verify has built-in protection against these risks.

How to switch an ongoing Twilio phone call to a <Pay> connector

I would like to use the < Pay> connector optionally while in an ongoing phone call. I cannot find out how to do trigger a new resource during an ongoing phone call.
You can modify an "in progress" call by passing new TwiML (XML) to execute which could contain your "<Pay>".
You must provide
the ID of the call you want to modify (the "CallSid" "CAe1644a7eed5088b159577c5802d8be38")
and an URL where Twilio will find the instructions (the "Url" "http://demo.twilio.com/docs/voice.xml")
I don't know what language you're using but in PHP with Twilio's library the code would look someting like this:
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACc0966dd96e4d55d26ae72df4d6dc3494";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls("CAe1644a7eed5088b159577c5802d8be38")
->update(array(
"method" => "POST",
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
print($call->to);
You can read more about this here
(https://www.twilio.com/docs/voice/tutorials/how-to-modify-calls-in-progress).

How to record-from-answer-dual with Twilio's object oriented interface?

I understand how to enable the 'record-from-answer-dual' with the XML style command set, but I'm not finding any way to accomplish the same thing with the more object-oriented style code, such as:
<?php
require_once 'twilio-php-master/Twilio/autoload.php';
$response = new Twilio\Twiml();
$sayMsg = 'Attention! Attention! The network operations
center has opened a ticket concerning an ATMS failure in the Eastern
region. The ticket number is ECHO,1,5,7,4. I repeat, the ticket number is
ECHO,1,5,7,4. Thank you.';
$response->record();
$response->say($sayMsg, array('voice' => 'alice'));
$response->hangup();
echo $response;
I've tried adding it to the new line, and the record line as an array-style entry, similar to enabling the Alice voice. No dice.
I want to record the entire call, from answer, including the message spoken by Twilio.
Thanks for any information anyone can provide!
Twilio developer evangelist here.
<Record> is used to record messages from a call, not to record the TwiML that follows. It's more useful if you are building a messaging or voicemail system for voice.
Given that your message sounds like some kind of announcement, I am guessing that you are generating this call from the REST API. In that case, you can use the Record parameter when you place the call and the entire call will be recorded. In PHP, that would be something like this:
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/console
$sid = "your_account_sid";
$token = "your_auth_token";
$client = new Client($sid, $token);
$call = $client->calls->create(
$to, $from,
array(
"url" => $url,
"record" => true
)
);
Check out the documentation on the parameters you can use when making a call, including Record here.
Let me know if that helps at all.
Update from Jeffrey's comment
This is the Perl version, using the unofficial Twilio Perl module:
use WWW::Twilio::API;
my ($twilaccountsid, $twilauthtoken, $fromnum, $tonum, $twiml_uri) = #_;
my $twilio = WWW::Twilio::API->new(AccountSid => $twilaccountsid, AuthToken => $twilauthtoken);
my $response = $twilio->POST( 'Calls', From => $fromnum, To => $tonum, Record => 'true', Url => $twiml_uri);
return $response->{content};

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());
}
}

Twilio - Carrier Lookup

How do I access the phone carrier with Twilio's carrier lookup?
Here is some of my sample code:
require_once('twilio/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "--------------";
$token = "------------------";
$client = new Lookups_Services_Twilio($sid, $token);
$number = $client->phone_numbers->get("5555555555", array("CountryCode" => "US", "Type" => "carrier"));
//How do I access the carrier here?
echo $number->phone_number;
Twilio developer evangelist here.
When you call for the carrier details, they are all returned as an object on the number called carrier. You can see this in the example response on the Twilio Lookup page. So, with your code:
$number = $client->phone_numbers->get("5555555555", array("CountryCode" => "US", "Type" => "carrier"));
echo $number->carrier->name;
echo $number->carrier->type;
With Twilio 5.x SDK, things changed very slightly:
use Twilio\Rest\Client as Twilio;
$client = new Twilio("sid", "token");
$response = $client->lookups->phoneNumbers("+15551234567")->fetch(["type" => "carrier"]);
echo $response->carrier["type"] . "\r\n";
echo $response->carrier["name"];
See https://www.twilio.com/docs/api/lookups for the complete current documentation.
When fetching "type" => "caller-name", the response can be unwound with:
echo $response->callerName["caller_name"];
camelCase, hyphen and dash all in one use case!

Resources