Sitemap / Advertise

Introduction

Via WhatsApp, observe the daily coronavirus case report of your country in a given period and get notified when there is a change.


Tags

Share

WhatsApp Coronavirus (COVID-19) Notifier Bot Running on Raspberry Pi

Advertisement:


read_later

Read Later



read_later

Read Later

Introduction

Via WhatsApp, observe the daily coronavirus case report of your country in a given period and get notified when there is a change.

Tags

Share





Advertisement

Advertisement




    Components :
  • [1]Raspberry Pi 3 Model B+
  • [1]Raspberry Pi SD Card (32GB)

Description

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.

project-image
Figure - 42.1


project-image
Figure - 42.2

Preview: What You Will Learn

- 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

Step 1: Creating a Twilio application

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.

Go to Twilio.

You can sign up for Twilio with my referral code here.

1) Create a new application

project-image
Figure - 42.3

2) Go to the Sandbox to get the code

project-image
Figure - 42.4

3) Connect your phone to your application with the code

project-image
Figure - 42.5

Step 2: Setting up a LAMP web server on Raspberry Pi

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.

project-image
Figure - 42.6

- 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

project-image
Figure - 42.7

- 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.

hostname -I

project-image
Figure - 42.8


project-image
Figure - 42.9

- 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

project-image
Figure - 42.10

- 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

project-image
Figure - 42.11

- To create a new user, run the MySQL secure installation command in the terminal window.

sudo mysql_secure_installation

- 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!.

project-image
Figure - 42.12

- 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';

FLUSH PRIVILEGES;

- Exit the MariaDB prompt with Ctrl + D.

project-image
Figure - 42.13

- To be able to run the Twilio's API on the localhost, install the php-curl package:

sudo apt-get install php-curl

project-image
Figure - 42.14

- Then, restart the apache server to be able to use the installed packages on the localhost:

sudo service apache2 restart

Step 3: Developing the coronavirus notifier bot and installing it on Raspberry Pi

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.

Install:

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.

project-image
Figure - 42.15

- Extract the coronabot.zip.

project-image
Figure - 42.16

- 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/

project-image
Figure - 42.17

- Go to the bot interface:

http://localhost/coronabot/

Features:

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.

project-image
Figure - 42.18

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.

project-image
Figure - 42.19

3) Display the current settings in the table (variables) and change them even though you activated the terminal to get WhatsApp messages.

project-image
Figure - 42.20

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.

http://localhost/coronabot/terminal.php

project-image
Figure - 42.21

Code Explanation (Optional):

In this section, I will explain how I developed the coronavirus notifier in PHP.

1) index.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.

project-image
Figure - 42.22


project-image
Figure - 42.23

2) index.css

- Design the bot interface.

project-image
Figure - 42.24

3) class.php

- 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).

project-image
Figure - 42.25


project-image
Figure - 42.26

4) terminal.php

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.

project-image
Figure - 42.27


project-image
Figure - 42.28

Step 4: Programming Raspberry Pi to create a loop

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.

project-image
Figure - 42.29


project-image
Figure - 42.30

Features

1) Get notified when there is a change in date, confirmed, or recovered values in the daily case report. (āš )

project-image
Figure - 42.31

2) Change the selected country via the bot interface on Raspberry Pi to obtain information about different countries via WhatsApp.

project-image
Figure - 42.32


project-image
Figure - 42.33

3) Observe the daily coronavirus (COVID-19) case report of your country in the given period (15 minutes) via WhatsApp. (šŸ””)

project-image
Figure - 42.34

Videos

Demonstration

Code

index.php

Download



<?php

// Include the coronabot class:
include_once "class.php";
// Define the coronabot object:
$corona_bot = new coronabot();
// Connect to the database:
$corona_bot->connect_to_database("localhost", "root", "bot", "coronabot");
// Create the variables table with the default settings in the coronabot database to save information (unless already created):
$corona_bot->create_database_table("variables");
// If the user changed the bot settings using the bot interface, save new settings to the database table (variables):
if(isset($_POST["submit"])){
	$corona_bot->save_new_bot_settings("variables", $_POST["sid"], $_POST["token"], $_POST["to_number"], $_POST["from_number"], $_POST["country"]);
}
// Get the previous data in the database table, including the Twilio settings:
$previous_data = $corona_bot->get_previous_data("variables");

?>

<!DOCTYPE html>
<html>
<head>
 <title>WhatsApp Coronavirus Notifier Bot</title>
 
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta name="theme-color" content="#ff5c33">
 <meta name="apple-mobile-web-app-status-bar-style" content="#ff5c33">

 <!--link to favicon-->
 <link rel="icon" type="image/png" sizes="36x36" href="icon.png">
 <link rel="icon" type="image/png" sizes="48x48" href="icon.png">
 <link rel="icon" type="image/png" sizes="78x78" href="icon.png">

  <!-- link to font -->
 <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@300;400&display=swap" rel="stylesheet">
 
 <!--link to index.css-->
 <link rel="stylesheet" type="text/css" href="index.css">
 
</head>
 <body>
<div id="controller">
<h2>WhatsApp Coronavirus Notifier Bot</h2>
<form action="" method="POST">
<label>Twilio App SID: </label><br><input type="text" name="sid" value="<?php echo $previous_data["sid"]; ?>" /><br><br>
<label>Twilio App AUTH_TOKEN: </label><br><input type="text" name="token" value="<?php echo $previous_data["token"]; ?>" /><br><br>
<label>Twilio App To_Phone_Number: </label><br><input type="text" name="to_number" value="<?php echo $previous_data["to_number"]; ?>" /><br><br>
<label>Twilio App From_Phone_Number: </label><br><input type="text" name="from_number" value="<?php echo $previous_data["from_number"]; ?>" /><br><br>
<label>Country (<?php echo $previous_data["country"]; ?>): </label><br>
<select name="country">
   <optgroup label="Accessible">
   <option value="Afghanistan">Afghanistan</option>
   <option value="Albania">Albania</option>
   <option value="Algeria">Algeria</option>
   <option value="Andorra">Andorra</option>
   <option value="Angola">Angola</option>
   <option value="Anguilla">Anguilla</option>
   <option value="Antigua and Barbuda">Antigua and Barbuda</option>
   <option value="Argentina">Argentina</option>
   <option value="Armenia">Armenia</option>
   <option value="Australia">Australia</option>
   <option value="Austria">Austria</option>
   <option value="Azerbaijan">Azerbaijan</option>
   <option value="Bahamas">Bahamas</option>
   <option value="Bahrain">Bahrain</option>
   <option value="Bangladesh">Bangladesh</option>
   <option value="Barbados">Barbados</option>
   <option value="Belarus">Belarus</option>
   <option value="Belgium">Belgium</option>
   <option value="Belize">Belize</option>
   <option value="Benin">Benin</option>
   <option value="Bhutan">Bhutan</option>
   <option value="Bolivia">Bolivia</option>
   <option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option>
   <option value="Brazil">Brazil</option>
   <option value="Brunei">Brunei</option>
   <option value="Bulgaria">Bulgaria</option>
   <option value="Burkina Faso">Burkina Faso</option>
   <option value="Burundi">Burundi</option>
   <option value="Cabo Verde">Cabo Verde</option>
   <option value="Cambodia">Cambodia</option>
   <option value="Cameroon">Cameroon</option>
   <option value="Canada">Canada</option>
   <option value="Central African Republic">Central African Republic</option>
   <option value="Chad">Chad</option>
   <option value="Chile">Chile</option>
   <option value="China">China</option>
   <option value="Colombia">Colombia</option>
   <option value="Comoros">Comoros</option>
   <option value="Congo (Brazzaville)">Congo (Brazzaville)</option>
   <option value="Congo (Kinshasa)">Congo (Kinshasa)</option>
   <option value="Costa Rica">Costa Rica</option>
   <option value="Cote d'Ivoire">Cote DIvoire</option>
   <option value="Croatia">Croatia</option>
   <option value="Cuba">Cuba</option>
   <option value="Cyprus">Cyprus</option>
   <option value="Czechia">Czech Republic</option>
   <option value="Denmark">Denmark</option>
   <option value="Djibouti">Djibouti</option>
   <option value="Dominica">Dominica</option>
   <option value="Dominican Republic">Dominican Republic</option>
   <option value="Ecuador">Ecuador</option>
   <option value="Egypt">Egypt</option>
   <option value="El Salvador">El Salvador</option>
   <option value="Equatorial Guinea">Equatorial Guinea</option>
   <option value="Eritrea">Eritrea</option>
   <option value="Estonia">Estonia</option>
   <option value="Eswatini">Eswatini</option>
   <option value="Ethiopia">Ethiopia</option>
   <option value="Fiji">Fiji</option>
   <option value="Finland">Finland</option>
   <option value="France">France</option>
   <option value="Gabon">Gabon</option>
   <option value="Gambia">Gambia</option>
   <option value="Georgia">Georgia</option>
   <option value="Germany">Germany</option>
   <option value="Ghana">Ghana</option>
   <option value="Greece">Greece</option>
   <option value="Greenland">Greenland</option>
   <option value="Grenada">Grenada</option>
   <option value="Guatemala">Guatemala</option>
   <option value="Guinea">Guinea</option>
   <option value="Guyana">Guyana</option>
   <option value="Haiti">Haiti</option>
   <option value="Honduras">Honduras</option>
   <option value="Hungary">Hungary</option>
   <option value="Iceland">Iceland</option>
   <option value="India">India</option>
   <option value="Indonesia">Indonesia</option>
   <option value="Iran">Iran</option>
   <option value="Iraq">Iraq</option>
   <option value="Ireland">Ireland</option>
   <option value="Israel">Israel</option>
   <option value="Italy">Italy</option>
   <option value="Jamaica">Jamaica</option>
   <option value="Japan">Japan</option>
   <option value="Jordan">Jordan</option>
   <option value="Kazakhstan">Kazakhstan</option>
   <option value="Kenya">Kenya</option>
   <option value="Korea, South">Korea, South</option>
   <option value="Kosovo">Kosovo</option>
   <option value="Kuwait">Kuwait</option>
   <option value="Kyrgyzstan">Kyrgyzstan</option>
   <option value="Latvia">Latvia</option>
   <option value="Lebanon">Lebanon</option>
   <option value="Liberia">Liberia</option>
   <option value="Libya">Libya</option>
   <option value="Liechtenstein">Liechtenstein</option>
   <option value="Lithuania">Lithuania</option>
   <option value="Luxembourg">Luxembourg</option>
   <option value="Madagascar">Madagascar</option>
   <option value="Malaysia">Malaysia</option>
   <option value="Maldives">Maldives</option>
   <option value="Mali">Mali</option>
   <option value="Malta">Malta</option>
   <option value="Mauritania">Mauritania</option>
   <option value="Mauritius">Mauritius</option>
   <option value="Mexico">Mexico</option>
   <option value="Moldova">Moldova</option>
   <option value="Monaco">Monaco</option>
   <option value="Mongolia">Mongolia</option>
   <option value="Montenegro">Montenegro</option>
   <option value="Mozambique">Mozambique</option>
   <option value="Namibia">Namibia</option>
   <option value="Nepal">Nepal</option>
   <option value="Netherlands">Netherlands</option>
   <option value="New Zealand">New Zealand</option>
   <option value="Nicaragua">Nicaragua</option>
   <option value="Niger">Niger</option>
   <option value="Nigeria">Nigeria</option>
   <option value="North Macedonia">North Macedonia</option>
   <option value="Norway">Norway</option>
   <option value="Oman">Oman</option>
   <option value="Pakistan">Pakistan</option>
   <option value="Panama">Panama</option>
   <option value="Papua New Guinea">Papua New Guinea</option>
   <option value="Paraguay">Paraguay</option>
   <option value="Peru">Peru</option>
   <option value="Phillipines">Philippines</option>
   <option value="Poland">Poland</option>
   <option value="Portugal">Portugal</option>
   <option value="Qatar">Qatar</option>
   <option value="Romania">Romania</option>
   <option value="Russia">Russia</option>
   <option value="Rwanda">Rwanda</option>
   <option value="Saint Lucia">Saint Lucia</option>
   <option value="Saint Vincent and the Grenadines">Saint Vincent and the Grenadines</option>
   <option value="San Marino">San Marino</option>
   <option value="Saudi Arabia">Saudi Arabia</option>
   <option value="Senegal">Senegal</option>
   <option value="Serbia">Serbia</option>
   <option value="Seychelles">Seychelles</option>
   <option value="Singapore">Singapore</option>
   <option value="Slovakia">Slovakia</option>
   <option value="Slovenia">Slovenia</option>
   <option value="Somalia">Somalia</option>
   <option value="South Africa">South Africa</option>
   <option value="Spain">Spain</option>
   <option value="Sri Lanka">Sri Lanka</option>
   <option value="Sudan">Sudan</option>
   <option value="Suriname">Suriname</option>
   <option value="Syria">Syria</option>
   <option value="Sweden">Sweden</option>
   <option value="Switzerland">Switzerland</option>
   <option value="Taiwan*">Taiwan*</option>
   <option value="Tanzania">Tanzania</option>
   <option value="Thailand">Thailand</option>
   <option value="Togo">Togo</option>
   <option value="Trinidad and Tobago">Trinidad and Tobago</option>
   <option value="Tunisia">Tunisia</option>
   <option value="Turkey">Turkey</option>
   <option value="Uganda">Uganda</option>
   <option value="Ukraine">Ukraine</option>
   <option value="United Arab Emirates">United Arab Emirates</option>
   <option value="United Kingdom">United Kingdom</option>
   <option value="US">United States of America</option>
   <option value="Uraguay">Uruguay</option>
   <option value="Uzbekistan">Uzbekistan</option>
   <option value="Venezuela">Venezuela</option>
   <option value="Vietnam">Vietnam</option>
   <option value="Yemen">Yemen</option>
   <option value="Zambia">Zambia</option>
   <option value="Zimbabwe">Zimbabwe</option>
   </optgroup>
</select><br><br>
<button type="submit" name="submit" value="ok">Submit</button>

</form>
</div>

 </body>
 </html>


index.css

Download



html{background-color:#eb2e00;font-family: 'Roboto Slab', serif;}

div{display:block;background-color:#1F2020;width:80%;margin:auto;border:2px solid white;border-radius:20px;padding-left:10px;padding-right:10px;}
h2{color:white;font-size:25px;text-align:center;user-select:none;}
form{padding-left:20px;padding-right:20px;}
label{color:#F3D060;}
input{width:80%;}
button{display:block;width:70%;margin:auto;border:none;border-radius:20px;background-color:#A5282C;margin-bottom:20px;color:white;font-size:20px;cursor:pointer;outline:none;}
button:hover{background-color:#EE7762;}


class.php

Download



<?php

class coronabot {
	public $conn, $previous_data;
	
	public function connect_to_database($DBServerName, $DBUserName, $DBPassword, $DBName){
		$this->conn = mysqli_connect($DBServerName, $DBUserName, $DBPassword, $DBName);
	}
	
	public function create_database_table($table){
		if(!$this->checkTable($table)){
		    $sql = "CREATE TABLE `$table` (
                      sid varchar(255) NOT NULL,
                      token varchar(255) NOT NULL,
                      to_number varchar(255) NOT NULL,
                      from_number varchar(255) NOT NULL,
                     `date` varchar(255) NOT NULL PRIMARY KEY,
                      country varchar(255) NOT NULL,
                      confirmed varchar(255) NOT NULL,
                      recovered varchar(255) NOT NULL
                    );";
			mysqli_query($this->conn, $sql);
			$sql_insert = "INSERT INTO `$table` (`sid`, `token`, `to_number`, `from_number`, `date`, `country`, `confirmed`, `recovered`) VALUES ('default', 'default', 'default', 'default', 'default', 'default', 'default', 'default');";
			mysqli_query($this->conn, $sql_insert);
		}
	}
	
	public function get_previous_data($table){
		$sql = "SELECT * FROM `$table`";
		$result = mysqli_query($this->conn, $sql);
		if(mysqli_num_rows($result) > 0){
			if($rows = mysqli_fetch_assoc($result)){
				$this->previous_data = array(
				           "sid" => $rows["sid"],
				           "token" => $rows["token"],
				           "to_number" => $rows["to_number"],
				           "from_number" => $rows["from_number"],
				           "country" => $rows["country"],
						   "date" => $rows["date"],
				           "confirmed" => $rows["confirmed"],
				           "recovered" => $rows["recovered"]
				       );
			}
		}
		return $this->previous_data;
	}
	
	public function save_new_bot_settings($table, $sid, $token, $to_number, $from_number, $country){
		if(!empty($sid) && !empty($token) && !empty($to_number) && !empty($from_number) && !empty($country)){
			$sql = "UPDATE `$table` SET `sid`='$sid', `token`='$token', `to_number`='$to_number', `from_number`='$from_number', `country`='$country';";
			mysqli_query($this->conn, $sql);
		}
	}
	
	public function save_new_report_data($table, $date, $confirmed, $recovered){
		$sql = "UPDATE `$table` SET `date`='$date', `confirmed`='$confirmed', `recovered`='$recovered';";
		mysqli_query($this->conn, $sql);
	}
	
	private function checkTable($table){
		$sql = "SHOW TABLES FROM `coronabot` LIKE '$table'";
		$checkSql = mysqli_num_rows(mysqli_query($this->conn, $sql));
		if($checkSql < 1){ return false; }else{ return true; }
	}
}

?>


terminal.php

Download



<?php
// Include the coronabot class and the Twilio's API autoload page.
include_once "class.php";
include_once "WhatsAppAPI/Twilio/autoload.php";
// Define the coronabot object and the database (MariaDB) settings:
$corona_bot = new coronabot();
$corona_bot->connect_to_database("localhost", "root", "bot", "coronabot");
// Get the required variables from the database table (variables):
$previous_data = $corona_bot->get_previous_data("variables");
// Initiate the Twilio's API for WhatsApp:
use Twilio\Rest\Client;
// Define the path to gather information from the API created by Rodrigo Pombo (pomber).
$data_url = "https://pomber.github.io/covid19/timeseries.json";
// Using the file_get_contents() function, fetch the raw data from the API.
$raw_data = file_get_contents($data_url);
// Decode the raw data in JSON by using the json_decode() function.
$data = json_decode($raw_data);
// Get the last (recently updated) element in the selected country in the decoded data object.
$country = $previous_data["country"];
$current_entry = end($data->$country);
// Save the information in the current_entry object to the current_data array to call them easily.
$current_data = array(
    "date" => $current_entry->date,
	"confirmed" => $current_entry->confirmed,
	"deaths" => $current_entry->deaths,
	"recovered" => $current_entry->recovered
);
// Detect the data status:
$STATUS = "STABLE";
$header = "šŸ””šŸ””šŸ””šŸ””šŸ””šŸ””šŸ””šŸ””šŸ””";
if($current_data["date"] != $previous_data["date"] || $current_data["confirmed"] != $previous_data["confirmed"] || $current_data["recovered"] != $previous_data["recovered"]){
	$STATUS = "CHANGED";
	$header = "āš āš āš āš āš āš āš āš āš ";
}
// Save the current data to the database table (variables):
$corona_bot->save_new_report_data("variables", $current_data["date"], $current_data["confirmed"], $current_data["recovered"]);
// According to the data status (STABLE or CHANGED), send a notification message or a information message via WhatsApp.
echo "Working...<br><br>Test: ".$country." / ".$current_data["confirmed"]." / ".$STATUS; // TEST THE SERVER
// Create a new client to send text messages over WhatsApp. Get the Twilio application settings from the database table, entered via the bot interface.
$twilio = new Client($previous_data["sid"], $previous_data["token"]);
// Note: Enter phone numbers without adding '+'.
$message = $twilio->messages 
             ->create("whatsapp:+".$previous_data["to_number"], 
                 array( 
                       "from" => "whatsapp:+".$previous_data["from_number"],       
                       "body" => "$header\n\nšŸŒ $country\n\nšŸ“Š Status: $STATUS\n\nāŒ› Date: ".$current_data["date"]."\n\nšŸ“Œ Confirmed: ".$current_data["confirmed"]."\n\nšŸ“Œ Deaths: ".$current_data["deaths"]."\n\nšŸ“Œ Recovered: ".$current_data["recovered"]."\n\n$header"
                      ) 
                 );
echo '<br><br>Message Send...';	

?>


coronabot_loop.py

Download



# WhatsApp Coronavirus Notifier Bot Running on Raspberry Pi
#
# Raspberry Pi 3 Model B+
#
# By Kutluhan Aktar
#
# Via WhatsApp, observe the daily coronavirus (COVID-19) case report of your country and get notified when there is a change.
# 
# For more information and explanation, visit the link below:
# https://www.theamplituhedron.com/projects/WhatsApp-Coronavirus-Notifier-Bot-Running-on-Raspberry-Pi/

from time import sleep
import requests

def activate_terminal(url):
    # Make an HTTP Post Request to the localhost.
    request = requests.get(url)
    # Print the response to check the localhost connection.
    print("\r\n" + request.text + "\r\n")

# Start the loop.
while True:
    activate_terminal("http://localhost/coronabot/terminal.php")
    sleep(15 * 60)


Schematics

project-image
Schematic - 41.1

Downloads

Coronavirus Notifier Bot (Zipped)

Download