2
votes

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?

2
Try "body" => "Your login code is 1234", or other templates described in the docs link. - Alex Baban
Hi @AlexBaban Thanks for the help but unfortunately is still says undelivered. Are you Twilio's WhatsApp user too? - Adrian
I only tried the sandbox. Maybe if your code is correct, try Twilio's support. - Alex Baban
I got no problem in sandbox. And of course I contacted them, currently Twilio's support also confuse what's happening. I wonder if anyone else having the same problem. For now I'll just wait for the solution while sit back and relax. - Adrian

2 Answers

2
votes

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!

1
votes

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.