-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
251 lines (221 loc) · 13.4 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
import logging
from tools import *
from telegram import ReplyKeyboardMarkup
from telegram.ext import Application, MessageHandler, filters, CommandHandler
from config import BOT_TOKEN
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
async def admin(update, context):
user = update.effective_user
if not await is_admin(user):
await update.message.reply_text(f'У вас нет доступа к этой команде.')
return None
admin_commands = [['/add_users', ''],
['', '']]
admin_keyboard = ReplyKeyboardMarkup(admin_commands, one_time_keyboard=True)
await update.message.reply_text('Выполнено, выдал вам клавиатуру администратора'
'', reply_markup=admin_keyboard)
async def add_users(update, context):
# Оповещает пользователей, которых добавили в bd в какой-то класс.
user = update.effective_user
if await is_user(user) != 'done':
await start(update, context)
return None
if not await is_admin(user):
await update.message.reply_text(f'У вас нет доступа к этой команде.')
return None
users = await connect_db('''SELECT tg_id FROM users where update_form = 1''')
for user_id in users:
user_class, user_school = (await connect_db(f'''SELECT class.name, schools.name FROM users left join class
on users.class = class.id left join schools on class.school_id = schools.id where tg_id = {user_id[0]}'''))[0]
if user_class is None or user_school is None:
continue
await context.bot.send_message(chat_id=user_id[0], text=f"""
Верификация пройдена! Образовательная площадка:
"{user_school}" приняла тебя в своей системе.
Теперь у тебя есть доступ функционалу твоего класса: "{user_class}"!""")
await connect_db(f'''UPDATE users
SET update_form = 0
WHERE tg_id = {user_id[0]} ''', )
if len(users) > 0:
await update.message.reply_text(f'Выполнено, оповестил всех новых учеников. ({len(users)} человек)')
else:
await update.message.reply_text(f'Запрос был выполнен, но нашлось лишь {len(users)} человек')
async def start(update, context):
user = update.effective_user
if await is_user(user):
if await is_user(user) == "wait":
await update.message.reply_html(rf"""Привет {user.mention_html()}!
Твоя заявка {user.id} находится на рассмотрении.""")
elif await is_user(user) == 'done':
await update.message.reply_html(rf"""{user.mention_html()}, вы уже прошли верификацию.""")
else:
await update.message.reply_html(f"""Привет {user.mention_html()}!
Для продолжения нашего взаимодействия, попроси куратора твоего образовательного учреждения добавить
тебя в нашу платформу. Не забудь указать id своего обращения(id: {user.id}) либо свой telegram id""")
async def token_connection(update, context):
token = context.args
if len(token) == 1:
if token[0].isdigit():
if await connect_db(f'''SELECT class_id
FROM tokens
WHERE token_id = {token[0]};'''):
class_id = (await connect_db(f'''SELECT class_id
FROM tokens
WHERE token_id = {token[0]};'''))[0][0]
await connect_db(f'''UPDATE users
SET class = {class_id}
WHERE tg_id = {update.effective_user.id} ''')
user_class, user_school = (await connect_db(f'''SELECT class.name, schools.name FROM users left join
class on users.class = class.id left join schools on class.school_id = schools.id where tg_id =
{update.effective_user.id}'''))[0]
await update.message.reply_text(f"""
Верификация пройдена! Образовательная площадка:
"{user_school}" приняла тебя в своей системе.
Теперь у тебя есть доступ функционалу твоего класса: "{user_class}"!""", reply_markup=default_keyboard)
else:
await update.message.reply_text('''
Ошибка, такого токена я не знаю, проверь, токен должен быть введен в одну строчку:
пример /token number
Где number - число, в вашем случае, number не являлся числом.''')
elif token:
await update.message.reply_text('''
Ошибка, такого токена я не знаю, проверь, токен должен быть введен в одну строчку:
пример /token number
Обратите внимание на написание токена в одну строчку!''')
else:
await update.message.reply_text('''
Ошибка, такого токена я не знаю, проверь, токен должен быть введен в одну строчку:
пример /token number
К сожалению вы отправили пустой токен.''')
async def today(update, context):
user = update.effective_user
if await is_user(user) != 'done':
await start(update, context)
await update.message.reply_text(await make_time_table(await get_class_id(user), await to_day()),
reply_markup=default_keyboard)
async def tomorrow(update, context):
user = update.effective_user
if await is_user(user) != 'done':
await start(update, context)
await update.message.reply_text(await make_time_table(await get_class_id(user), await to_day() + 1),
reply_markup=default_keyboard)
async def dep_to(update, context):
message = context.args
user = update.effective_user
if await is_user(user) != 'done':
await start(update, context)
if not await is_admin(user):
await update.message.reply_text(f'У вас нет доступа к этой команде.')
return None
if len(message) >= 2:
print(message)
if ':' not in update.message.text:
await update.message.reply_text('''Ошибка, такого запроса я не знаю.
Проверьте, написание сообщения для обучающихся по шаблону /dep_to {класс/классы}: {сообщение}
Пример: /dep_to 11А: Завтра будет олимпиада!
Для оповещения сразу всех учеников используйте команду /dep_to all: {сообщение}
Пример: /dep_to all: Наша школа самая сильная!
В вашем запросе не нашёлся символ ":"''')
return None
else:
class_alt_name = update.message.text.replace('/dep_to ', '', 1).replace(' ', '').split(':')[0].split(',')
class_alt_name[-1] = class_alt_name[-1].replace(':', '', 1)
user_school = (await connect_db(f'''SELECT schools.id FROM users
left join class on users.class = class.id
left join schools on class.school_id = schools.id
where users.tg_id = {update.effective_user.id}'''))[0][0]
if len(class_alt_name) == 1:
class_alt_name = list(class_alt_name)
if class_alt_name[0].lower() == 'all':
class_alt_name = list(await connect_db(f'''SELECT class.local_alt_name FROM users
left join class on users.class = class.id
left join schools on class.school_id = schools.id
where schools.id = {user_school}'''))
class_alt_name = set(class_alt_name)
print(class_alt_name)
for class_id in class_alt_name:
if class_id:
if len(class_id[0]) > 1:
class_id = class_id[0]
print(class_id)
is_error = False
if not (await connect_db(f'''SELECT tg_id FROM users
left join class on users.class = class.id
left join schools on class.school_id = schools.id
where schools.id = {user_school} and class.local_alt_name = '{class_id}'
''')):
if not is_error:
is_error = True
await update.message.reply_text(
f'Не удалось отправить оповещение, так как "{class_id}" класс отсутствует в системе')
for user_id in (await connect_db(f'''SELECT tg_id FROM users
left join class on users.class = class.id
left join schools on class.school_id = schools.id
where schools.id = {user_school} and class.local_alt_name = '{class_id}'
''')):
user_id = user_id[0]
message = update.message.text.replace('/dep_to', 'to', 1)
await context.bot.send_message(chat_id=user_id, text=f"Сообщение от {user_id}:\n{message}")
else:
await update.message.reply_text('''Ошибка, такого запроса я не знаю.
Проверьте, написание сообщения для обучающихся по шаблону /dep_to {класс/классы}: {сообщение}
Пример: /dep_to 11А: Завтра будет олимпиада!
Для оповещения сразу всех учеников используйте команду /dep_to all: {сообщение}
Пример: /dep_to all: Наша школа самая сильная!''')
async def days_to_ege(update, context):
user = update.effective_user
if await is_user(user) != 'done':
await start(update, context)
schedule = await get_schedule_from_server()
await update.message.reply_text(schedule, reply_markup=default_keyboard)
async def text(update, context):
user = update.effective_user
if await is_user(user) != 'done':
await start(update, context)
else:
if update.message.text.lower() in ['на сегодня', 'сегодня', 'сейчас', 'расписание']:
await update.message.reply_text(await make_time_table(await get_class_id(user), await to_day()),
reply_markup=default_keyboard)
elif update.message.text.lower() in ['на завтра', 'завтра']:
await update.message.reply_text(await make_time_table(await get_class_id(user), (await to_day() + 1)),
reply_markup=default_keyboard)
elif update.message.text.lower() == 'воскресенье':
await update.message.reply_text(await make_time_table(await get_class_id(user), 0),
reply_markup=default_keyboard)
elif update.message.text.lower() == 'понедельник':
await update.message.reply_text(await make_time_table(await get_class_id(user), 1),
reply_markup=default_keyboard)
elif update.message.text.lower() == 'вторник':
await update.message.reply_text(await make_time_table(await get_class_id(user), 2),
reply_markup=default_keyboard)
elif update.message.text.lower() == 'среда':
await update.message.reply_text(await make_time_table(await get_class_id(user), 3),
reply_markup=default_keyboard)
elif update.message.text.lower() == 'четверг':
await update.message.reply_text(await make_time_table(await get_class_id(user), 4),
reply_markup=default_keyboard)
elif update.message.text.lower() == 'пятница':
await update.message.reply_text(await make_time_table(await get_class_id(user), 5),
reply_markup=default_keyboard)
elif update.message.text.lower() == 'суббота':
await update.message.reply_text(await make_time_table(await get_class_id(user), 6),
reply_markup=default_keyboard)
else:
await update.message.reply_text('Я не совсем понимаю', reply_markup=default_keyboard)
def main():
application = Application.builder().token(BOT_TOKEN).build()
text_handler = MessageHandler(filters.TEXT, text)
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("admin", admin))
application.add_handler(CommandHandler("add_users", add_users))
application.add_handler(CommandHandler("token", token_connection))
application.add_handler(CommandHandler("today", today))
application.add_handler(CommandHandler("tomorrow", tomorrow))
application.add_handler(CommandHandler("dep_to", dep_to))
application.add_handler(CommandHandler("days_to_ege", days_to_ege))
application.add_handler(text_handler)
application.run_polling()
if __name__ == '__main__':
main()