-
Notifications
You must be signed in to change notification settings - Fork 4
/
cowincheckbot.py
340 lines (293 loc) · 11.3 KB
/
cowincheckbot.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
import logging
from typing import Dict
from telegram import ReplyKeyboardMarkup, Update, ReplyKeyboardRemove
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
CallbackContext,
)
import requests
from datetime import date
import configparser
# Read configuration file. Config format available as dummy_config.ini
config = configparser.ConfigParser()
config.read("config.ini")
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
STATE, DISTRICT, PINCODE, DONE = range(4)
TG_MESSAGE_CHAR_LIMIT = 4096
done_markup = [
['Done'],
]
headers = {
'User-Agent': config['API']['user-agent'] #User agent required in the header for public api to work.
}
def create_markup(choice, data) -> list:
markup = []
markup_str = ""
i = 0
t = []
for a in data[choice+"s"]:
markup_str = str(a[choice+'_id'])+". "+a[choice+'_name']
t.append(markup_str)
if (i%2) or i == len(data[choice+"s"])-1:
markup.append(t.copy())
t.clear()
i = i+1
return markup
def start(update: Update, context: CallbackContext) -> int:
if(update.message.text == "New Search"):
context.user_data.clear( )
if('state_id' in context.user_data and 'district_id' in context.user_data):
repeat_markup=[[context.user_data['state_name']+" : "+ context.user_data['district_name']], ['New Search', 'Done']]
update.message.reply_text(
"Do you want to repeat previous search? ",
reply_markup=ReplyKeyboardMarkup(repeat_markup, one_time_keyboard=True, resize_keyboard=True),
)
return DISTRICT
if('pincode' in context.user_data):
repeat_markup=[["PINCODE: "+context.user_data['pincode']], ['New Search', 'Done']]
update.message.reply_text(
"Do you want to repeat previous search? ",
reply_markup=ReplyKeyboardMarkup(repeat_markup, one_time_keyboard=True, resize_keyboard=True),
)
return STATE
r = requests.get("https://cdn-api.co-vin.in/api/v2/admin/location/states", headers=headers)
if(r.status_code != 200):
send_as_markdown("API Error", update)
return DONE
states_data = r.json()
state_markup = create_markup('state', states_data)
update.message.reply_text(
"Enter *PINCODE* or select *STATE*:",
parse_mode="MarkdownV2",
reply_markup=ReplyKeyboardMarkup(state_markup, one_time_keyboard=True, resize_keyboard=True),
)
return STATE
def state_choice(update: Update, context: CallbackContext) -> int:
text = update.message.text
context.user_data['state_id'], context.user_data['state_name'] = text.split(".")
r = requests.get("https://cdn-api.co-vin.in/api/v2/admin/location/districts/"+context.user_data['state_id'], headers=headers)
if(r.status_code != 200):
send_as_markdown("API Error", update)
return DONE
districts_data = r.json()
district_markup = create_markup('district', districts_data)
update.message.reply_text(
"Select district:",
reply_markup=ReplyKeyboardMarkup(district_markup, one_time_keyboard=True, resize_keyboard=True),
)
return DISTRICT
def format_slots_output(centers):
r = ""
i=1
available_centers = []
# Filter centers with available_cpacity > 0
for center in centers:
for session in center['sessions']:
if session['available_capacity']:
if center not in available_centers:
available_centers.append(center)
# Prepare the markup for output
for center in available_centers:
if(i!=1):
r+="\n"
r+=str(i)+"\. *`"+center['name']+"`* "+str(session['min_age_limit'])+"\+ _"+center['fee_type']+"_\n"
for session in center['sessions']:
if session['available_capacity']:
r+="\t\t`"+session['date']+"` "+session['vaccine']+" `"+str(session['available_capacity'])+"` slots\n"
i+=1
return r
def filter_by_pincode(centers, pincode):
new_center = []
for center in centers:
if center['pincode'] == pincode:
new_center.append(center)
return new_center
def split_text(message):
split_pos = []
newline_pos = 0
last_pos = 0
for i in range(1, len(message)):
if message[i] == '\n':
newline_pos = i
if i%(TG_MESSAGE_CHAR_LIMIT-1) == 0:
split_pos.append((last_pos, newline_pos))
last_pos = newline_pos
split_pos.append((last_pos, len(message)-1))
split_list = []
for a,b in split_pos:
split_list.append(message[a:b])
return split_list
def send_as_markdown(message, update):
update.message.reply_text(
message,
parse_mode="MarkdownV2",
reply_markup=ReplyKeyboardMarkup(done_markup, one_time_keyboard=True, resize_keyboard=True)
)
return
def send_instruction(update):
update.message.reply_text(
"Visit https://selfregistration.cowin.gov.in/ to book your slots.\nHappy vaccination!",
reply_markup=ReplyKeyboardMarkup(done_markup, one_time_keyboard=True, resize_keyboard=True)
)
return
def send_info(update, date):
update.message.reply_text(
"Slots for next *7* days from *"+date+"* :",
parse_mode="MarkdownV2",
reply_markup=ReplyKeyboardMarkup(done_markup, one_time_keyboard=True, resize_keyboard=True)
)
return
def district_choice(update: Update, context: CallbackContext) -> int:
today = date.today().strftime("%d/%m/%Y")
text = update.message.text
if not 'district_id' in context.user_data:
context.user_data['district_id'], context.user_data['district_name'] = text.split(".")
r = requests.get("https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id="+context.user_data['district_id']+"&date="+today, headers=headers)
if(r.status_code != 200):
send_as_markdown("API Error", update)
return DONE
centers = r.json()['centers']
center_list = format_slots_output(centers)
if len(center_list) <= TG_MESSAGE_CHAR_LIMIT:
if not len(center_list):
send_as_markdown("No slots available", update)
else:
send_info(update, today)
send_as_markdown(center_list, update)
send_instruction(update)
return DONE
else:
context.user_data['centers'] = centers
update.message.reply_text(
"Too many results! Enter pincode to filter:",
)
return PINCODE
def direct_pincode_choice(update: Update, context: CallbackContext) -> int:
today = date.today().strftime("%d/%m/%Y")
text = update.message.text
if not 'pincode' in context.user_data:
context.user_data['pincode'] = text.strip()
r = requests.get("https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode="+context.user_data['pincode']+"&date="+today, headers=headers)
if(r.status_code != 200):
send_as_markdown("API Error", update)
return DONE
centers = r.json()['centers']
center_list = format_slots_output(centers)
if len(center_list) <= TG_MESSAGE_CHAR_LIMIT:
if not len(center_list):
send_as_markdown("No slots available", update)
else:
send_info(update, today)
send_as_markdown(center_list, update)
send_instruction(update)
return DONE
else:
message_chunks = split_text(center_list)
if(len(message_chunks)):
send_info(update, today)
for message in message_chunks:
if len(message):
send_as_markdown(message, update)
send_instruction(update)
return DONE
def pincode_choice(update: Update, context: CallbackContext) -> int:
today = date.today().strftime("%d/%m/%Y")
pincode = int(update.message.text)
if(context.user_data['centers']):
center_list = format_slots_output(filter_by_pincode(context.user_data['centers'], pincode))
if len(center_list) <= TG_MESSAGE_CHAR_LIMIT:
if not len(center_list):
send_as_markdown("No slots available", update)
else:
send_info(update, today)
send_as_markdown(center_list, update)
send_instruction(update)
return DONE
else:
# Split messages it TG_MESSAGE_CHAR_LIMIT sized chunks
message_chunks = split_text(center_list)
if(len(message_chunks)):
send_info(update, today)
for message in message_chunks:
if len(message):
send_as_markdown(message, update)
send_instruction(update)
return DONE
def done(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
f"Thank you !\n\nIf you want to start a new search, Press /start again.",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
def main() -> None:
tg_config = config['TELEGRAM']
# Create the Updater and pass it your bot's token.
updater = Updater(tg_config['token'])
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Add conversation handler with the states CHOOSING, TYPING_CHOICE and TYPING_REPLY
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
STATE: [
MessageHandler(
Filters.regex('^\d+\.\s[\s\w]+$'), state_choice
),
MessageHandler(
Filters.regex('^\d{6}$'), direct_pincode_choice
),
MessageHandler(
Filters.regex('^PINCODE: \d{6}$'), direct_pincode_choice
),
MessageHandler(
Filters.regex('^New Search$'), start
),
MessageHandler(
Filters.regex('^Done$'), done
)
],
DISTRICT: [
MessageHandler(
Filters.regex('^\d+\.\s[\s\w]+$'), district_choice
),
MessageHandler(
Filters.regex('^[\s\w]+\s:\s[\s\w]+$'), district_choice
),
MessageHandler(
Filters.regex('^New Search$'), start
),
MessageHandler(
Filters.regex('^Done$'), done
)
],
PINCODE: [
MessageHandler(
Filters.regex('^\d{6}$'), pincode_choice
)
],
DONE: [
MessageHandler(
Filters.regex('^Done$'),done
)
],
},
fallbacks=[MessageHandler(Filters.regex('^Start$'), start)],
)
dispatcher.add_handler(conv_handler)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()