twiliowhatsapptwilio-api

In twilio what is the use of whatsapp templates option provided seprately?


As for sending a Whatsapp message out of 24 hour window, we require a whatsapp approved template. What is the difference between the option provided as whatsapp templates and content Editor on twilio.

What I know by now is that the template created through content Editor are used by their sid to send messages outside 24hr window. But then what is the use of other templates as when I tried using its sid to generate a message their was error -

{
    "code": 20422,
    "message": "Invalid Parameter",
    "more_info": "https://www.twilio.com/docs/errors/20422",
    "status": 400
}

And if possible how can I use the templates created using the option Whatsapp templates ??

enter image description here


Solution

  • Good question. I know this is somewhat confusing. Let me try to explain these two different things:

    WhatsApp Message Template

    A WhatsApp message template is a message format that you can use over and over again to message users (...). To use a message template, you must first submit it to WhatsApp. Meta reviews and approves each message template to maintain high-quality content and avoid spam. Once WhatsApp has approved your template, you can use the message template to send notifications.

    From the docs

    TLDR: These messages have be to approved by Meta and can be used outside of a 24hr window.

    What message types are included?

    Where to find in the console

    As shown in your screenshot above

    templates in console

    How to use them in code

    You use the normal messages.create() function with the body property but you need to make sure the body matches the template exactly (incl whitespaces, encoding etc)

    // Download the helper library from https://www.twilio.com/docs/node/install
    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = require('twilio')(accountSid, authToken);
    
    client.messages
      .create({
         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. Thanks',
         to: 'whatsapp:+14155238886'
       })
      .then(message => console.log(message.sid));
    

    Twilio Content API / Editor

    Create and send rich messaging content over any Twilio-supported messaging channel using Twilio's Content API. As you build out your messaging user experience, Content API offers a common framework to maintain a consistent user experience across all channels, without sweating the implementation details.

    From the docs

    TLDR: These templates are not WhatsApp-exclusive and are, depending on the message type, bound to the 24hr window or not.

    What message types are included?

    Where to find in the console

    As shown in the screenshot:

    enter image description here

    How to use them in code

    You use the normal messages.create() function but you need to use the contentSid property to specific which template you want to use and you need a messaging service as a sender.

    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = require('twilio')(accountSid, authToken);
    
    client.messages
          .create({
             contentSid: 'HXXXXXXXXX',
             from: 'MGXXXXXXXX',
             contentVariables: JSON.stringify({
               1: 'Name'
             }),
             to: 'whatsapp:+18551234567'
           })
          .then(message => console.log(message.sid));
    

    Docs