-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
446 lines (368 loc) · 13.9 KB
/
main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import functools
import os
import time
from difflib import get_close_matches
from telebot import TeleBot
from telebot.apihelper import ApiException
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup
from telebot.types import ReplyKeyboardMarkup, ReplyKeyboardRemove
import utils
from config import DEFAULT_TIME_SET
from models import Group, Student
from templates import chair_info, notify_template, time_menu_template
token = os.environ.get("BOT_TOKEN")
bot = TeleBot(token)
bot.skip_pending = True
@bot.message_handler(func=lambda m: utils.redis_storage.get(m.chat.id) == b"set_group")
@utils.throttle(time=1)
def handle_group(message):
user = message.from_user.id
group = message.text.upper()
if utils.get_or_create_group(group):
utils.redis_storage.delete(user)
Student.set_group(group_code=group, student_id=user)
group_full = Group.get_group_full_name(group)
bot.send_message(user, text=utils.set_group_message.format(group_full, group))
return send_buttons(message)
return suggest(message, group, utils.get_cached_groups())
@bot.message_handler(
commands=["dispatch"], func=lambda m: m.from_user.id == utils.ADMIN_ID
)
@utils.in_thread
def handle_dispatch(message):
data = message.text.split(maxsplit=1)
if len(data) != 2:
return bot.send_message(message.chat.id, text="Incorrect input.")
return run_dispatch(message, data[1])
def run_dispatch(message, content):
receivers = 0
dispatch_format = "<b>Run dispatch</b>\n<pre>{}/{}</pre>"
users = Student.select()
response = bot.send_message(
message.chat.id,
text=dispatch_format.format(receivers, len(users)),
parse_mode="html",
)
progress = functools.partial(
bot.edit_message_text,
chat_id=response.chat.id,
message_id=response.message_id,
parse_mode="html",
)
dispatch = functools.partial(bot.send_message, text=content, parse_mode="html")
for user in users:
try:
dispatch(user.student_id)
receivers += 1
if not receivers % 100:
progress(text=dispatch_format.format(receivers, len(users)))
except ApiException as e:
print(e)
time.sleep(0.4)
progress(text=dispatch_format.format(receivers, len(users)) + "\n<b>Successful</b>")
def get_cancel_button():
markup = InlineKeyboardMarkup()
markup.add(InlineKeyboardButton(text="Відміна", callback_data="cancel"))
return markup
def wait_for_group(message):
user = message.from_user.id
utils.redis_storage.set(user, "set_group")
return bot.send_message(
user, text="Відправте шифр вашої групи:", reply_markup=get_cancel_button()
)
@bot.callback_query_handler(func=lambda call: call.data == "cancel")
def cancel(call):
user = call.from_user.id
utils.redis_storage.delete(user)
return bot.edit_message_text(
chat_id=user, message_id=call.message.message_id, text="Відмінено"
)
@bot.message_handler(commands=["set"])
@utils.throttle()
def set_group_command(message):
return wait_for_group(message)
@bot.message_handler(commands=["get"])
@utils.throttle()
@utils.group_required(wait_for_group)
def get_my_group(message, *args):
user = message.from_user.id
desc = Student.get_group_desc(message.chat.id)
return bot.send_message(user, text=utils.group_info.format(**desc))
def create_notify_keyboard(user):
keyboard = InlineKeyboardMarkup()
user_notify_status = Student.get_user_notify_status(user)
turn = "Увімкнути" if not user_notify_status else "Вимкнути"
notify_status_btn = InlineKeyboardButton(
text=turn, callback_data="change_notify_status"
)
set_time_btn = InlineKeyboardButton(text="Встановити час", callback_data="set_time")
close_btn = InlineKeyboardButton(text="Закрити", callback_data="close")
keyboard.add(notify_status_btn, set_time_btn)
keyboard.add(close_btn)
return keyboard
def create_time_buttons():
keyboard = InlineKeyboardMarkup()
keyboard.add(
*[
InlineKeyboardButton(text=_time, callback_data=f"time_{_time}")
for _time in DEFAULT_TIME_SET
]
)
keyboard.add(
InlineKeyboardButton(text="Назад", callback_data="back"),
InlineKeyboardButton(text="Відміна", callback_data="cancel"),
)
return keyboard
@bot.callback_query_handler(func=lambda call: call.data == "back")
def back_to_notify(call):
utils.redis_storage.delete(call.from_user.id)
return make_notification_menu(
message=call.message,
action=bot.edit_message_text,
message_id=call.message.message_id,
)
@bot.message_handler(commands=["notify"])
@utils.throttle()
@utils.group_required(wait_for_group)
def notification_menu(message, *args):
return make_notification_menu(message)
def make_notification_menu(message, action=bot.send_message, **kwargs):
user = message.chat.id
notify_time = Student.get_notify_time(user)
action(
chat_id=user,
text=notify_template.format(notify_time or "Не вказано"),
reply_markup=create_notify_keyboard(user),
parse_mode="html",
**kwargs,
)
@bot.message_handler(func=lambda m: utils.redis_storage.get(m.chat.id) == b"set_time")
@utils.throttle(time=1)
def handle_notify_time(message):
user = message.chat.id
time = utils.validate_time(message.text)
if not time:
bot.send_message(
chat_id=user,
text="Хибний формат часу, спробуйте знову:",
reply_markup=get_cancel_button(),
)
else:
Student.set_notify_time(user, time)
bot.delete_message(
chat_id=user, message_id=utils.redis_storage.get(f"{user}::time_id")
)
bot.send_message(
chat_id=user,
text=f"Тепер Ви будете отримувати сповіщення о <b>{time.time()}</b>.",
parse_mode="html",
)
utils.redis_storage.delete(user)
@bot.callback_query_handler(func=lambda call: call.data.startswith("time"))
def handle_default_time(call):
time = call.data.split("_")[1]
user = call.from_user.id
Student.set_notify_time(user, time)
bot.edit_message_text(
chat_id=user,
text=f"Тепер Ви будете отримувати сповіщення о <b>{time}:00</b>.",
message_id=call.message.message_id,
parse_mode="html",
)
utils.redis_storage.delete(user)
@bot.callback_query_handler(func=lambda call: call.data == "set_time")
def set_notify_time_menu(call):
user = call.from_user.id
msg_id = bot.edit_message_text(
message_id=call.message.message_id,
chat_id=user,
reply_markup=create_time_buttons(),
text=time_menu_template,
)
utils.redis_storage.set(user, "set_time")
utils.redis_storage.set(f"{user}::time_id", msg_id.message_id)
@bot.callback_query_handler(func=lambda call: call.data == "change_notify_status")
def change_notify_status(call):
user = call.from_user.id
Student.trigger_notify(user)
bot.edit_message_reply_markup(
user,
message_id=call.message.message_id,
reply_markup=create_notify_keyboard(user),
)
def get_chair_status_message(user):
current_state = Student.get_extend_flag(user)
return chair_info.format(current_state)
def settings_buttons():
markup = InlineKeyboardMarkup()
markup.add(
InlineKeyboardButton(text="Змінити", callback_data="change_chair"),
InlineKeyboardButton(text="Закрити", callback_data="close"),
)
return markup
@bot.message_handler(commands=["chair"])
@utils.throttle()
@utils.group_required(wait_for_group)
def get_change_chair_dialog(message, *args):
user = message.from_user.id
bot.send_message(
user,
text=get_chair_status_message(user),
reply_markup=settings_buttons(),
parse_mode="html",
)
@bot.callback_query_handler(func=lambda call: call.data == "change_chair")
def reset_chair_status(call):
user = call.from_user.id
Student.reset_extended_flag(user)
bot.edit_message_text(
chat_id=user,
message_id=call.message.message_id,
text=get_chair_status_message(user),
parse_mode="html",
reply_markup=settings_buttons(),
)
@bot.callback_query_handler(func=lambda call: call.data == "close")
def close_chair_dialog(call):
user = call.from_user.id
bot.edit_message_text(chat_id=user, message_id=call.message.message_id, text=":)")
@bot.message_handler(commands=["start"])
@utils.throttle()
@utils.group_required(wait_for_group)
def greeting(message, *args):
return send_buttons(message)
@bot.message_handler(commands=["suggest"])
def suggest_future(message):
return NotImplementedError
def reached_requests_limit_markup(message):
bot.send_message(
chat_id=message.chat.id, text="Ви вичерпали ліміт запитів на сьогодні."
)
@bot.message_handler(commands=["info"])
@utils.throttle()
def get_stats(message):
user = message.from_user.id
requests_count = utils.REQUESTS_LIMIT_PER_DAY - int(utils.get_requests_count(user))
bot.send_message(
user,
parse_mode="html",
disable_web_page_preview=True,
text=utils.info_message.format(len(Student.select()), requests_count),
)
@bot.message_handler(func=lambda m: m.text in utils.DAYS)
@utils.throttle()
@utils.limit_requests(reached_requests_limit_markup)
@utils.in_thread
@utils.group_required(wait_for_group)
def send_schedule(message, user, group):
extended_flag = Student.get_extend_flag(user)
bot.send_message(
user,
text=utils.get_schedule(message.text, group, bot, user, extended_flag),
reply_to_message_id=message.message_id,
parse_mode="html",
disable_web_page_preview=True,
)
utils.track(user, "Get schedule")
def reached_limit_alert(callback):
bot.edit_message_text(
chat_id=callback.message.chat.id,
message_id=callback.message.message_id,
text="Ви вичерпали ліміт запитів на сьогодні.",
)
@bot.callback_query_handler(func=lambda call: call.data.startswith("weekday"))
@utils.limit_requests(reached_limit_alert)
@utils.in_thread
@utils.group_required(wait_for_group)
def handle_weekday(call, user, group):
day = call.data.split("_")[1]
extended_flag = Student.get_extend_flag(user)
keyboard = InlineKeyboardMarkup()
keyboard.add(InlineKeyboardButton(text="Назад", callback_data="back_weekdays"))
bot.edit_message_text(
chat_id=user,
message_id=call.message.message_id,
text=utils.week_day_schedule(
utils.get_correct_day(int(day)), group, extended_flag
),
parse_mode="html",
reply_markup=keyboard,
disable_web_page_preview=True,
)
utils.track(user, "Weekday schedule")
@bot.callback_query_handler(func=lambda call: call.data == "back_weekdays")
def back_to_weekdays(call):
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text="Дні тижня:",
reply_markup=build_weekdays_buttons(),
)
@bot.message_handler(commands=["date"])
@utils.throttle()
@utils.limit_requests(reached_requests_limit_markup)
@utils.in_thread
@utils.group_required(wait_for_group)
def certain_date(message, user, group):
splited = message.text.split()
bot.send_chat_action(user, "typing")
if len(splited) == 2:
extended_flag = Student.get_extend_flag(user)
bot.send_message(
user,
text=utils.from_string(splited[1], group, extended_flag),
reply_to_message_id=message.message_id,
parse_mode="html",
disable_web_page_preview=True,
)
else:
bot.reply_to(message, text="Хибний формат команди.")
utils.track(user, "Get schedule")
@bot.message_handler(regexp="Вказати конкретну дату")
@utils.throttle()
def send_tip(message):
bot.reply_to(message, text=utils.tip_message)
@bot.callback_query_handler(func=lambda call: call.data == "how_it_works")
def how_it_works(call):
keyboard = InlineKeyboardMarkup()
keyboard.add(InlineKeyboardButton(text="Назад", callback_data="back_weekdays"))
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text=utils.weekdays_info,
reply_markup=keyboard,
)
def build_weekdays_buttons():
keyboard = InlineKeyboardMarkup(row_width=2)
buttons = [
InlineKeyboardButton(text=name, callback_data=f"weekday_{call}")
for call, name in enumerate(utils.DAY_NAMES)
]
buttons.append(InlineKeyboardButton(text="Закрити", callback_data="close"))
buttons.append(
InlineKeyboardButton(text="Як це працює?", callback_data="how_it_works")
)
keyboard.add(*buttons)
return keyboard
@bot.message_handler(regexp="Дні тижня")
@utils.throttle()
def weekdays(message):
bot.send_message(
message.chat.id, text="Дні тижня:", reply_markup=build_weekdays_buttons()
)
def send_buttons(message):
markup = ReplyKeyboardMarkup(resize_keyboard=True)
markup.add(*utils.DAYS)
markup.add("Вказати конкретну дату", "Дні тижня")
bot.send_message(message.from_user.id, text="Меню", reply_markup=markup)
def suggest(message, group, groups):
markup = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
matches = get_close_matches(group, groups, n=25, cutoff=0.5)
if matches:
markup.add(*matches)
return bot.send_message(
message.chat.id, text=utils.suggest_message, reply_markup=markup
)
return bot.send_message(
message.chat.id, text=utils.group_not_found, reply_markup=ReplyKeyboardRemove()
)