Integrating Twilio With MuleSoft

Leverage the Twilio Connector to send customizable messages to the receivers in the form of both WhatsApp and SMS messages.

Saagnik Adhikary
Another Integration Blog
7 min readOct 3, 2022

--

Before we begin, set up a Twilio account (you can create an account and start a free trial here). For now, skip the two-factor authentication by clicking on set up later, and you will be logged into your Twilio management console.

To send messages from Twilio, you will need to purchase a Trial Number, which is similar to a mobile number you are already familiar with. You can complete the “purchase” by utilizing the $15.50 credit that is provided with your trial account.

To do this, click on Get A Trial Number on the main dashboard. On the next popup, click on Choose This Number after verifying that it has SMS capabilities. You will be presented with your new Twilio trial number and you are now ready to integrate with MuleSoft.

At this point, your Twilio dashboard should look like the image below:

The Trial Number, Account SID, and Auth Token will need to be entered as configuration properties to set up the Twilio connector in MuleSoft.

The number shown in the snippet above can only send SMS messages, not WhatsApp messages. In order to send WhatsApp messages, you must set up a WhatsApp Sandbox in Twilio, which I will describe below.

Setting up WhatsApp Sandbox

On the left hand panel under the Develop tab, expand the Settings dropdown and click on the WhatsApp sandbox settings option.

Navigating into this, you will be presented with a number from which you will receive all Twilio messages on your WhatsApp account. You need to join the Sandbox environment to receive WhatsApp messages from that number.

In my case, the number is +1 415 523 8886 and the Sandbox environment name is melted-slipped (Twilio comes up with goofy WhatsApp sandbox names!). It also shows me the text (join melted-slipped) that I need to send from my WhatsApp number to +1 415 523 8886 in order to receive messages from the number on my WhatsApp.

Once I send the text to that number from my WhatsApp, I am configured to receive SMS messages on my WhatsApp, as shown in the snippet below.

You can further confirm this by clicking Twilio Sandbox for WhatsApp page under Sandbox Participants, and checking for the WhatsApp number (as a USERID) that was used to join the sandbox (using join <sandbox-name>), as shown below.

Back to familiar territory now: Anypoint Studio!

We create a very simple flow in our Studio console, backed by ingenious credentials in config.yaml, that will enable us to leverage the same endpoint to send messages to both WhatsApp and SMS text.

The first component is the HTTP Listener component with its default configurations (Protocol- HTTP, Host — localhost, Port — 8081). The important thing to note is the configurable endpoint, which is shown in the snippet below.

The second component, the Transform Message component, is the heart of our flow that handles all the logic that enables messages to be directed to our phones either as a Text message or as a WhatsApp message.

Before we look at that logic, let’s first understand the input payload to our application.

First, we will send a JSON payload with three fields: communicationChannel, contactNo and message. These fields form the backbone of our communication. Here is why:

  • communicationChannel: specify where you want the message to be sent to, either text or WhatsApp
  • contactNo: the communicationChannel’s/receiver’s text or WhatsApp number (with extension)
    Note: for the WhatsApp number, the number must have already joined the Twilio WhatsApp Sandbox, using the join <sandbox-name>, as was explained earlier
  • message: the customizable message you want to send

A sample POST payload is displayed below:

{

“communicationChannel”: “text”,

“contactNo”: “+918116210182”,

“message”: “Hey Saagnik, sending you a Hi! from Twilio.”

}

The Transform Message component prepares the message in the application/x-www-form-urlencoded form. This is the only form that the Twilio Create Message component expects and works with. We will create a message as shown in the snippet below.

The Body field is derived directly from the payload’s message field and includes the customizable message to send to the receiver.

The To field prepares the receiver’s number to receive the customizable message. When the ‘communicationChannel’ is selected as ‘text’, then the receiver’s number is directly picked up from the ‘contactNo’ field in the payload and set as is to the ‘To’ field. However, when the ‘communicationChannel’ is selected as ‘WhatsApp’, then the receiver’s number needs to be prefixed with WhatsApp. This must be followed by the number, as picked up from the ‘contactNo’ field in the payload, and then set to the ‘To’ field.

The From field is the Twilio number that will send your customized message to your receiver. Now we have two numbers to do that:

  • +17722526765 : this is the number we obtained at the very beginning while we setup our Twilio account (by clicking ‘Get a Trial Number’). Note: this is used to send text/SMS messages and but cannot send messages to WhatsApp.
  • +14155238886 : this is the number we obtained while we setup the Twilio WhatsApp Sandbox. This number can send the messages to any WhatsApp number that has joined its Sandbox environment (using the join <sandbox-name>), as was explained earlier.

The values are configured in the config.yaml, which also has values for setting up the Twilio connector configuration. The snippet below shows my purposeful config.yaml (Note: I have used secure properties to mask crucial values, including username and password).

As you can see there are a few other values configured here. We will specifically look at two of them that are mandatory for our solution to function.

  • Username: this is the ACCOUNT SID value that was obtained from the Twilio dashboard.
  • Password: this is the AUTH TOKEN value, also obtained from the Twilio dashboard.

Note: if you have lost those values, you can return to your Twilio account and click on Console in the top left corner to return back to the dashboard. The values are located under the Account Info.

Finally, we will configure our last component, the “Create Message” component. It is configured as follows:

Next, our Create Message component needs very simple values. Luckily, we have already completed most of the heavy lifting in the Transform Message component logic.

Note: You must provide the username value here, as the Account SID value.

Putting it all together:

I send my first request to receive a text/SMS to any desired number:

I successfully get the message in the associated inbox, as shown below.

Now, I leverage the same endpoint with a different payload to send a WhatsApp message to a number that is already logged into the Twilio WhatsApp Sandbox environment. The number must be logged into the Twilio Sandbox in order to receive a message on WhatsApp from Twilio, since it is a trial Twilio account.

I successfully receive the message on that configured WhatsApp number, as shown below.

We can now log back in to the Twilio console to check the logs regarding our message deliveries. To do this choose the Monitor tab from the top left corner of your console and expand Logs and choose Messaging.

Clicking on a certain message lets you dive right in and check properties that are associated with your sent message.

If a WhatsApp message was sent, you can even see the time the message was read by your recipient, along with all other detailed delivery steps involved if it interests you.

You can now safely disconnect from the Twilio Sandbox environment by sending stop to that chat, as shown below.

Thank you for reading my article, I hope you have learned useful information. Have fun sending customizable messages to your contacts on either WhatsApp or SMS!

--

--

Saagnik Adhikary
Another Integration Blog

Eclectic learner, proficient in the AWS Cloud, delivers REST APIs & EDAs by leveraging MuleSoft to its core. Most likely, to stop by for a verse!