RestException is not a valid phone number on Twilio trial account - twilio

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.

Related

Unable to send Programmable sms to Philippines numbers (Twilio)

am finding it difficult to send an sms to a Philippines number using the example from the Twilio SMS PHP Quickstart documentation...
here's my code
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;
$account_sid = 'XXXXXXXXXXXX'; /////my account_sid
$auth_token = 'XXXXXXXXXX'; ////my auth_token
$client = new Client($account_sid, $auth_token);
$twilio_number = "+XXXXXXXXXXX";////// my twilio_number
$number = "XXXXXXXXXX"; /////Philippines number 12 digits
$client->messages->create(
// Where to send a text message (your cell phone?)
'+'.$number,
array(
'from' => $twilio_number,
'body' => 'My Twilio Message'
)
);
echo json_encode('done');
it works with other countries like the US, Canada etc. At least I've tried those and they work. Anybody with an idea please ?
After contacting Twilio support, they helped me realize that I had not enabled geo permission to send SMS towards these countries, rookie mistake ...
Any one having this issue, you can click here to enable this setting.

How to use unicodes or special characters in SMS (Twilio, Plivo)

In a case i have to send SMS with two whitespaces at the beginning of the message content as like below
setparam 3003:70eac;
This is for some device configuration. But while receiving the SMS it is
setparam 3003:70eac;
The whitespaces are gone.
This is how i send the SMS with php helper library
$client = new Client($account_sid, $auth_token);
$client->messages->create(
'<to_number>',
array(
'from' => '<from_number>',
'body' => ' setparam 3003:70eac;'
)
);
I had also tried by sending as encoded like below
%20%20setparam%203003%3A70eac%3B
This results without parsing
%20%20setparam%203003%3A70eac%3B
Any help will be great support to me.
We couldn't use whitspaces normaly while sending SMS in Twilio or Plivo, as those were trimmed automatically. But can be send by escaping the unicode value of whitspace in the message.
Examples:
Python
from twilio.rest import Client
# Your Account SID from twilio.com/console
account_sid = "<twilio_sid>"
# Your Auth Token from twilio.com/console
auth_token = "<twilio_token>"
client = Client(account_sid, auth_token)
message = client.messages.create(
to="<to_number>",
from_="<from_number>",
body="\U00002001\U00002001Whitspace at front")
print(message.sid)
JSON
{
"src" : "<from_number>",
"dst" : "<to_number>",
"text" : "\u0020\u0020Whitspace at front",
"type" : "sms",
"method" : "POST"
}
PHP
<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/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 = 'ACfe1c3f0e87c51710e95e842f2e71922b';
$token = 'your_auth_token';
$client = new Client($sid, $token);
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
'+15558675309',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => '+15017250604',
// the body of the text message you'd like to send
'body' => "\020\020Whitspace at front"
)
);
You can see the message body is denoted with double quotes(") . That is important in PHP. Denoting with single quote(') won't work. The different quote usages in PHP and Double quoted strings.
More reference links for PHP.
String Functions
Converting to and from ASCII
Result:
' Whitspace at front'
In twilio you will have to use Unicode 'Punctuation Space' which is '\2008'. '\0020' wont work.

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 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.

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