ACME WhatsApp Banking — Twilio setup

Victor Paulo
4 min readAug 29, 2019

--

This post is related to ACME WhatsApp banking, if you didn’t read the initial part, please click here to start from the beginning.

What is Twilio?

Cloud communications platform for building SMS, Voice & Messaging applications on an API built for global scale.

Twilio has a WhatsApp sandbox in Beta testing which allow us to create applications based on this social media platform in a seamless way with a minimal configuration and code requirements.

First things first, you need to create your Twilio account and then you will get a credit where you will be able to use this platform for free (at least till now while I am writing this article).

Twilio API for WhatsApp is now available in early access, which allows developers to start building and prototyping in a sandbox. In order to launch apps in production, you can request access to enable WhatsApp on your Twilio number. WhatsApp is currently opening up this access in a Limited Availability program, where WhatsApp approval is required for all customers who wish to create their own profiles.

Configuring Twilio WhatsApp sandbox

Once you have created your Twilio account, you will get a WhatsApp sandbox number to test your application, you just need to add it in your contact list and send a message join <code>, as shown below, to start interacting with Twilio.

Twilio WhatsApp Playground

After playing a little bit and understanding how it works, it's time to configure the sandbox by adding your callback URL (webhook) so that each message sent to your sandbox will be sent to your backend application.

Twilio WhatsApp Sandbox

Getting the Twilio Credentials for the callback application

In order to connect to Twilio by the callback application we need to get the credentials. We need Account SID and Auth Token information.

Twilio API credentials

Creating the callback API to interact with Twilio

Now it's time to create the callback, the API that will consume and process messages sent from the WhatsApp. The application must be exposed on the internet by a public address (IP or name).

I decided to create the application on Node.js, for that, I needed to install the Twilio SDKs for javascript.

$ npm init
$ npm install twilio express body-parser dotenv --save

After command above the node_modules folder will be created in a current directory and the twilio dependency will be added in a package.json file. It's advisable to add an entry on .gitignore for node_modules to avoid sending the dependencies to github.

The code

Below you can see a simple code to log the content from the Twilio when we send a message through WhatsApp. You must expose your callback application endpoint using a HTTP POST method.

  • The receiver

The receiver application logs the payload sent from Twilio.

Twilio callback application

The output log is:

Message log from Twilio WhasApp API
  • The sender

The sender application intercepts the message sent from Twilio (shown above) and reply with any content you want. Please check the echo example below where we reply with the same information that was sent.

Twilio WhatsApp echo application

You must add you accountSid and authToken credentials in a .env file. Also, remember to add .env entry in your .gitignore file to avoid sharing your credentials publicly in a github.

.env file for Twilio API

The .env content should be moved from .env file into kubernetes secret to avoid the information be exposed when creating and publishing the docker image.

To create a kubernetes secret from .env file, run the following command:

$ kubectl create secret generic apisecret --from-env-file=.env

And then, we need just bind the secret to environment variable of the container as shown in the helm deployment chart below:

K8s — Binding secret to container's environment variable

Extra Tip

The Twilio callback endpoint should be an address visible (routable) on the internet. That said, if you want to run the your node.js application locally before deploying it on cloud you should install Ngrok to test your end-to-end scenario.

ngrok allows you to expose a web server running on your local machine to the internet. Just tell ngrok what port your web server is listening on.

$ ngrok http 192.168.0.3:8080 --log stdout# Where 192.168.0.3 is your local IP address.

Ngrok will give you an external routable address for your application (http and https).

Ngrok- HTTP and HTTPs random addresses

The next post will describe how to setup a Kubernetes cluster on Google Cloud which will act as a middleware for orchestrating the calls between microservices and the backend represented by IBM Watson and AWS. Please check it out here.

--

--

Victor Paulo

“We are what we repeatedly do. Excellence then, is not an act but a habit.” — Aristotle