Skip to content

Commit df24903

Browse files
Merge pull request avinashkranjan#408 from vybhav72954/iss_116
Added a COVID-19 Bot for WhatsApp
2 parents 4e6c833 + b68ecb3 commit df24903

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

Whatsapp_COVID-19_Bot/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
2+
TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
3+
NUM="+91XXXXXXXXXX"

Whatsapp_COVID-19_Bot/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# WhatsApp COVID-19 Bot
2+
3+
A COVID-19 Bot build using `Twilio API`, that tracks the Number of Infected persons, Recovered Persons and Number of
4+
Total deaths along with the day-to-day increase in the statistics. The information is then updated via WhatsApp.
5+
6+
## Note
7+
8+
The script requires personal info, like `API-Token`, `API-ID`, and `PHONE-NUMBER`
9+
for that reason, a [`.env`](.env.example) file has been used, for more info, see usage.
10+
11+
## Usage
12+
13+
- Setup a Virtual Environment.
14+
- Download dependencies using `pip install -r requirements.txt`.
15+
- Set up an account at [`Twilio`](https://www.twilio.com/). It's Free.
16+
- Follow
17+
[this guide](https://medium.com/hackernoon/how-to-send-whatsapp-message-using-python-and-twilio-api-fc63f62154ca)
18+
for Setting up WhatsApp API.
19+
- Make a `.env` file similar to `.env.example` file.
20+
- Paste the required information:
21+
22+
```text
23+
SID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" <ACCOUNT SID goes here>
24+
TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" <AUTH TOKEN>
25+
NUM="+91XXXXXXXXXX" <TARGET PHONE NUMBER GOES HERE>
26+
```
27+
28+
- The bot script is now ready.
29+
- For scheduling, [`apscheduler`](https://apscheduler.readthedocs.io/en/stable/)
30+
has been used for cron-style scheduling.
31+
- The script is coded to run the bot once every day.
32+
- For making your own cron schedule, use this [website](https://crontab.guru/).
33+
- After setting up the Cron Job, run the scheduler script using `python3 schedule.py`.
34+
35+
## Output
36+
37+
![](https://i.postimg.cc/GpmVJrJt/Whats-App-Image-2021-02-27-at-20-18-13.jpg)
38+
39+
Sample Message
40+
41+
```text
42+
Last Updated on: 2021-02-27
43+
Top 3 Indian States sorted by Newly registered cases of COVID-19.
44+
[Maharashtra]
45+
| Total Infected = 68810
46+
| New Infections = 3349
47+
| Total Recovery = 2017303
48+
| New Recovery = 4936
49+
| Total Deaths = 52041
50+
| New Deaths = 48
51+
52+
[Punjab]
53+
| Total Infected = 4222
54+
| New Infections = 352
55+
| Total Recovery = 170968
56+
| New Recovery = 255
57+
| Total Deaths = 5814
58+
| New Deaths = 15
59+
60+
[Gujarat]
61+
| Total Infected = 2136
62+
| New Infections = 145
63+
| Total Recovery = 262487
64+
| New Recovery = 315
65+
| Total Deaths = 4408
66+
| New Deaths = 0
67+
```
68+
69+
## Author(s)
70+
71+
Made by [Vybhav Chaturvedi](https://www.linkedin.com/in/vybhav-chaturvedi-0ba82614a/)
72+
73+
### Disclaimer
74+
75+
Kindly follow all the guidelines of Twilio and respect the request rate.
76+
77+
Despite the recent downfall in cases, COVID-19 is still a major threat for all of us, I strongly request you to follow
78+
all the necessary guidelines.

Whatsapp_COVID-19_Bot/covid_bot.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# IMPORTS
2+
3+
from twilio.rest import Client
4+
import pandas as pd
5+
import requests
6+
from datetime import datetime
7+
import decouple
8+
9+
# Twilio Details
10+
account_sid = decouple.config("SID") # please change it to your own
11+
auth_token = decouple.config("TOKEN") # please change it to your own
12+
client = Client(account_sid, auth_token)
13+
14+
15+
def send_message(receiver, message):
16+
"""
17+
Send message to receivers using the Twilio account.
18+
:param receiver: Number of Receivers
19+
:param message: Message to be Sent
20+
:return: Sends the Message
21+
"""
22+
message = client.messages.create(
23+
from_='whatsapp:+14155238886',
24+
body=message,
25+
to=f'whatsapp:{receiver}'
26+
)
27+
return message
28+
29+
30+
# The list of Receivers, setup the .env file accordingly for maximum safety. See README
31+
receiver_list = [decouple.config("NUM")]
32+
33+
# Covid Report of India. See README fir Info.
34+
url = "https://api.apify.com/v2/key-value-stores/toDWvRj1JpTXiM8FF/records/LATEST?disableRedirect=true"
35+
data_json = requests.get(url).json()
36+
37+
# Reading the Information form JSON data.
38+
df = []
39+
for row in range(len(data_json["regionData"])):
40+
df.append(data_json["regionData"][row])
41+
df = pd.DataFrame(df)
42+
# Sorted top 3 states according to New-Infections
43+
data = df.sort_values(['newInfected'], ascending=False)[:3]
44+
45+
46+
# Final Message to be sent
47+
region_name = data["region"].tolist()
48+
current_timestamp = str(datetime.now().date())
49+
messages = f"Last Updated on: {current_timestamp}\n" \
50+
f"Top 3 Indian States sorted by Newly registered cases of COVID-19."
51+
for regions in region_name:
52+
each_row = data[data["region"] == regions]
53+
54+
# The Information contained in the message.
55+
message_partition = f"""
56+
[{regions}]
57+
| Total Infected = {str(each_row['totalInfected'].tolist()[0])}
58+
| New Infections = {str(each_row['newInfected'].tolist()[0])}
59+
| Total Recovery = {str(each_row['recovered'].tolist()[0])}
60+
| New Recovery = {str(each_row['newRecovered'].tolist()[0])}
61+
| Total Deaths = {str(each_row['deceased'].tolist()[0])}
62+
| New Deaths = {str(each_row['newDeceased'].tolist()[0])}
63+
"""
64+
65+
messages = messages + message_partition
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
python-decouple==3.4
2+
twilio==6.41.0
3+
pandas==0.25.3
4+
requests==2.22.0
5+
apscheduler==3.6.3

Whatsapp_COVID-19_Bot/schedule.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Imports
2+
3+
from apscheduler.schedulers.blocking import BlockingScheduler
4+
from covid_bot import *
5+
6+
scheduler = BlockingScheduler()
7+
8+
9+
def bot_schedule():
10+
"""
11+
A scheduler for the covid_bot script
12+
"""
13+
for numb in receiver_list:
14+
print(f"sending message to {numb}")
15+
send_message(numb, messages)
16+
17+
18+
# The Job is scheduled for once a day.
19+
# The event is triggered at 5:30AM IST (Midnight UTC)
20+
scheduler.add_job(bot_schedule, 'cron', days='*/1')
21+
scheduler.start()

0 commit comments

Comments
 (0)