Tutorial: send whatsapp template messages using mautic webhook without third party services

Hello, i don’t know if this is the right category to post this thread, if not please move to the best one.

I hope this will be helpful and sorry for my english :slight_smile:

Yesterday i found a way to send whatsapp template messages (maybe it could work also with non template messages but i didn’t test that case) via mautic webhook without using third party services like twilio, wowa, spoki etc, we only need to upload a php file on a domain and use this file as a webhook url.

Basically you must:

  • upload the files i created on your server (at the end of the post you can find the download link)
  • set a webhook campaign action and point URL to one of the files uploaded in the previous step
  • create a campaign and set webhook data in the way explained below
  • that’s all, our custom webhook will check for data, format in the correct way and forward it to facebook’s cloud api.

what you’ll find into the zip file:

  • index.php empty file, it only needs to not list files in directory
  • webhook.whatsapp.php this file process data and forward them to whatsapp api cloud. This must be the Webhook URL of the campaign action
  • webhook.whatsapp.log log file where the previous file will log any errors

where to upload files
you can upload them where you want, in any domain you have, also in the same domain of mautic. For example:

  • www.domain.it/mautic-whatsapp-webhook
  • www.domain.it/whatsapp/mautic-whatsapp-webhook

mautic webhook screen
the only “difficult” step is the way we must write the mautic webhook data to let the system format data in the correct way for whatsapp api. It is not user friendly but not too hard to understand.

this is the webhook screenshot

About config you must simulate nested arrays needed by whatsapp cloud api with a separator. I used the | pipe symbol but we can use every char we wan, take care to not use chars that we can write into strings (all digits for example) or data will be corrupted.

this is the webhook config explained:

sender_id you can grab it from Log into Facebook. It is the id of the number that will send the message.
access_token you can grab it from Log into Facebook. This will expire after 23 hours, anyway i found a blog post that explains how to get an access token that will never expire
messaging_product this seems to be fixed to “whatsapp”. Maybe it could be hard coded into the php file but at the moment i set it here.
to the mobile number of our lead
type template is the string to use to send template messages, this can also be image, audio etcetera (see below link for Whatsapp Api Cloud reference guide )
template|name this is the first nested array (template) and it’s first value (name) that indicates the name of the message template (as mentioned, here we’re using one of the default template, but we can create our own templates)
template|language|code here we must set the template language code. Every template can have translations, you’ll find it when you create the template in facebook business center

THESE ARE THE BASIC DATA TO SEND A SIMPLE MESSAGE, WITH NO CUSTOM DATA (i.e. the hello_world template), BELOW I’M GOING TO WRITE OPTIONAL FIELDS NEEDED FOR MORE COMPLEX MESSAGES

template|components|0|type this is required to create the array of components. This is required for templates that embed custom variables (such as name or any other field). For simple messages without custom data (like the hello_world template) it is not needed.
template|components|0|parameters|X|type with this field we’re going to tell whatsapp which is the TYPE of the X custom value we’re passing to the api
template|components|0|parameters|0|text with this field we’re going to tell whatsapp which is the VALUE of the X custom value we’re passing to the api.
##################################

A NOTE ABOUT template|components|0|parameters|X|type AND template|components|0|parameters|X|text:
if we have more then one custom value into the message (for example first name, last name and a date) we must add 6 data fields (2 for each variable):

  • template|components|0|parameters|0|type + template|components|0|parameters|0|text
  • template|components|0|parameters|1|type + template|components|0|parameters|1|text
  • template|components|0|parameters|2|type + template|components|0|parameters|2|text

the first one represent the type of message we can send via whatsapp and can be one of text, currency, date_time, video,image,document and the second one must respect what we set in the first… for example a string if we send a text, a media object if we send an image etcetera (anyway see Whatsapp Api Cloud reference guide below for all data type and explaination)
##################################

well, this is the first release, i have tested it only with simple template and template with 1 custom data and it works well.

it should be tested with videos or images

below you can file useful links

DOWNLOAD ZIP FILE

how to create your facebook app and setup never expires access token

Whatsapp Api Cloud reference guide for fields and values