-
Notifications
You must be signed in to change notification settings - Fork 1
/
COVID19BharatBot.py
291 lines (213 loc) · 12.4 KB
/
COVID19BharatBot.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
#!/usr/bin/env python3
"""
COVID19Bharat Bot to provide live statistics and Corona virus information from India.
"""
import logging
# importing modules
import json
import requests
import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Read the token from the secret file
def read_token():
with open('token.json') as f:
data = json.load(f)
# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
return data['token']
############################### Bot Control ####################################
def statistics_menu(update, context):
query = update.callback_query
query.edit_message_text(text=get_stats(), parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=statistics_keyboard())
def statewise_menu(update, context):
query = update.callback_query
query.edit_message_text(text=get_stats_statewise(), parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=statewise_keyboard())
def helplines_menu(update, context):
query = update.callback_query
query.edit_message_text(text=get_helpline_data(), parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=helplines_keyboard())
def guidelines_menu(update, context):
query = update.callback_query
query.edit_message_text(text=guidelines_message(), parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=guidelines_keyboard())
############################ Keyboards #################################
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('Statistics', callback_data='statistics')],
[InlineKeyboardButton('Statistics State-wise', callback_data='statewise')],
[InlineKeyboardButton('Helplines', callback_data='helplines')],
[InlineKeyboardButton('Guidelines', callback_data='guidelines')]]
return InlineKeyboardMarkup(keyboard)
def statistics_keyboard():
keyboard = [[InlineKeyboardButton('Statistics State-wise', callback_data='statewise')],
[InlineKeyboardButton('Helplines', callback_data='helplines')],
[InlineKeyboardButton('Guidelines', callback_data='guidelines')]]
return InlineKeyboardMarkup(keyboard)
def statewise_keyboard():
keyboard = [[InlineKeyboardButton('Statistics', callback_data='statistics')],
[InlineKeyboardButton('Helplines', callback_data='helplines')],
[InlineKeyboardButton('Guidelines', callback_data='guidelines')]]
return InlineKeyboardMarkup(keyboard)
def helplines_keyboard():
keyboard = [[InlineKeyboardButton('Statistics', callback_data='statistics')],
[InlineKeyboardButton('Statistics State-wise', callback_data='statewise')],
[InlineKeyboardButton('Guidelines', callback_data='guidelines')]]
return InlineKeyboardMarkup(keyboard)
def guidelines_keyboard():
keyboard = [[InlineKeyboardButton('Statistics', callback_data='statistics')],
[InlineKeyboardButton('Statistics State-wise', callback_data='statewise')],
[InlineKeyboardButton('Helplines', callback_data='helplines')]]
return InlineKeyboardMarkup(keyboard)
###############################################################################
# #
# Official feeds for the data #
# #
# Official data Stats: https://api.rootnet.in/covid19-in/stats/latest #
# Stats as a daily series: https://api.rootnet.in/covid19-in/stats/daily #
# Hospitals & bed stats: https://api.rootnet.in/covid19-in/stats/hospitals #
# Contact & helpline: https://api.rootnet.in/covid19-in/contacts #
# Notifications & advisories: https://api.rootnet.in/covid19-in/notifications #
# #
# Unofficial feeds for the data #
# #
# Patient data: https://api.rootnet.in/covid19-in/unofficial/covid19india.org
# State-wise data: https://api.rootnet.in/covid19-in/unofficial/covid19india.org/statewise
# State-wise history(from 14th March): https://api.rootnet.in/covid19-in/unofficial/covid19india.org/statewise/history
latest_stats = "https://api.rootnet.in/covid19-in/unofficial/covid19india.org/statewise"
daily_stats = "https://api.rootnet.in/covid19-in/stats/daily"
hospitals_stats = "https://api.rootnet.in/covid19-in/stats/hospitals"
helpline = "https://api.rootnet.in/covid19-in/contacts"
notifications = "https://api.rootnet.in/covid19-in/notifications"
def get_new_stats():
# Opening latest_stats JSON file
return requests.get(latest_stats).json()
# Opening helpline JSON file
helpline_json = requests.get(helpline).json()
# print(json_file.json())
# latest total data
def get_stats():
latest_stats_json = get_new_stats()
return "*COVID19 in India* \n\n" + "Confirmed - " + str(
latest_stats_json['data']['total']['confirmed']) + "\nDeaths - " + str(
latest_stats_json['data']['total']['deaths']) + "\nRecovered - " + str(
latest_stats_json['data']['total']['recovered']) + "\nActive - " + str(
latest_stats_json['data']['total']['active']) + "\n\n*Stay Home, Stay Safe, Save Lives.*"
# latest statewise total data
def get_stats_statewise():
latest_stats_json = get_new_stats()
state_data = "*State-Wise Cases:*\n\nState | Confirmed | Recovered | Deaths | Active\n"
for single_state in latest_stats_json['data']['statewise']:
state_data += "*" + str(single_state['state']) + "* | " + str(single_state['confirmed']) + " | " + str(
single_state['recovered']) + " | " + \
str(single_state['deaths']) + " | " + str(single_state['active']) + "\n"
state_data += "\n\n*Stay Home, Stay Safe, Save Lives.*"
return state_data
# helpline data
def get_helpline_data():
helpline_data = ""
# print(helpline_json["data"]["contacts"])
for state in helpline_json["data"]["contacts"]["regional"]:
# print(str(state["loc"]))
helpline_data += str(state["loc"]) + ": " + str(state["number"]) + "\n"
helpline_data += "\n\n*Stay Home, Stay Safe, Save Lives.*"
return helpline_data
############################# Messages #################################
def main_menu_message():
return 'Hello, I\'m COVID19 Bharat Bot, I\'m here to help you with COVID19 Bharat updates based on state press ' \
'bulletins ' \
'and reliable news channels. Let me ' \
'know how can I help you\n/stats - Statistics India\n/statewise - Statistics ' \
'state-wise India\n/helpline - COVID helpline numbers\n/faq - Frequently Asked Questions\n/guidelines - to ' \
'win war against COVID-19'
def faq_message():
return "*Q.Why does I have more postive count than MoH?*\nA.MoH updates the data at a scheduled time and I provide " \
"you update from COVID19India.org which takes data from state press bulletin and reliable news " \
"channels.\n\n*Q. Why people putting in time and resources to create me while not gaining a single penny " \
"from " \
"me?*\nA. Because it affects all of us. Today it's someone else who is getting infected. Tomorrow it will " \
"be us. We need to prevent the spread. We need to document the data so that people with knowledge are able " \
"to map the data.\n\n*Q. How is the data gathered for this project?*\nA. I collect the data from " \
"COVID19India.org which takes it from each state press release, official government links and reputable " \
"news channels as source. Data is validated by group of volunteers and pushed into Google sheets at the " \
"moment. Google sheet is also available for public."
def guidelines_message():
return "*Help the nation,\nHelp stop coronavirus.*\n\n*DO THE FIVE*\n1. *HANDS* Wash them often\n2. *ELBOW* Cough " \
"into it\n3. *FACE* Don't touch it\n4. *SPACE* Keep safe distance\n5. *HOME* Stay if you can\n\n*DO NOT DO " \
"THE FIVE*\n1. *EYES* Do " \
"not touch it\n2. *NOSE* Do not touch it\n3. *MOUTH* Do not touch it\n4. *HOME* Do not leave it\n5. " \
"*RUMORS* Do not spread it\n\n*Stay Home, Stay Safe, Save Lives.*"
def help_message():
return "This is what all you can ask me to share\n/about - Description\n/stats - Statistics India\n/statewise - " \
"Statistics state-wise India\n/helpline - COVID helpline numbers\n/faq - Frequently Asked " \
"Questions\n/guidelines - to win war against COVID-19'"
############################# Responders ###############################
def start(update, context):
update.message.reply_text(main_menu_message(), reply_markup=main_menu_keyboard())
# Send statistics
def stats(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(get_stats(), parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=statistics_keyboard())
# Send description
def helpline(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(get_helpline_data(), parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=helplines_keyboard())
# Send description
def description(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(main_menu_message(), parse_mode=telegram.ParseMode.MARKDOWN)
def guidelines(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(guidelines_message(), parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=guidelines_keyboard())
def statewise(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(get_stats_statewise(), parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=statewise_keyboard())
def faq(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(faq_message(), parse_mode=telegram.ParseMode.MARKDOWN, reply_markup=main_menu_keyboard())
# Help details
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text(help_message(), parse_mode=telegram.ParseMode.MARKDOWN)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
############################# Handlers ###############################
def main():
"""Start the bot."""
updater = Updater(read_token(), use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("guidelines", guidelines))
dp.add_handler(CommandHandler("helpline", helpline))
dp.add_handler(CommandHandler("stats", stats))
dp.add_handler(CommandHandler("statewise", statewise))
dp.add_handler(CommandHandler("about", description))
dp.add_handler(CommandHandler("faq", faq))
# Buttons
dp.add_handler(CallbackQueryHandler(helplines_menu, pattern='^helplines$'))
dp.add_handler(CallbackQueryHandler(statistics_menu, pattern='^statistics$'))
dp.add_handler(CallbackQueryHandler(statewise_menu, pattern='^statewise$'))
dp.add_handler(CallbackQueryHandler(guidelines_menu, pattern='^guidelines$'))
# on noncommand i.e message - echo the message on Telegram
# dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# 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()