-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
237 lines (189 loc) · 7.02 KB
/
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
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
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
CallbackContext,
)
import telegram
import requests
import logging
import messages
import re
import json
from datetime import time
from env import getenv
def get_users():
try:
with open("users.json") as f:
data = json.load(f)
return data
except Exception as e:
if (
type(e).__name__ == "JSONDecodeError"
or type(e).__name__ == "FileNotFoundError"
):
open("users.json", "w+").write(json.dumps({}))
return {}
else:
logging.error("Exception occured", exc_info=True)
return False
def save_users(users):
try:
open("users.json", "w").write(json.dumps(users))
except Exception:
logging.error("Exception occured", exc_info=True)
def add_user(chat_id, username, first_name):
users = get_users()
user_data = {
"username": username,
"first_name": first_name,
}
users[str(chat_id)] = user_data
save_users(users)
def start(update: telegram.Update, context: CallbackContext):
chat_id = update.message.chat_id
add_user(chat_id, update.message.chat.username, update.message.chat.first_name)
context.bot.send_message(
chat_id=chat_id, text=messages.msg_start, parse_mode=telegram.ParseMode.HTML
)
def about(update: telegram.Update, context: CallbackContext):
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id, text=messages.msg_about, parse_mode=telegram.ParseMode.HTML
)
def count(update: telegram.Update, context: CallbackContext):
message = messages.get_count_msg()
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
def today(update: telegram.Update, context: CallbackContext):
message = messages.get_today_msg()
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
def lastupdated(update: telegram.Update, context: CallbackContext):
message = messages.get_lastupdated_msg()
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
def statewise(update: telegram.Update, context: CallbackContext):
message = messages.get_statewise_msg()
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
def state(update: telegram.Update, context: CallbackContext):
message = messages.ask_state_msg()
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id,
text=message,
parse_mode=telegram.ParseMode.HTML,
reply_markup=telegram.ForceReply(),
)
def districtwise(update: telegram.Update, context: CallbackContext):
message = messages.ask_state_for_districtwise_msg()
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id,
text=message,
parse_mode=telegram.ParseMode.HTML,
reply_markup=telegram.ForceReply(),
)
def daily_message(context: CallbackContext):
users = get_users()
message = messages.get_count_msg()
for chat_id in users:
try:
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
except Exception as e:
if type(e).__name__ == "Unauthorized":
print(
"{name} has blocked the bot.".format(
name=users[chat_id]["first_name"]
)
)
continue
print("Daily updates sent!")
def subscribe(update: telegram.Update, context: CallbackContext):
chat_id = update.message.chat_id
add_user(chat_id, update.message.chat.username, update.message.chat.first_name)
message = messages.subscription_success()
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
def unsubscribe(update: telegram.Update, context: CallbackContext):
chat_id = update.message.chat_id
users = get_users()
del users[str(chat_id)]
save_users(users)
message = messages.unsubscription_success()
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
def handle_message(update: telegram.Update, context: CallbackContext):
message = messages.default_msg()
if update.message.reply_to_message:
if update.message.reply_to_message.text == messages.ask_state_msg():
message = messages.get_state_msg(update.message.text)
if (
update.message.reply_to_message.text
== messages.ask_state_for_districtwise_msg()
):
message = messages.get_district_msg(update.message.text)
if re.match("hi|hello|hey", update.message.text, re.IGNORECASE):
message = messages.hello_msg(update.message.chat.first_name)
elif re.match(".*count", update.message.text, re.IGNORECASE):
message = messages.get_count_msg()
elif re.match(".*state", update.message.text, re.IGNORECASE):
state(update, context)
elif re.match(".*unsubscribe", update.message.text, re.IGNORECASE):
unsubscribe(update, context)
elif re.match(".*subscribe", update.message.text, re.IGNORECASE):
subscribe(update, context)
chat_id = update.message.chat_id
context.bot.send_message(
chat_id=chat_id, text=message, parse_mode=telegram.ParseMode.HTML
)
def main():
updater = Updater(getenv("API_KEY"), use_context=True)
dp = updater.dispatcher
logging.basicConfig(
filename="bot.log",
filemode="w",
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", start))
dp.add_handler(CommandHandler("count", count))
dp.add_handler(CommandHandler("today", today))
dp.add_handler(CommandHandler("statewise", statewise))
dp.add_handler(CommandHandler("about", about))
dp.add_handler(CommandHandler("lastupdated", lastupdated))
dp.add_handler(CommandHandler("state", state))
dp.add_handler(CommandHandler("districtwise", districtwise))
dp.add_handler(CommandHandler("subscribe", subscribe))
dp.add_handler(CommandHandler("unsubscribe", unsubscribe))
dp.add_handler(MessageHandler(Filters.text, handle_message))
update_hour = 15
update_minute = 30
update_time = getenv("UPDATE_TIME")
if update_time != None:
update_hour, update_minute = map(int, update_time.split(":"))
updater.job_queue.run_daily(
daily_message,
time(hour=update_hour, minute=update_minute),
name="daily_count",
days=(0, 1, 2, 3, 4, 5, 6),
)
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()