First of all, I hope that you and your loved ones are safe and healthy during these unprecedented times.
Tracking the current data on the daily impact of the coronavirus had encumbered lots of my friends' and acquaintances' thought processes due to the uncertain data update times. Hence, I developed a bot hosted by a Raspberry Pi, which notifies the user when there is a change in the daily case report of the selected country via WhatsApp, in the hope of soothing their minds even a little bit. In other words, you will get notified via WhatsApp (as notification messages) when the daily case report of your country is uploaded or updated by the data provider - The 2019 Novel Coronavirus Visual Dashboard operated by the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE) with the support of ESRI Living Atlas Team and the Johns Hopkins University Applied Physics Lab (JHU APL). Also, the notifier bot sends an information message in the given period if there is no change (STABLE).
The project in a nutshell:
To obtain the information from the data provider(JHU CSSE with JHU APL), I used a well-programmed application by Rodrigo Pombo (pomber). It collects all the required data for the bot in a JSON file.
As you may have got familiar with my previous projects, to send notification messages via WhatsApp, I used the Twilio's API for WhatsApp in PHP.
To host the coronavirus notifier bot and manage the data evaluation process automatically in PHP to detect the data status of the selected country (STABLE or CHANGED), I set up a LAMP (Linux, Apache, MySQL, PHP) server with MariaDB on Raspberry Pi.
Running on the LAMP server hosted by Raspberry Pi, I designed a user-friendly interface in which you can change the country whose data is collated and enter your Twilio application settings (SID, AUTH_TOKEN, from_phone_number, to_phone_number). The interface also allows you to automatically create a database table named variables before activating the notifier bot. And thus, without needing any coding, you can observe the daily coronavirus case report of the selected country by merely using the bot interface.
By the following steps, you can replicate the WhatsApp coronavirus notifier bot project to run it on your Raspberry Pi or your personal computer as the localhost.
- How to create a Twilio application
- How to install and configure Apache, PHP, and MySQL to create a LAMP web server on Raspberry Pi
- How to install CURL on Raspberry Pi
- How to define a new user and create a new database with MariaDB on Raspberry Pi
- How to develop a web application (bot) in PHP to send WhatsApp notification or information messages depending on the incoming data from an external server
- How to install the bot on Raspberry Pi
- How to activate the bot terminal in a loop on Raspberry Pi
To send WhatsApp messages through Twilio's API, the only thing you need to do is to sign up for a Twilio Trial Account.
After signing up, with your SID and Auth Token, you can use Twilio's API for WhatsApp without any charge. As explained on Twilio, you need to join a shared phone number with your phone in order to verify your phone number and initiate the Twilio's API for WhatsApp.
For more information and learn how to create a new account, click the link below.
You can sign up for Twilio with my referral code here.
1) Create a new application
2) Go to the Sandbox to get the code
3) Connect your phone to your application with the code
If you are a novice in programming with Raspberry Pi, you can inspect well-explained instructions and tutorials on the official Raspberry Pi website.
To go to the official Raspberry Pi setting up tutorial, click here.
- Open a terminal window by selecting Accessories > Terminal from the menu.
- Install the apache2 package by typing the following command into the terminal and pressing Enter:
sudo apt-get install apache2 -y
- To test the server, open Chromium by selecting Internet > Chromium Web Browser from the menu and enter http://localhost.
- If you want, you can use the IP address of your Raspberry Pi as well.
- Install the PHP package with the following command:
- Do not forget to update the Pi.
sudo apt-get update
sudo apt-get install php -y
- Install the MySQL Server (MariaDB) and PHP-MySQL packages by entering the following command into the terminal window:
sudo apt-get install mariadb-server php-mysql -y
- To create a new user, run the MySQL secure installation command in the terminal window.
- You will be asked Enter current password for root (enter for none): — press Enter.
- Type in Y and press Enter to Set root password.
- Type in bot at the New password: prompt, and press Enter.
- Type in Y to Remove anonymous users.
- Type in Y to Disallow root login remotely.
- Type in Y to Remove test database and access to it.
- Type in Y to Reload privilege tables now.
- When complete, you will see the message All done! and Thanks for using MariaDB!.
- To create a database (coronabot), run mysql in the terminal window:
sudo mysql -uroot -p
- Enter the root password - bot
- You will be greeted by the message Welcome to the MariaDB monitor.
- Create the database for the coronavirus notifier bot, named as coronabot, at the MariaDB [(none)]> prompt using:
create database coronabot;
GRANT ALL PRIVILEGES ON coronabot.* TO 'root'@'localhost' IDENTIFIED BY 'bot';
- Exit the MariaDB prompt with Ctrl + D.
- To be able to run the Twilio's API on the localhost, install the php-curl package:
sudo apt-get install php-curl
- Then, restart the apache server to be able to use the installed packages on the localhost:
Step 3: Developing the coronavirus notifier bot and installing it on Raspberry Pi
sudo service apache2 restart
I developed the coronavirus notifier bot (coronabot) in PHP and explained the source code of the bot in the Code Explanation section below for those who are interested.
However, if you are not fond of web development, you can skip the Code Explanation section since the bot is ready to install and run.
If you have completed Step 1 and Step 2, you can run the bot interface to enter settings (SID, AUTH_TOKEN, To_Number, From_Number, Country) without needing any coding on your Raspberry Pi by installing the coronabot on the localhost. All database and user settings are pre-filled.
By the following steps, install the coronabot on the localhost:
- Download the bot with all code and asset files, including the Twilio's API for WhatsApp, in a zipped folder (coronabot) in Downloads.
- Extract the coronabot.zip.
- Move the coronabot folder from Downloads to the server (/var/www/html) by using the terminal because the apache server is a protected location.
sudo mv /home/pi/Downloads/coronabot/ /var/www/html/
- Go to the bot interface:
1) Open the bot interface to create a table named as variables in the coronabot database. You should see the default variables if the bot is working properly after installation.
2) Enter the required settings for the bot - SID, AUTH_TOKEN, To_Phone_Number, From_Phone_Number, Country. All accessible countries are provided. Note: Refrain from entering phone numbers with '+' at the beginning.
3) Display the current settings in the table (variables) and change them even though you activated the terminal to get WhatsApp messages.
4) Via the terminal, according to the settings in the table (variables), get the daily case report of the selected country from the data provider and send WhatsApp notification or information messages depending on the data status (CHANGED or STABLE). If there is a change in date, confirmed, or recovered variables, the bot defines the data status as CHANGED.
Code Explanation (Optional):
In this section, I will explain how I developed the coronavirus notifier in PHP.
- Include the coronabot class.
- Define the coronabot object.
- Connect to the database. If you completed Step 1 and Step 2, the settings should be "localhost", "root", "bot", "coronabot".
- Create the variables table with the default settings in the coronabot database to save information (unless already created).
- If the user changed the bot settings using the bot interface, save new settings to the database table (variables).
- Get the previous data in the database table, including the Twilio settings.
- Print the previous user data in the database table (variables) and create a form with accessible countries.
- Design the bot interface.
- Create a class named as coronabot.
- In the connect_to_database function, connect to the database depending on the entered settings.
- In the create_database_table function, create a database table with the given name and insert the default variables unless already created.
- In the get_previous_data function, get previous data from the database table.
- In the save_new_bot_settings function, if the user changes the bot settings, save the new settings to the database.
- In the save_new_report_data function, save the daily report data of the selected country to the database table.
- In the checkTable function, check whether there is a table with the given name in the database (coronabot).
Note: If you have completed Step 1 and Step 2, do not change any values.
- Include the coronabot class and the Twilio's API autoload page.
- Define the coronabot object and the database (MariaDB) settings - "localhost", "root", "bot", "coronabot".
- Get the required variables from the database table (variables).
- Initiate the Twilio's API for WhatsApp.
- Define the path to gather information from the application created by Rodrigo Pombo (pomber).
- Using the file_get_contents() function, fetch the raw data from the application.
- Decode the raw data in JSON by using the json_decode() function.
- Get the last (recently updated) element in the selected country in the decoded data object.
- Save the information in the current_entry object to the current_data array to call them easily.
- Detect the data status (CHANGED or STABLE).
- Save the current data to the database table (variables).
- According to the data status (STABLE or CHANGED), send a notification message (🔔) or an information message (⚠) via WhatsApp.
- Create a new client to send text messages over WhatsApp. Get the Twilio application settings from the database table, entered via the bot interface.
Download the requests library to make HTTP requests in Python.
pip install requests
- Include the required libraries - requests and sleep.
- In activate_terminal function, make an HTTP Get request to the terminal and print the response text.
- Start the loop and execute the activate_terminal function in the given period - 15 minutes.
Note: I chose to get update messages via WhatsApp every 15 minutes, but you can change the period in the code - 15 * 60.
1) Get notified when there is a change in date, confirmed, or recovered values in the daily case report. (⚠)
2) Change the selected country via the bot interface on Raspberry Pi to obtain information about different countries via WhatsApp.
3) Observe the daily coronavirus (COVID-19) case report of your country in the given period (15 minutes) via WhatsApp. (🔔)