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.
Related
Using the Twilio API, I've got my PHP functioning to send to one phone number, and can successfully send. We're looking to send to multiple numbers from one request and to do so, I've set up an array of numbers to iterate through, however, I keep getting a 500 error when I attempt to send the message by hitting the URL. Below is the file I'm working with.
Running PHP 7.2 on a Linux server. I'm running CentOS 7.7 and Apache 2.4.43 if that matters at all.
// 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/src/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 = 'XXXXXXXX';
$token = 'XXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = “This is a test of sending the text message to multiple phone numbers.”
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}
);
I'm not super familiar with PHP as I'm mostly in marketing, but I'm deputizing as developer in these crazy times because I know just enough to be dangerous. I'm thinking it is something with the foreach section, as that's the only piece that has changed from the single send.
Any help is appreciated!
Figured it out thanks to the help from #LuisE! I went through and figured out where I was missing the semicolons after the array, the $bodyTxt, and the $message = $twilio->messages.
// 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/src/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 = 'XXXXXXXXXX';
$token = 'XXXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = 'This is a test of sending the text message to multiple phone numbers.';
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages;
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}
I am sending sms to mobile using twilio sms service in PHP http://s.test.com?v=ccc but in sms it only show http://s.test.com as a hypelink missing ?v=ccc part . how can I make it show this whole http://s.test.com?v=ccc as a hyperlink in sms ?
This is the code I am using to send sms :
function sendText($number, $id, $product_domain)
{
$short_url = 'http://s.'. $product_domain .'.com?v='.$this->toBase($id);
$client = new Services_Twilio(TWILIOSID, TWILIOTOKEN);
$client->account->messages->sendMessage("123-4567-9870", $number,
"Your Scope is complete ".$short_url." Customer Service 877.697.2673 . To disable this service reply with \"stop\"");
}
This $short_url will be generated as http://s.test.com?v=ccc
Changing http://s.test.com?v=ccc to http://s.test.com/?v=ccc fixed the issue for me.
I'm trying to use the WhatsApi Official library to send a message via WhatsApp from a php file. I've moved in my Apache web server the library, in a folder call test, like this:
The file whatsapp.php is this one:
<?php
require_once './src/whatsprot.class.php';
$username = "1XXXXXXXXX";
$password = "password";
$w = new WhatsProt($username, "0", "My Nickname", true); //Name your application by replacing “WhatsApp Messaging”
$w->connect();
$w->loginWithPassword($password);
$target = '1xxxxxxxxx'; //Target Phone,reciever phone
$message = 'This is my messagge';
$w->SendPresenceSubscription($target); //Let us first send presence to user
$w->sendMessage($target,$message ); // Send Message
echo "Message Sent Successfully";
?>
I'm facing some problem with the library new WhatsProt(), which blocks all the code (may be sockets ?).
So my question is, how can I fix this problem ? If no, are there any other solution to send message from a pho script ?
You can use below script to send message from whatsapp in PHP.
https://github.com/venomous0x/WhatsAPI/tree/master/examples
Configure the source code in Apache and run examples/whatsapp.php file.
You have change below configurations.
//simple password to view this script
$config['webpassword'] = 'MakeUpPassword';
and
$config['YOURNAME'] = array(
'id' => 'e807f1fcf82d132f9bb018ca6738a19f',
'fromNumber' => '441234567890',
'nick' => "YOURNICKNAME",
'waPassword' => "EsdfsawS+/ffdskjsdhwebdgxbs=",
'email' => 'testemail#gmail.com',
'emailPassword' => 'gmailpassword'
);
It's working for me..
afaik you are probably better off currently writing an interface to a python project. E.g. have a microservice that does sending of messages for you in python, and you call them via some json request or similar
see this project, looks promising: https://github.com/tgalal/yowsup
it seems like the only viable option so far, as everything else was shut down or has a high probability to get your account banned
see discussion here:
https://stackoverflow.com/a/46635985/533426
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!
I would like to send notifications from my webserver to my smartphone, preferably through one of the popular mobile chat apps like WhatsApp, Viber or Kik.
Is there any known documentation or API or something, that describes how to send a message to these clients, for example using PHP?
Note that I only need to be able to send notifications to my own smartphone, so requiring specific info to identify my particular client (like cellphone number or something) is fine.
There are many web services that allows you to send and receive SMS/notifications. PHP itself doesn't support this on it's own. You can use a service like Twilio to do this. You can send messages to your own smartphone, or even a friend's.
An example:
<?php
require "Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($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(
"+14158675309" => "Curious George",
"+14158675310" => "Boots",
"+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->sms_messages->create(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"YYY-YYY-YYYY",
// 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";
}
See the documentation here.
Hope this helps!