-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.py
162 lines (115 loc) · 6.75 KB
/
handlers.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
from os import path
from aiogram import types
from loader import dp, db
from states import states
import keyboards as keyboards_markup
from data import constants, bot_text
from utils import time_out_of_bounds
from aiogram.dispatcher.filters import Command, Text
from aiogram.dispatcher import FSMContext
@dp.message_handler(Command(bot_text.COMMAND_START))
async def show_menu(message: types.Message):
await message.answer(bot_text.START_TEXT, parse_mode='Markdown')
await message.answer(text=bot_text.SET_UP_PATH, reply_markup=keyboards_markup.set_up_path)
# Help command handler
@dp.message_handler(Text(equals=[bot_text.BUTTON_HELP]))
async def process_start_command(message: types.Message):
await message.reply(text=bot_text.HELP_TEXT, parse_mode='Markdown')
await message.answer(text=bot_text.SET_UP_PATH, reply_markup=keyboards_markup.out_test_managing)
# Set up path command & user registration in db
@dp.message_handler(Text(equals=[bot_text.BUTTON_SET_PATH, bot_text.BUTTON_RESET_PATH]))
async def cmd_dialog_stream(message: types.Message):
await states.PytestPath.pytest_path.set() # states start working
await message.reply(text=bot_text.SETTING_PATH_INSTRUCTION, reply=False)
@dp.message_handler(state=states.PytestPath.pytest_path)
async def testing(message: types.Message, state: FSMContext):
async with state.proxy() as data:
data['text'] = message.text
user_path = data['text']
if not path.exists(user_path):
await message.answer(text=bot_text.INVALID_PATH, reply_markup=keyboards_markup.out_test_managing)
dp.current_state(user=message.from_user.id)
await state.reset_state()
else:
# updating path
db.add_user(message.from_user.id)
db.update_user(message.from_user.id, user_path=user_path)
# Path successfully set
await message.answer(text=bot_text.SETTING_PATH_SUCCESS, reply_markup=keyboards_markup.out_test_managing)
dp.current_state(user=message.from_user.id) # Close state
await state.reset_state()
# Start testing command
@dp.message_handler(Text(equals=[bot_text.BUTTON_START_TESTING]))
async def start_testing(message: types.Message):
db.add_user(message.from_user.id)
user_path = db.get_user_path(message.from_user.id)
if user_path is None:
await message.answer(text=bot_text.INVALID_PATH, reply_markup=keyboards_markup.out_test_managing)
else:
db.update_user(user_id=message.from_user.id, status=True)
await message.answer(text=bot_text.START_TESTING_INSTRUCTION_1)
await message.answer(text=bot_text.START_TESTING_INSTRUCTION_2)
await message.answer(text=bot_text.START_TESTING_INSTRUCTION_3, reply_markup=keyboards_markup.in_test_managing)
# Stop testing command
@dp.message_handler(Text(equals=[bot_text.BUTTON_END_TESTING]))
async def stop_testing(message: types.Message):
db.add_user(message.from_user.id)
db.update_user(user_id=message.from_user.id, status=False)
await message.answer(text=bot_text.STOP_TESTING, reply_markup=keyboards_markup.out_test_managing)
# Set notification period & user registration in db
@dp.message_handler(Text(equals=[bot_text.BUTTON_EDIT_FAILURE_PERIOD, bot_text.BUTTON_EDIT_NOTIFICATIONS_PERIOD]))
async def cmd_dialog_notifications(message: types.Message):
await states.Period.period.set()
if message.text == bot_text.BUTTON_EDIT_FAILURE_PERIOD:
await message.reply(text=bot_text.FAILURE_PERIOD_INSTRUCTION_1)
await message.reply(text=bot_text.FAILURE_PERIOD_INSTRUCTION_2)
else:
await message.reply(text=bot_text.NOTIFICATION_PERIOD_INSTRUCTION_1)
await message.reply(text=bot_text.NOTIFICATION_PERIOD_INSTRUCTION_2)
@dp.message_handler(state=states.Period.period)
async def update_period(message: types.Message, state: FSMContext):
async with state.proxy() as data:
data['text'] = message.text
try:
period_method, time = data['text'].split()
if period_method not in [bot_text.CHANGE_NOTIFICATION_PERIOD, bot_text.CHANGE_FAILURE_PERIOD]:
raise ValueError
time = int(time)
if time < constants.SLEEP_TIME or time > constants.TIME_LIMIT:
await time_out_of_bounds(message, state)
else:
db.add_user(message.from_user.id)
if period_method == bot_text.CHANGE_NOTIFICATION_PERIOD:
db.update_user(user_id=message.from_user.id, notifications_period=time)
else:
db.update_user(user_id=message.from_user.id, failures_period=time)
await message.answer(text=bot_text.UPDATE_PERIOD_SUCCESS,
reply_markup=keyboards_markup.out_test_managing)
dp.current_state(user=message.from_user.id)
await state.reset_state()
except ValueError:
# if time input cannot be converted to integer
await message.answer(text=bot_text.UPDATE_PERIOD_FAILURE, reply_markup=keyboards_markup.out_test_managing)
dp.current_state(user=message.from_user.id)
await state.reset_state()
# Mute failure notifications command
@dp.message_handler(Text(equals=[bot_text.BUTTON_MUTE_FAILURE_NOTIFICATIONS]))
async def mute(message: types.Message):
db.update_user(user_id=message.from_user.id, failure_mute=False)
await message.answer(text=bot_text.MUTED_FAILURE_NOTIFICATIONS, reply_markup=keyboards_markup.in_test_managing)
# Unmute alert notifications command
@dp.message_handler(Text(equals=[bot_text.BUTTON_UNMUTE_FAILURE_NOTIFICATIONS]))
async def unmute(message: types.Message):
db.update_user(user_id=message.from_user.id, failure_mute=False)
await message.answer(text=bot_text.LOUD_FAILURE_NOTIFICATIONS, reply_markup=keyboards_markup.in_test_managing)
# Not to detect failures command (only scheduled notifications)
@dp.message_handler(Text(equals=[bot_text.BUTTON_GET_ONLY_SCHEDULED_NOTIFICATIONS]))
async def stop_failure(message: types.Message):
db.update_user(user_id=message.from_user.id, detect_failures=False)
await message.answer(text=bot_text.GET_SCHEDULED_NOTIFICATIONS_ANSWER,
reply_markup=keyboards_markup.in_test_managing)
# Detect failures command (all notifications)
@dp.message_handler(Text(equals=[bot_text.BUTTON_GET_ALL_NOTIFICATIONS]))
async def start_failures(message: types.Message):
db.update_user(user_id=message.from_user.id, detect_failures=True)
await message.answer(text=bot_text.GET_ALL_NOTIFICATIONS_ANSWER, reply_markup=keyboards_markup.in_test_managing)