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.
Related
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.
i've successfully make a voice call with twilio like this:
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => "http://demo.twilio.com/docs/voice.xml")
);
the above code will call to the $toNumber with twilio demo message, when i try to use TwiML to create the dynamic message like this:
$response = new TwiML();
$response->say('Votre otp est 2234', ['voice' => 'woman', 'language' => 'fr']);
$call = $client->calls->create(
$toNumber, $fromNumber,
$response
);
i got an error like this:
[HTTP 400] Unable to create record: Url parameter is required. For
more information, see http://www.twilio.com/docs/api/twiml
Twilio developer evangelist here.
You can't send the TwiML to Twilio when you make a call like that. You need to send a URL that Twilio will send an HTTP request to when the call connects (like in the original example). So, to fix your code you should replace the demo URL with your own URL.
$call = $client->calls->create(
$toNumber, $fromNumber,
array("url" => $YOUR_URL_HERE)
);
That URL can be your own application, or something like a TwiML Bin or Twilio Function. Check out this article to see how to respond with TwiML to a request from Twilio.
I notice that you are building something for one time passwords too. You might be interested in checking out the Authy API that can implement and deliver OTP codes via calls, SMS and app.
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.
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};
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)