I am currently developing a web app using PHP and using twilio, to SMS from it.
The issue I am facing is that, after reading the documentation I began using \n in my SMS body, to insert a new line, but on the receiver end, never is the new line appearing. (Tests took place on an Android 4.4.4 mobile)
Ricky from Twilio here. If you're sending a message with our PHP helper library we take care of encoding the newline for you:
$client = new Services_Twilio($account_sid, $auth_token);
$message = $client->account->messages->sendMessage(
'+15555555555', // From a Twilio number in your account
'+15555551234', // Text any number
"Hello monkey!\n how are you?"
);
If you're sending a message by making an HTTP request to our API directly you'll need to make sure the information you're sending is URL encoded. Thankfully PHP makes it pretty easy with the urlencode function:
$params =
'&From=' . urlencode("+15555555555") .
'&To=' . urlencode("+15555551234") .
'&Body=' . urlencode("Hello monkey!\n how are you?");
Hope that helps!
Related
Hi I just got approved on Twilio to use it's WhatsApp messaging service.
I have no problem when testing it in the sandbox, but I got trouble when I moved it into production environment.
Based on Twilio explanation, I have to start the conversation to WhatsApp customer using one of pre-approved templates. When the customer replied, we got 24 hours of window to send freeform messages.
I already did what's in the https://www.twilio.com/docs/sms/whatsapp/tutorial/send-whatsapp-notification-messages-templates but unfortunately the given example is actually for freeform message.
Here's the script:
<?php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "ACxxxxxxxxxxxxxxxxxxxxx";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create("whatsapp:+14155238886", // to
array(
"from" => "whatsapp:+15005550006",
"body" => "Hi Joe! Thanks for placing an order with us. We’ll let you know once your order has been processed and delivered. Your order number is O12235234"
)
);
print($message->sid);
Can anyone please help me with PHP script on how to send the WhatsApp message using this pre-approved template?
Alright, maybe some of you got here trying to ask the similar question and here's what I got after contacting the Twilio Support:
My WhatsApp API works now.
There's nothing wrong with my code nor their code (what's in their documentation https://www.twilio.com/docs/sms/whatsapp/tutorial/send-whatsapp-notification-messages-templates), actually they're using the same code to send either template message or freeform message.
Their Template Submission API to WhatsApp contains bug that creates mismatch between what we actually had in Twilio and what WhatsApp actually received. So that's why the first message I sent (even though I used the pre-approved template) always treated as freeform message thus it undelivered.
Twilio WhatsApp API is still in beta service, means bugs are expected. While it's still in beta, they recommend that we need to create templates as simple as possible and avoid formatting like bold, italics, strikethrough, etc also new lines (\n) being used in templates.
Thats all I can share and I hope you don't have problem just like I did.
Cheers!
Below is our code with predefined templates
$number = "+919XXXXXXXXX";
$to = "whatsapp:" . $number;
$from = "whatsapp:+1YYYYYYYYYY";
$msg = "Un rendez-vous de {{1}} pour {{2}} avec {{3}} et prévu le {{4}} a été créé.";
$accountSid = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
$authToken = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
$twilioClient = new Client($accountSid, $authToken);
$msg_data = array("from" => $from, "body" => $msg);
try {
$message = $twilioClient->messages->create($to, $msg_data);
$response = $message->sid ? $message->sid : '';
error_log("Twilio msg response : " . print_r($response, true));
} catch (TwilioException $e) {
error_log('Could not send whatsapp notification to ' . $number);
error_log('Could not send whatsapp TwilioException' . $e->getMessage());
}
One suggestion check for white space while creating message string. Even for a single white space they reject it.
I'm using sendgrid/sendgrid-php the repo on github to send transnational emails. Today I've updated the library, the new one uses the API v3 whereas I used v2 before. I've changed the code as per their examples, here is a dump of my SendGrid\Mail object:
The problem is that I'm constantly receiving the 400 BAD REQUEST error without any additional info:
What am I doing wrong? The mail object seems to be correct.
I'm trying to send the email the following way:
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$request_body = [creating the mail object];
$response = $sg->client->mail()->send()->post($request_body);
I was having the same issue and discovered all personalizations need to be strings with double quotes. In your example:
$md_email_id = (string)26921
More info https://github.com/sendgrid/sendgrid-php/issues/264
Sending twilio message from my api returns this error
Twilio sending message The requested resource /2010-04-01/Accounts//Messages.json was not found
this is how I send a message. anyone incountered a problem?
$this->client = new \Services_Twilio($sid, $token);
return $this->client->account->messages->sendMessage($from ,$to, $message);
this is the documentation I followed
https://www.twilio.com/docs/api/rest/sending-sms
How do I create Messages.json
I used this https://github.com/twilio/twilio-php on laravel 4.2
Another Twilio developer evangelist here. Think I might be able to help.
The error message you got was this:
The requested resource /2010-04-01/Accounts//Messages.json was not found
The key point is the URL, particularly the double slash in the middle. That is where your Account Sid should be, thus leading to the 404 error.
In this case, I would double check how you are setting $sid. Make sure it is assigned before you try to create the Twilio client object.
Hi Twilio developer evangelist here.
Sorry to hear you're having trouble with your code.
You seem to not have posted your complete code, so I don't see where you actually require the library.
I have however written an example which worked for me.
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'AC';
$token = '01';
$client = new \Services_Twilio($sid, $token);
$message = $client->account->messages->sendMessage(
'+44', // From a valid Twilio number
'+44', // Text this number
"Hello monkey!"
);
I have removed some sensitive data on the code, but if you replace that with your token and numbers, you should be able to send a text message correctly.
Also, just in case you have the contents of the 'Services' folder elsewhere, make sure your paths are correct.
You have entered messages when it should be sms_messages.
CHANGE
$this->client->account->messages->sendMessage($from ,$to, $message);
INTO
$this->client->account->sms_messages->create($from ,$to, $message);
Also make sure that you are running this code inside a class otherwise you need to remove the $this-> from your code and use a local varible.
A big part of the app I am working on is chat with Push Notifications. The whole system is working great except that when a user sends a message with an emoji in it, the receiver of this message will see this in their push note:
When the receiver taps on the push notification, the emoji's from the message are rendered fine in the app, like so:
The messages are stored on my server in a MySQL db, and a php script packages up the push note JSON and sends it to APNS.
Here is the part of my PHP script that packages up the JSON to send to APNS (borrowed mostly from a Ray Wenderlich tutorial):
function makePayload($senderName, $text)
{
$nameJson = $this->jsonEncode($senderName);
$nameJson = truncateUtf8($nameJson, 20);
// Convert and truncate the message text
$textJson = $this->jsonEncode($text);
$textJson = truncateUtf8($textJson, self::MAX_MESSAGE_LENGTH);
$payload = '{"aps":{"alert":"' . $nameJson . ': ' . $textJson . '","sound":"default"} . '}';
return $payload;
}
I'm pretty stumped. I'm not sure why iOS isn't parsing the "alert:" portion of my push note JSON correctly. Any ideas are much appreciated.
I want to convert text to voice message and send to users phone no
currently i am using making call api with TwiMLTM
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "xxx";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+343443", "+3444", "http://demo.twilio.com/docs/voice.xml", array(
"SendDigits" => "1234#",
"Method" => "GET"
));
echo $call->sid;
it is working,but this make call to user,but we need to voice message
Note : message is coming from textarea
Twilio Evangelist here.
This is a little tricky. Twilio allows you to make phone calls, so if the dialled number is answered by voicemail, you can leave a message. If it is answered by a human being, then you'll need to interact with them. It is possible to use the if_machine parameter when creating a call to detect an answering machine. However you cannot 'send' a voice mail as if sending an SMS or an email. You need to make the call, and decide how to handle that depending on who/what answers.
You could try sending the message as an SMS however, which would deliver the exact text to the user:
$sms = $client->account->messages->sendMessage("+343443", "+3444", $message_text);
However, if the number you are sending to is not able to receive an SMS, then you need to make the call and interact with either a human or answering machine.
Best of luck!