Search code examples
phptwiliowhatsapp

Send WhatsApp Notification using Pre-approved Template Message on Twilio


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?


Solution

  • 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!