-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVaccineCountToTG.py
109 lines (93 loc) · 3.92 KB
/
VaccineCountToTG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
"""
Created on Sun May 9 14:18:47 2021
@author: Ashit Agarwal
"""
import requests
from datetime import date
import re
from dotenv import load_dotenv
import os
load_dotenv()
#Gets the last chat id so it can be used later to send message
def getLastChatId():
updates = "https://api.telegram.org/bot{}/getUpdates".format(token)
response = requests.get(updates)
response = response.json()
last_update = len(response["result"]) - 1
chat_id = response["result"][last_update]["message"]["chat"]["id"]
return str(chat_id)
#Function to send message to Telegram Bot
#@param: bot_message: Contains the message to be sent
def telegramBotSendText(bot_message):
send_text = 'https://api.telegram.org/bot' + token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
#Gets the pincode from users and checks if valid or not
#@returns: returns pincode entered
def getPincode():
correct = False
while correct == False :
pincode = input("Enter Pincode\n")
correct = bool(re.match("^[1-9][0-9]{5}$", str(pincode)))
if correct == True:
return pincode
#Gets current date
#@return : returns date today
def getCurrentDate():
dateToday = date.today()
dateToday = dateToday.strftime("%d-%m-%Y")
return dateToday
#Gets the Centers data for the entered pincode and date
#@params : pincode: pincode of the area to be looked for.
#@params : dateToday: the current date
#@returns : returns the data received if present other returns empty string
def getRequest(pincode, dateToday):
sampleUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pincode, dateToday)
headers = {'accept': 'application/json','Accept-Language': 'hi_IN', 'User-Agent': sampleUserAgent}
response = requests.get(url, headers=headers)
if response.status_code == 200:
result = response.json()
result = result['centers']
return result
else:
telegramBotSendText("Error Encountered")
return ""
#Finds the center that have vaccines and calls printSessionsAvailable() funtion
#to send the centers with vaccine
#@param: result: Contains data of all the centers for the pincode
def showResults(result):
centerCount = len(result)
totalCenterVaccineAvailableAt = 0
for i in range(centerCount):
nameOfCenter = result[i].get("name")
sessions = result[i].get("sessions")
sessions = list(filter(lambda x: x.get("available_capacity") > 0, sessions))
sessionsCount = len(sessions)
if(sessionsCount > 0):
telegramBotSendText(nameOfCenter)
totalCenterVaccineAvailableAt += 1
printSessionsAvailable(sessions, sessionsCount)
if totalCenterVaccineAvailableAt == 0 :
telegramBotSendText("All slots are booked")
#Sends available vaccine count message to Telegram
def printSessionsAvailable(sessions, sessionsCount):
for i in range(sessionsCount):
vaccineAvailable = sessions[i].get("available_capacity")
dateAvailable = sessions[i].get("date")
ageLimit = sessions[i].get("min_age_limit")
if(vaccineAvailable > 0 ):
message = "On Date {} vaccines available are {} for age {}+".format(dateAvailable,vaccineAvailable, ageLimit)
telegramBotSendText(message)
token = os.getenv('bot_token')
bot_chatID = getLastChatId()
pincode = 201005
#pincode = getPincode()
dateToday = getCurrentDate()
telegramBotSendText("Get 7 days covid vaccination data from {}".format(dateToday))
result = getRequest(pincode, dateToday)
if len(result)>0:
showResults(result)
else:
telegramBotSendText("No Center Available")