-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcowin_bot.py
174 lines (144 loc) · 5.68 KB
/
cowin_bot.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import constrants as keys
from telegram.ext import *
from telegram import *
import requests
from datetime import datetime
# Adding headers
header={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'}
print("Bot started...")
# creating start Command Handler
def start_command(update,context):
update.message.reply_text('Welcome to Covid Vaccine centre tracker.\n Type "Vaccine Center near me"---To find the Vaccine centre near you')
# creating help command handler
def help_command(update,context):
update.message.reply_text('Type "Vaccine Center near me"---To find the Vaccine centre near you')
# creating state command handler
def state_handler(name):
URL = 'https://cdn-api.co-vin.in/api/v2/admin/location/states'
state_name = str(name).title()
response = requests.get(URL,headers=header)
response_json = response.json()
data = response_json["states"]
flag = 0
for each in data:
if each['state_name'] == state_name:
id_state = each['state_id']
flag = 1
break
if flag == 1:
return id_state
else:
return
# creating distict command handler
def district_handler(name,id):
query = 'https://cdn-api.co-vin.in/api/v2/admin/location/districts/{}'.format(id)
district_name = str(name).title()
response = requests.get(query,headers=header)
response_json = response.json()
data = response_json["districts"]
flag=0
for each in data:
if each['district_name'] == district_name:
id = each['district_id']
flag = 1
break
if flag == 1:
return id
else:
return
def pincode_handler(pincode):
print(pincode)
now = datetime.now()
date = now.strftime("%d-%m-%Y")
URL='https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}'.format(pincode,date)
response = requests.get(URL,headers=header)
return response
def message_handler(update,context):
global c
global state_id
text = str(update.message.text).lower()
#print(text)
if text == 'vaccine center near me' and c==0:
update.message.reply_text('Search by Pincode or Search by District')
#update.message.reply_text('Enter the State Name')
c=1
elif text == 'search by pincode' and c==1:
update.message.reply_text('Enter the Pincode')
c=2
elif c==2:
response = pincode_handler(text)
response_json = response.json()
if not response_json["centers"]:
update.message.reply_text('No vaccine centers avaliable under this pincode.Try again!!')
else:
for center in response_json["centers"]:
for session in center["sessions"]:
update.message.reply_text('center_id: {} \nCenter Name: {}\nAddress: {}\nPinecode: {}\nfee_type: {}\nDate: {} \navailable_capacity: {}\nVaccine: {}\nAvailable_capacity_dose1: {}\nAvailable_capacity_dose2: {}\nMinimum age limit: {}\nslot:{}'.format(center["center_id"]
,center["name"]
,center["address"]
,center['pincode']
,center['fee_type']
,session['date']
,session["available_capacity"]
,session['vaccine']
,session['available_capacity_dose1']
,session['available_capacity_dose2']
,session["min_age_limit"]
,session["slots"]))
c=0
elif text == 'search by district' and c==1:
update.message.reply_text('Enter the State name')
c=3
elif c==3:
state_id = state_handler(text)
if state_id:
update.message.reply_text('Enter the District Name')
c = 4
else:
update.message.reply_text('Wrong State name!!Try again')
elif c==4:
district_id = district_handler(text,state_id)
if district_id:
now = datetime.now()
today_date = now.strftime("%d-%m-%Y")
search = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id={}&date={}'.format(district_id,today_date)
response = requests.get(search,headers=header)
response_json = response.json()
data = response_json["sessions"]
flag=0
for each in data:
if each["available_capacity"] > 0:
flag=1
update.message.reply_text('center_id: {} \nCenter Name: {}\nAddress: {}\nPinecode: {}\nfee_type: {}\nfee: {} \navailable_capacity: {}\nVaccine: {}\nAvailable_capacity_dose1: {}\nAvailable_capacity_dose2: {}\nMinimum age limit: {}\nslot:{}'.format(each["center_id"]
,each["name"]
,each["address"]
,each['pincode']
,each['fee_type']
,each['fee']
,each["available_capacity"]
,each['vaccine']
,each['available_capacity_dose1']
,each['available_capacity_dose2']
,each["min_age_limit"]
,each["slots"]))
c=0
if flag == 0:
update.message.reply_text("No vaccine avaliable at the centers near you right now!\nTry after sometime")
else:
update.message.reply_text('Wrong district name!!Try again')
else:
update.message.reply_text("Sorry I don't understand you\n Type '/help' for any help")
def error(update,context):
print(f"update{update} caused error{context.error}")
if __name__=="__main__":
global c
c =0
updater =Updater(keys.API_key,use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start",start_command))
dp.add_handler(CommandHandler("help",help_command))
dp.add_handler(MessageHandler(Filters.text, message_handler))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()