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.
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.
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.
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).
I am trying to record an outbound call using twillio php code :
Once i execute the code in browser, call will go to destination number [+919999999999].
Issue :
but once it recieved, some default voice will play and call will be disconnected....
Requirement :
But i want both pepoles [ source & destination ] should speak and want to record that conversation....
<?php
require_once '/var/www/html/ecom1/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "account_sid";
$token = "auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+919999999999",
"+918888888888",
array(
"record" => True,
"url" => "http://twimlets.com/forward?PhoneNumber=%2B918888888888&"
)
);
print($call->sid);
?>
I am using Trail account....
Append the parameter "Record=true" when making a POST request to tell Twilio to record an outgoing call via the REST API.
By default, the recording will be single-channel (mono). For dual-channel recordings (two legs of the call each in separate stereo channels), append the parameter "RecordingChannels=dual".
// Get the PHP helper library from twilio.com/docs/php/install
require_once '/path/to/vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = '<account_sid value>';
$auth_token = '<auth_token value>';
$client = new Client($account_sid, $auth_token);
$calls = $client->accounts("<account id>")
->calls->create("<Valid To number>", "<Valid From number>", array(
'Method' => "POST",
'Record' => "true",
'RecordingChannels' => "dual"
));
Please Note :
When attempt to initiate an outbound phone call, ensure the URL you specified to handle the call should be valid URL.
If you specified an Application Sid for your outbound phone call, the application must have a valid VoiceUrl or the call will fail.
Hope this information helps you !
I have integrated Twilio and it works fine. Now I want to capture all the intermediate message statuses. I referred to Sending Messages.
My code looks like -
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 = '****************';
$token = '*****************';
$client = new Client($sid, $token);
// send message
$message = $client->messages->create(
// the number you'd like to send the message to
'+1xxxxxxxxx',
array(
'from' => '+1xxxxxxxx',
'body' => 'Test web hook message '.date('h:i'),
'statusCallback' => "https://xxxxxx/xxxx.php",
)
);
But the output/response returned to statusCallback is different as -
"{\"SmsSid\":\"SM72478c1ea61f467dbc33338123c0ad0\",\"SmsStatus\":\"sent\",\"MessageStatus\":\"sent\",\"To\":\"+1xxxxxxxx\",\"MessageSid\":\"SM72478c1ea612222dbc3b7858123c0ad0\",\"AccountSid\":\"ACb655a10c1c2222e4af158c5395d64beb\",\"From\":\"+1xxxxxxx\",\"ApiVersion\":\"2010-04-01\"}"
But I need the response as it is defined at Sending Messages
EDIT
If checked at Sending Messages, we can see the fields returned in the output are - account_sid, api_version, body, num_segments, num_media, date_created, date_sent, date_updated, direction, error_code, error_message, from, price, sid, status, to and uri.
But I receive fields as - SmsSid, SmsStatus, MessageStatus, To, MessageSid, AccountSid, From and ApiVersion.
For me, the fields - num_segments, date_sent, direction, error_code, error_message are important which I am not receiving. Do I need to use another API of TWILIO to retrieve this information ?
Why am I getting different response ?
Twilio developer evangelist here.
When you send a message and set a statusCallback URL the sending messages documentation says:
Twilio will POST the MessageSid along with the other standard request parameters as well as MessageStatus and ErrorCode.
The standard request parameters are:
MessageSid
SmsSid
AccountSid
MessagingServiceSid
From
To
Body
NumMedia
as well as some others specifically about media or geographic data based on the two numbers.
If you need to find out those other attributes of the message, you will need to look up the message using the REST API.
Let me know if that helps at all.
What does your code for your callback url script look like?
What you have is just an escaped JSON string, so to match what you see on the documentation you just have to do this:
$json = '{\"SmsSid\":\"SM72478c1ea61f467dbc33338123c0ad0\",\"SmsStatus\":\"sent\",\"MessageStatus\":\"sent\",\"To\":\"+1xxxxxxxx\",\"MessageSid\":\"SM72478c1ea612222dbc3b7858123c0ad0\",\"AccountSid\":\"ACb655a10c1c2222e4af158c5395d64beb\",\"From\":\"+1xxxxxxx\",\"ApiVersion\":\"2010-04-01\"}';
echo stripslashes($json);