WhatsApp Integration With MuleSoft

Learn how to integrate WhatsApp with MuleSoft using the WhatsApp Cloud API.

Shubham Chaurasia
Another Integration Blog
9 min readNov 11, 2022

--

WhatsApp Integration With Mulesoft

On May 2022, at Meta’s flagship conversations event, CEO Mark Zuckerberg announced that they are opening WhatsApp to all businesses with a WhatsApp Cloud API. Previously, businesses had to go through the Business Service Providers (BSPs), such as Twilio and Verloop.io, to have their business communication channels on the WhatsApp Platform.

The WhatsApp Cloud API is the cloud version of the WhatsApp Business API. It allows businesses and developers to leverage WhatsApp to customize their experiences and respond to customers quickly and easily. With the WhatsApp Cloud API, businesses can build and integrate systems that connect thousands of customers with agents or bots, enabling programmatic and manual communication.

You can integrate the API with different backend systems, such as CRM and marketing platforms.

Let’s see some of the differences between what we had earlier (i.e., on-premise APIs) compared to what we have now (i.e., the Cloud API).

Difference Between the On-Premise API and Cloud API

  • Cloud API is hosted on Meta’s servers while the on-premise API is hosted by either individual businesses or BSP’s servers and data centers
  • Cloud API uses Graph API while the on-premise API uses REST API
  • Cloud API is meant for small to medium size businesses while the on-premise API is meant for large enterprises
  • WhatsApp Cloud API users can eliminate the hosting costs involved with WhatsApp on-premise/business APIs.

Now, let’s consider the architecture differences.

For WhatsApp communication, we have two types of conversation:

On-Premise API and Cloud API Architecture

Business Initiated Conversation(BIC): business start a message thread by messaging the user. BIC requires a pre-approved template and prior opt-in. BICs are priced per conversation

User Initiated Conversation(UIC): user starts a message thread by messaging the businesses. No prior opt-in is required.

To send messages or notifications, you need to have business templates. You can create your own template or use pre-built ones. Messages can include appointment reminders, shipping information, issue resolution, or payment updates.

Currently, you can send the following template types:

‣ Text-based message templates: using this you can send simple text-based messages

‣ Media-based message templates: using these templates, you can send documents, images, or videos in the message

‣ Interactive message templates: these templates provide interactive buttons (call-to-action and quick reply). The first option allows your customer to call a phone number and visit a website while the latter option allows your customer to return a simple text message.

Let’s see some of the features that the WhatsApp Cloud API provides.

‣ Encryption: the message is protected by Signal protocol encryption that secures messages before they leave the device. Data is encrypted in transit and at rest. The API uses Graph API for sending messages, and Webhooks for receiving events. Both operate over industry standard HTTPS, protected by TLS.

‣ Scaling: the cloud API automatically scales and adjusts to handle your workload, within your rate limit.

‣ Throughput: the WhatsApp Cloud API supports up to 80 messages per second, combined sending and receiving of text and media messages by default, and up to 250 messages/per second by request.

‣ Metrics: the WhatsApp Cloud API provides the option to see different types of metrics. For example, you can see the number of messages sent and delivered.

➡ Use Cases: let’s consider some possible use cases for the WhatsApp Cloud API:

  • Real-time feedback for your products and services
  • Order and delivery updates and order confirmation
  • Quick responses through the quick reply and call to action buttons
  • Leverage smart buttons for sending product details
  • Install AI-enhanced WhatsApp chatbot and keep your business live 24x7
  • Reduce application drop offs and abandoned carts by automating reminders

This completes the background information about the WhatsApp Cloud API. Now, let’s dive into the integration of the WhatsApp Cloud API with MuleSoft.

❒ Meetup Session/ YouTube Recording

I have delivered a Meetup on this topic. You can watch it on YouTube below. You can watch the step-by-step changes you need to do in the code in the below video.

👉 MuleSoft Integration with WhatsApp

Before we start, we are required to set up developer assets and platform access:

  1. Go to developers.facebook.com
  2. Click on ‘My Apps’
  3. Click on ‘Create App’
  4. Select ‘Business’ as ‘App Type’
  5. Click on ‘Next’ and fill in the required information
  6. ‘Apps Page’ will appear
  7. Click on ‘WhatsApp app Setup’
  8. The WhatsApp Business Platform API dialog box will appear
  9. Leave the defaults (create a business account) and click on ‘Continue’.
  10. ‘Getting Started’ will appear.

This page contains useful information, including the temporary access token (which is valid for 24 hours), your phone number ID, WhatsApp business account ID, configuration for phone number to send a message, how to send message details, and configure webhooks.

Send a Message

Let’s start sending a message to WhatsApp using MuleSoft.

  1. Create a ‘New Project’ in Studio and create the following flow:

2. In ‘HTTP Request Configuration’, go to the ‘Settings’ tab, and on the default headers option, from the drop-down option, select ‘Edit inline’, and configure the temporary token.

3. For HTTP Cloud API request, input the values as shown in the image below:

4. In ‘Transform Message’, paste the following JSON code (check the attached image for reference). Enter the phone number you would like to message in the ‘To field’:

{
"messaging_product": "whatsapp",
"to": "91123456789",
"type": "template",
"template": {
"name": "hello_world",
"language": {
"code": "en_US"
}
}
}

5. Run the project and hit the ‘HTTP Listener’ endpoint.

6. You’ll receive the “Hello World” message on your WhatsApp number.

In the process shown above, we were using the default template. You can create your customized template, as per your business requirements, and can add buttons to it.

Sample Message Using a Customized Template

Now, we have completed the first part of sending messages. The next part is receiving a message, getting an alert when you receive a message from the user, and an alert when a message’s status has changed. For this, we will use the webhook.

Receive a Message

There are two options to receive a message. You can create your own webhook or use the existing webhook services that help you set up a webhook endpoint (e.g., Glitch).

  • If you want to create a webhook using Glitch, you can use this link.

In this tutorial, I will help you set up your own webhook in MuleSoft. Follow the steps below:

  • You need to create an endpoint on a secure server that can process HTTPS requests.
  • Your endpoint must be able to process two types of HTTPS requests: verification requests and event notifications.

Note: since both requests use HTTPS, your server must have a valid TLS or SSL certificate correctly configured and installed. Self-signed certificates are not supported.

Configuring the TLS Certificate

  1. Create a TLS context and configure your certificate. Consult the image below for reference.

2. Create an HTTP Listener Configuration (use this configuration for verification and event notification flow). In the ‘General Tab’, select HTTPS as ‘Protocol’, Host as ‘Default’, and Port as ‘8082’.

3. Go to the TLS tab, and in the TLS configuration, select ‘Global Reference’. Then, select the TLS configuration you created in the last step. Click on ‘OK’.

Now we need to create the endpoint for the verification request. This GET request will be sent by WhatsApp when you configure your webhook URL for the first time in your app dashboard, in the webhook section.

GET Verification Request HTTP Endpoint

  1. Create a flow as shown in the image below:

2. In HTTP Listener Configuration, and in Connector Configuration, select the HTTP Listener Configuration we created in the last step from the drop down menu. Set /webhook as the path.

3. In the ‘Advance’ section, select ‘GET’ as the allowed methods.

4. In the ‘Transform’ message, set the following payload:

%dw 2.0
output text/plain
var mode = attributes.queryParams."hub.mode"
var token = attributes.queryParams."hub.verify_token"
var challenge = attributes.queryParams."hub.challenge"
var verify_token = "helloTest"
var statuscode=200
---
if ((!(mode=='') and !(token=='')) and
((mode == "subscribe") and (token == verify_token)) )
challenge as String
else
403

Note: in the 6th line, we have a “verify_token” value (i.e., “helloTest”). This will be used when configuring the webhook in the WhatsApp cloud app dashboard along with the URL (which we will get after deploying this app to CloudHub). You can change it as per your need.

Now, let’s see the event notifications flow which will receive the user message. This will be a POST flow.

POST Event Notifications HTTP Endpoint

Whenever a trigger event occurs, the WhatsApp Business Platform sees the event and sends a notification to this webhook URL.

  1. Create a flow as shown in the image below:

2. In HTTP Listener Configuration, and in the Connector Configuration, select the HTTP Listener Configuration we created in the last step from the drop down menu. Set /webhook as the path.

3. In the ‘Advance’ section, Select ‘POST’ as allowed methods.

4. In ‘Transform’ message, set the message as payload and output in JSON format.

5. Save and run the application to verify the app is running properly.

6. Deploy the application to CloudHub. For more information, you can refer to this link.

7. After the application is deployed, you will get a URL(<app-name>.us-e2.cloudhub.io) for your application.

Configure the WebHook URL and Subscribe to Messages

  1. Go to the ‘Webhooks’ section from the left menu
  2. From the drop-down, select the ‘WhatsApp Business Account’
  3. Click on ‘Subscribe to this Object’
  4. A dialog box will appear
  5. Input the webhook URL with a webhook endpoint in ‘Callback URL & Token’
  6. Click on ‘Verify & Save’

You’ll see various options to subscribe to. You can use the test option which will send that notification message to your webhook.

When you respond to the business number/account in WhatsApp, the message will be received in the webhook.

As you can see, the angry emoji is received in the logs of our deployed app.

This completes the demonstration of the MuleSoft integration with the WhatsApp Cloud API.

How To Generate Permanent WhatsApp Access Token

  1. Click on this Link:- Meta Business Settings
  2. Select the business account your app is associated with.
  3. Below Users, click System Users. Click Add.
  4. Name the system user, choose Admin as the user role, and click
  5. Create System User.
  6. Select the whatsapp_business_messaging permission
  7. Click Generate New Token

Thank you for reading this article! If you found this helpful, please leave a clap and follow so you can read more articles in the future.

--

--

Shubham Chaurasia
Another Integration Blog

MuleSoft Ambassador | Mule Certified Architect & Developer | 11x Salesforce| 4x AWS | 2x GCP | 2x Solace | 2xAzure | https://linkedin.com/in/shubhamchaurasia1/