How to Create Covid Whatsapp Bot
Using Python Flask, Twilio SMS API and Heroku.
In this tutorial, you will learn how to make a Whatsapp bot and deploy it on a Heroku server. The bot provides the number of cases reported and some prevention measures.
Before we start you will need to have the following
1. Configure the Twilio WhatsApp Sandbox
- Twilio provides a WhatsApp sandbox where you can easily develop and test your application.
- Let’s connect our smartphone to the sandbox. From your Twilio Console, select Programmable SMS and then click on WhatsApp. The WhatsApp sandbox page will show you the sandbox number assigned to your account and a join code.
- To enable the Whatsapp sandbox for your smartphone, send a Whatsapp message with the given code to the number assigned to your account. The code is going to begin with the word join, followed by a randomly generated two-word phrase.
2. Create a Python Virtual Environment
$ mkdir covid-bot && cd covidbot
$ pip install virtualenv
$ virtualenv covid-bot
$ covid-bot\Scripts\activate$ pip install flask twilio requests
Now the fun part begins. We will start with this initial setup of Flask.
Webhook
The Twilio API for WhatsApp uses a webhook to notify an application when there is an incoming message. Our chatbot application needs to define an endpoint that is going to be configured as this webhook so that Twilio can communicate with it.
from flask import Flask
app = Flask(__name__)
@app.route('/bot’, methods=['POST'])
def bot():
# add webhook logic here and return a response
The important thing to keep in mind about the code above is that the application defines a /bot
endpoint that listens to POST
requests. Each time an incoming message from a user is received by Twilio, they will in turn invoke this endpoint.
3. Messages and Responses
The first thing we need to do in our chat bot is obtain the message entered by the user. This message comes in the payload of the POST
request with a key of Body
. We can access it through Flask’s request
object:
from flask import request
incoming_msg = request.values.get('Body', '')
The response that Twilio expects from the web hook needs to be given in TwiML or Twilio Markup Language, which is an XML-based language. The Twilio helper library for Python comes with classes that make it easy to create this response without having to create XML directly
4. Chatbot logic
For the actual chatbot logic, I’m going to use a very simple, yet surprisingly effective approach. What I’m going to do is search the incoming messages for the keywords A,B,C
so on… and handle the query based on the request. Here is the basic structure of the chatbot:
Third-Party APIs
To supply the chatbot with the information of covid cases. I’m going to use publicly available APIs. I’ve chosen this COVID API.
A GET
request to https://coronavirus-19-api.herokuapp.com/all
returns the cases, deaths, and recovered number of covid cases in JSON format.
Everything Together
Now you have seen all the aspects of the chatbot implementation, so we are ready to integrate all the pieces into the complete chatbot service. You can copy the code below into a app.py
file:
5. Deploy the bot on Heroku
We need a couple of things before we make our app.
- Heroku can’t know what libraries your project uses, so we have to tell him using the
requirements.txt
file to generate the requirements file use pip
pip freeze > requirements.txt
Now you have your requirements file ready to go
- Second you need the
Procfile
which tells heroku where our app starts, so create aProcfile
file and in it type the following
web: gunicorn app:app
- You can also add a
.gitignore
file to your project so that non important files don’t get uploaded to the repository. - Go to the dashboard on Heroku and create a new app. Once you create an app it will direct you to the deploy page, open the settings tab in new window and domain of the app which will be something like
https://appname.herokuapp.com/
Now to deploy using the Heroku-CLI
- Login to heroku
$ heruko login
- Initialize a git repository in our directory
$ git init
$ heroku git:remote -a {heroku-project-name}
- Deploy the app
$ git add .
$ git commit -m "Final Commit Deploying to Heroku"
$ git push heroku master
At this point you will see the building progress in your terminal, if everything went okay you will see something like this.
remote: -----> Launching...
remote: Released v6
remote: https://project-name.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
- Now go to the app page
http://covidjv-bot.herokuapp.com
and since we have set a default endpoint it displays Hello World!! - Final Step is to put the above
herokuapp url
into the Sandbox.
Demo
To use this bot simply click on this LINK or Manual way Send a WhatsApp message to (+1) 415523 8886 with code join shout-daughter. It will show a message saying connected to sandbox. Now start talking by saying Hi | India | USA | Italy and it will display the followup chats.
Conclusion
I hope this tutorial was useful and you now have a better idea of how to build your WhatsApp chatbot. To view the source code.
Thanks for reaching so far. If you liked it give it a clap or two 👏👏👏
Feel free to connect with me on LinkedIn.