|
| 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 |
0 commit comments