-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_utils.py
113 lines (92 loc) · 4.03 KB
/
bot_utils.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
import asyncio
import aiogram
from aiogram import exceptions
from logging import getLogger
import config
from texts import Buttons, Commands
from db import get_all_courses, User, get_or_create
log = getLogger(__name__)
async def send_message(user_id: int, text: str, bot, disable_notification: bool = False) -> bool:
"""
Safe messages sender
:param bot:
:param user_id:
:param text:
:param disable_notification:
:return:
"""
# TODO Логика если сообщение не пролазит по длине
try:
await bot.send_message(user_id, text, disable_notification=disable_notification)
except exceptions.BotBlocked:
log.error(f"Target [ID:{user_id}]: blocked by user")
except exceptions.ChatNotFound:
log.error(f"Target [ID:{user_id}]: invalid user ID")
except exceptions.RetryAfter as e:
log.error(f"Target [ID:{user_id}]: Flood limit is exceeded. Sleep {e.timeout} seconds.")
await asyncio.sleep(e.timeout)
return await send_message(user_id, text, bot, disable_notification)
except exceptions.UserDeactivated:
log.error(f"Target [ID:{user_id}]: user is deactivated")
except exceptions.TelegramAPIError:
log.exception(f"Target [ID:{user_id}]: failed")
else:
log.info(f"Target [ID:{user_id}]: success")
return True
return False
def get_user_settings(from_user): # TODO
pass
def get_courses_keyboard(user=None, session=None, additional_buttons=None):
"""Get keyboard with user courses. If user is None, then return all courses"""
if additional_buttons is None:
additional_buttons = []
keyboard = aiogram.types.ReplyKeyboardMarkup()
for but in additional_buttons:
keyboard.add(aiogram.types.KeyboardButton(but))
if user is None:
courses = get_all_courses()
log.debug(f"Get all courses for user: {user}, courses: {courses}")
if not courses:
raise ValueError("No courses found")
for course in courses:
keyboard.add(aiogram.types.KeyboardButton(course.name))
else:
if isinstance(user, aiogram.types.User):
user = get_or_create(session=session, model=User, create=False, telegram_id=user.id)
for course in user.courses:
keyboard.add(aiogram.types.KeyboardButton(course.name))
return keyboard
def back_keyboard():
keyboard = aiogram.types.ReplyKeyboardMarkup()
keyboard.add(aiogram.types.KeyboardButton(Buttons.Menu.menu))
return keyboard
def get_menu_keyboard():
keyboard = aiogram.types.ReplyKeyboardMarkup()
keyboard.add(aiogram.types.KeyboardButton(Buttons.Menu.menu_settings))
keyboard.add(aiogram.types.KeyboardButton(Buttons.Menu.menu_notification))
keyboard.add(aiogram.types.KeyboardButton(Buttons.Menu.menu_pair_call))
keyboard.add(aiogram.types.KeyboardButton(Buttons.Menu.menu_feedback))
return keyboard
def get_notification_keyboard(from_user):
keyboard = aiogram.types.ReplyKeyboardMarkup()
keyboard.add(aiogram.types.KeyboardButton(Buttons.Notification.on_notification))
keyboard.add(aiogram.types.KeyboardButton(Buttons.Notification.off_notification))
keyboard.add(aiogram.types.KeyboardButton(Buttons.Menu.menu))
return keyboard
def get_call_type_keyboard(from_user):
keyword = aiogram.types.ReplyKeyboardMarkup()
keyword.add(aiogram.types.KeyboardButton(Buttons.PairCall.global_pair_call_button))
keyword.add(aiogram.types.KeyboardButton(Buttons.PairCall.course_pair_call_button))
keyword.add(aiogram.types.KeyboardButton(Commands.kick_all_calls))
keyword.add(aiogram.types.KeyboardButton(Buttons.Menu.menu))
return keyword
def restart_keyboard():
keyboard = aiogram.types.ReplyKeyboardMarkup()
keyboard.add(aiogram.types.KeyboardButton(Buttons.Settings.settings_restart))
keyboard.add(aiogram.types.KeyboardButton(Buttons.Menu.menu))
return keyboard
async def close(loop, dp, bot):
loop.stop()
await dp.storage.close()
await dp.storage.wait_closed()
await bot.session.close()