CovBot - a WhatsApp Based Chatbot for COVID 19 Info & More

by abbasmj in Circuits > Software

653 Views, 2 Favorites, 0 Comments

CovBot - a WhatsApp Based Chatbot for COVID 19 Info & More

CoVbot - A Simple WhatsApp Chatbot For Latest COVID 19 Status Demo
chatbot_iq80hc.png

CoVbot is a simple and intuitive Whatsapp based chatbot. The main feature of the bot is:

It can give you the latest status of COVID-19 in the country of choice in a simple and intuitive way.

Additionally, the bot can suggest fun activities to do AT HOME such as:

  1. Suggest a Movie - A movie to watch from the Top 10 movies list, with a short overview of the plot and duration. Since this list is not hardcoded in software this will always give you the latest updates based on current trends.
  2. Suggest a TV Show - A tv show to watch from the most popular TV shows, with a short overview of the plot and ratings. Since this list is not hardcoded in software this will always give you the latest updates based on current trends.
  3. Suggest a Book - A book to read from the Top 10 Booklist, with the blurb and cover image of the book.
  4. Daily workouts - This is a video-based on a 7-day workout schedule provided by CRANK gym on their Instagram Account.

If you enjoy this Instructable, please consider voting for it in the First Item Author Contest. (and yes this is my first instructable so if something is not clear or needs more explanation please let me know in the comment section and I can help you out :)

Supplies

This project is solely software-based project, so anyone following this instructable with a laptop/desktop PC/macOS/Linux and internet can complete this project. I will also include a detailed section as at the end to explain the code/process in more detail for people with some programming experience, Please note this is not needed to complete the project

Project Difficulty Level:

Not very easy, but not very hard

The Hardware we will use:

  • A laptop/desktop running Windows/macOS/Linux.
  • A mobile-phone with WhatsApp Messenger installed

The software we will use:

  1. Python Programming Language
  2. ngrok - is a tool used to allow us to access our server from outside our network
  3. Editor of your choice : ( e.g. Notepad++, Sublime Text, Vim etc)

Install the Required Software

Python Tutorial for Beginners 1: Install and Setup for Mac and Windows

In this step we will be doing the following:

  1. Install Python > 3.6 and test it
  2. Install required python libraries
  3. Install ngrok

If you have both the application installed you can skip this step

Install Python:

The whole backend/server codebase for this project is written in Python 3.6. Therefore to run our applications we need to have Python > 3.6 installed on our computer. Follow the tutorial by CoreySchafer on how to install python for Windows and macOS

You can test everything is installed correctly by typing the following in the cmd/Terminal:

 python -c 'print(f"Hello World")'

If everything was installed correctly, then Hello World should be printed on the screen. If you get an invalid syntax error, then you have the wrong version of python installed. Install a version of python >= 3.6

Install required python libraries using pip:

We will be using the following python libraries to make our software work:

  1. Flask - This is the framework for our server
  2. Twilio - This library provides a way for python to communicate with WhatsApp
  3. Request - This library is used to request data from APIs
  4. BeautifulSoup4 - This library is used to scrape information from websites
  5. lxml - This library is used in combination with BeautifulSoup to extract the relevant information from a website

To install these libraries you can do the following:

Open CMD/Terminal and type the following command:

pip install flask, twilio, request, beautifulsoup4, lxml

OR

Download the requirements.txt file and open a terminal in the directory where the file is and type:

pip install -r requirements.txt

Install ngrok

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

Follows the guide on the ngrok website to install ngrok for the operating system you are using.

Tip: Step 3 of the guide is not relevant for this project so can be skipped



Downloads

Create a Twilio Account

Untitled 2.png
twilio_whatsapp.png

In this step we will be doing the following:

  1. Signup for a Twilio Account
  2. Quick Introduction to useful sections on Twilio's Console

Sign Up:

In this project, we will be using the Twilio Whatsapp API to connect our python program to Whatsapp. To be able to use Twilio API we need to first create an account on the Twilio's official website. For a trial account Twilio offers a $15 free credit to for us to use.

Quick Introduction:

Once you have created an account, The most interesting sections of the Twilio console for this project are:

Dashboard - From the dashboard, you can see the amount of credit you still have left, you can edit your project name and change the programming language

Whatsapp subsection in Programmable SMS section - From the Whatsapp section of the console, you can get access to the join in code for the bot, the number of Whatsapp we will use in the project to talk to our bot and also the setting up the webhook. More details about all these will be described in the next steps

Get API Key for COVID 19 Data, Movies and TV Show

api_key_tmdb.png
rapid_api_key.png
An API key or application programming interface key is a code that gets passed in by computer applications. The program or application then calls the API or application programming interface to identify its user, developer or calling program to a website.

We use an API to get information for the latest COVID 19 Status, Movies and TV Shows. To access the API you need a key which is personal to each user. In this step, we will get these keys.

Get API Key for COVID 19 Data:

  • Scroll down to the “Header Parameters” section of the API console.
  • Your API Key should be visible in the “X-RapidAPI-Key” field.

Get API Key for movies and tv show data:

  • Log in or sign up for your TMDB account
  • After that go to your setting - API
  • Scroll down to the section "API Key (v3 auth)"
  • Your API key should be visible below it

Create "config.py" file

We will now create a config.py file to store our API keys. We create a separate file for these, cause API keys are confidential information and if you share your project you should not share your API key.

  • Create a new project directory
  • Inside the newly created directory create a new file called "config.py"
  • Edit this file with an editor of your choice (Sublime, Notepad++) and copy and replace the following text with relevant information obtained in the previous step:
session_key = "secret" #This is not safe.. but just for test its is ok
rapid_api_key = "<your COVID 19 Data API key>"
api_key = " <your movies and tv show data API key>"
  • Save the file

Connect Python and Twilio Via Ngrok

In this step we will do the following:

  1. Download the source code
  2. Execute the program and forward local IP to a public address via ngrok so we can make a request to it
  3. Configure our Twilio account to forward the request to our server

Download the source code for the bot:

Download all the attached file in this setup into the project directory created in the last step.

Execute the program:

Go to the directory of the source code in the CMD/terminal and execute the following command:

python server_main.py

Make sure to have the "config.py" that we created in the last step or else you will get an error

The output should be something like this:

 * Serving Flask app "server_main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on <a href="http://127.0.0.1:5000/"> http://127.0.0.1:5000/ </a> (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 740-257-236<strong>
</strong>

This means your server is running correctly on your local network on port 5000. To make this server accessible from outside your local network we will use ngrok

Forward local IP to a public address via ngrok

Go to the directory where you downloaded ngrok via CMD/terminal and execute the following command:

ngrok http 5000

The output should be something like this :

ngrok by @inconshreveable                                                                                                                                                     (Ctrl+C to quit)
                                                                                                                                                                                              
Session Status                online                                                                                                                                                          
Session Expires               7 hours, 59 minutes                                                                                                                                             
Version                       2.3.35                                                                                                                                                          
Region                        United States (us)                                                                                                                                              
Web Interface                 <a href="http://127.0.0.1:4040"> http://127.0.0.1:4040 </a>                                                                                                                                           
Forwarding                    _http://d44c955749bf.ngrok.io_ -> _http://localhost:5000_                                                                                                           
Forwarding                    _https://d44c955749bf.ngrok.io_ -> _http://localhost:5000_                                                                                                         
                                                                                                                                                                                              
Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                                                     
                              0       0       0.00    0.00    0.00    0.00   <br>

Copy the HTTP link from the "Forwading" part (just till ngrok.io). (I have added the _ in this example to avoid instructable to intercept it as a link)

Configure Twilio to use the new address to forward server request to:

Now that we have successfully forwarded our server to be accessed publicly we need to configure Twilio such that when a request is made using Twilio Whatsapp API we forward the request to our server. To do this we need to do the following:

  1. Go to the Whatsapp section on Twilio's Console
  2. Navigate to the "Sanbox" subsection
  3. In the "WHEN A MESSAGE COMES" textbox paste the HTTP link from ngrok copied with the /sms extension (DO NOT COPY THE _ at the start and end):
_http://d44c955749bf.ngrok.io/sms_<br>


Now everything for the chatbot is set up. In the next step, we can test if everything is working correctly

Test Our Application

image5.jpeg
image4.jpeg
image3.jpeg
image2.jpeg
image1.jpeg
image0.jpeg

In this step we will do the following:

  1. Join our bot using the access code
  2. Try out our application

Join our bot using the access code

In this project, we will use the Twilio's Whatsapp Sandbox Number for our Whatsapp bot. For each person, this number will be different. You can find your number like this:

  • Login to your Twilio Account
  • Go to Twilio Console -> Whatsapp Section -> Sandbox
  • You will see your Twilio Sandbox number with the following text:
Invite your friends to your Sandbox. Ask them to send a WhatsApp message to : <Your Number>
  • Due to a security policy of Whatsapp, everyone who wants to talk to an automated bot via Whatsapp needs to explicitly opt-in using a code. You can find out your code by looking in the same section as your Twilio sandbox number with the text:

After you have acquired your Twilio Sandbox Whatsapp Number and opt-in code, to start using the bot go to your mobile phone and do the following:

  1. Save the "Twilio Sandbox Whatsapp Number" to your contact with your choice of name (E.g CovBot)
  2. Open Whatsapp and send the following message to that contact:
join <your opt-in-code>
  • You should see a message like this:
Twilio Sandbox: Your are all set ....

This means you have connected with your bot and ready to ask it qs

Try out our application:

To test the various features of your bot after you have connected send the following message to the bot:

Hi

You should see the following text:

Welcome to CoVbot !

A simple chatbot that can
give the latest updates of
COVID-19 in a simple, quick
& easy way. Since we are all
quarantined, the bot can be
used to suggest some fun
activities to make the most
of our time at home as we
strive to keep ourselves,
friends and family safe.

We hope you enjoy it and find it useful!

Send 4 to get started !<br>

Now you can follow the option to try out the different features of the bot.

THAT'S IT! Your Whatsapp Chatbot is ready !!!! Congratulations

How Does It Work and Future Improvement:

main_loop.png
movies_tv__data_explanation.png
covid_data_explanation.png
book_code.png

This section is for people who have some python programming experience. If you don't you can skip this section

Code Explanation

Main Loop:

When a Whatsapp message is sent to your Twilio's number, the Twilio API makes a POST request to your server that you specified. The server is implemented using the Flask framework and so we can use the Flask Request object to retrieve the data received during the POST request. Based on the data ( Numbers in this case) we decide what option the user has selected and give the appropriate information

COVID Data and Movies and TV Shows:

The COVID 19, Movies and TV Shows Data are retrieved from an API. I use the request python library to get the data and then convert it into a JSON format. Then I just parse the relevant information. For the Movies and TV Show, I also used the random python library to choose a random TV show and Movie

Book Details

The book suggestion algorithm used in the project is just a web scraper. I could not find any API that gives you a random book with details, so I just scrape a website using BeautifulSoup4 and using the lxml parser I extract the relevant information of the book

Sub Menu Details:

Implementing the sub-menus was a little bit more tricky than the rest since Whatsapp messages are like SMSes which is a stateless protocol. To solve this I have used two techniques:

  1. Using Global Variables to remember the state of the message - This is only implemented for the COVID 19 Sub-option. In this when a user selects "Status of COVID 19 in my country option" a global variable named incomplete_message is set to True to indicate then another messaged based on the previous option is still needed. Then there is a check in the start that checks if a message is marked incomplete if so it assumes the message is the country name for the COVID data and passes the information to the correct function and set the global variable incomplete message to False
  2. Using Twilio Cookies and Flask Sessions - Cookies and Flask Sessions are used in the "Suggest a Quarantine Activity" sub-menu to implement statefulness, just like any web application on the internet uses it these days, but instead of remembering the things like your username or account it remember the conversion between Two numbers. If you like more information on this part I recommended reading this awesome guide on Twilio Cookies written by the Twilio Team

Future Improvements:

  1. Make use of Object Orientedness of Python and Design Patterns to improve code architecture and DRYness
  2. Remove global variables
  3. Error Handling can be improved
  4. Automatically update ngrok address using corn and Twilio CLI
  5. Code Documentation

Final Notes

I hope you all like this project. This is my first instructable, so If something is not clear or needs more explanation do let me know in the comment section, and I can help you out. Also if you made this project with a different technique or feature list do all share it