Last week, I decided to build a Twitter bot to tweet out the latest Covid-19 cases and news tracked by Worldometers and India Covid-19 Tracker.
While waiting for my developer account request to get approved by Twitter (which is taking rather longer than usual), I started looking into some other platforms where I could start developing right away.
One such platform is WhatsApp, which for being the most popular messaging application in the world, is rapidly gaining interest among businesses to connect with their customers using the WhatsApp Business API. Usually, WhatsApp takes several weeks for approving a WhatsApp Business account if applied directly. But with one of it’s official service providers like Twilio, it could take only about a couple of weeks.
Let us explore the Twilio API for WhatsApp to start interacting with WhatsApp programmatically.
The first step is to create a free Twilio account. All new users get a $15 bonus trial credit on signup to test out their platform. To start using the Twilio API for WhatsApp in production, we will need to purchase a number from Twilio and apply for a WhatsApp Business profile. We are going to skip this step for now and directly start developing our application, and test it out using the Sandbox in the console.
I will be using Python3 on my local development machine running Linux.
First, we need to copy the Accound SID and Auth Token from the Twilio console, and use them to set our environment variables that will allow our Python script to access our Twilio account and use the API.
From the terminal, run the export command to set our environment variables named TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN, as shown below:
$ export TWILIO_ACCOUNT_SID='<your twilio account sid>'
$ export TWILIO_AUTH_TOKEN='<your twilio authentication token>'
With our environment variables now set, we are ready to write our Python script and test it out. But first, we are going to create a new directory for our project. We are going to call it whatsapp-test-bot.
From the terminal, run the command below to create our directory:
$ mkdir whatsapp-test-bot
Next, we are going to create a new virtual environment to keep our dependencies isolated from other projects. Navigate to our newly created directory, and create a new virtual environment using the command below:
$ python3 -m venv whatsapp_test_env
or, if you have the virtualenv python package installed, run
$ virtualenv whatsapp_test_env
After creating the virtual environment, activate it using the command below:
$ source ./whatsapp_test_env/bin/activate
Then, install the Twilio Python helper library into the virtual environment:
$ pip install twilio
Finally, create a file named app.py and add the following code:
from twilio.rest import Client
# client credentials are read from TWILIO_ACCOUNT_SID and AUTH_TOKEN
client = Client()
# this is the Twilio sandbox testing number
from_whatsapp_number='whatsapp:+14155238886'
# add your own WhatsApp Messaging number here
to_whatsapp_number='whatsapp:+xxxxxxxxxxx'
client.messages.create(body='Hello, world!',
from_=from_whatsapp_number,
to=to_whatsapp_number)
The above code imports the Twilio Python helper library, instantiates the helper library client, sets a from and to WhatsApp number, and then sends a single message with the client.messages.create()
function call.
Our Python script is now ready, and we just need to activate the WhatsApp sandbox from the Twilio console to test it out.
From the WhatsApp page in the Twilio Console, note the default sandbox number provided by Twilio and the sandbox keyword.
Open WhatsApp on your device, and send join <your sandbox keyword>
to the sandbox number to connect to it. You should receive a reply from the number confirming that you have been connected to the sandbox.
That’s it! With the virtual environment activated, and the environment variables set, you are now ready to receive messages sent by your app from the sandbox you have connected to.
To disconnect from the sandbox, simply send stop
.
In the next part of this story, we will go beyond the basics, and look at the process of actually building a chatbot that would scrape Worldometers for the latest data on Coronavirus, and respond to user queries with accurate results from the website.
See you in the next one!