-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
335 lines (251 loc) · 11.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
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
import asyncio
from os import getenv
from aiogram import F
from aiogram import Bot, Dispatcher, Router, types
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.filters.command import Command
from aiogram.types import Message, CallbackQuery
from aiogram.utils.markdown import hbold
from aiogram.fsm.context import FSMContext
from config import TOKEN, FEEDBACK_GROUP, ADMIN
from states import User, Admin
from data.database_setup import Database
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
import functions as func
from datetime import datetime
import json
from pytz import utc
#-----------Open files---------------------#
with open("./data/languages.json") as file:
languages = json.load(file)
user_info = func.get_user_lan_data()
dp = Dispatcher()
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
db = Database()
scheduler = AsyncIOScheduler(timezone=utc)
def schedule_jobs():
trigger_updating = CronTrigger(year="*", month="*", day_of_week="6", day="*", hour="18", minute="0", second="0")
trigger_sending = CronTrigger(year="*", month="*", day_of_week="6", day="*", hour="18", minute="30", second="0")
trigger_updating_day = CronTrigger(hour="13", minute="10", second="0")
scheduler.add_job(func.update_screenshots, trigger=trigger_updating_day, kwargs={"only_turin": True})
scheduler.add_job(func.update_screenshots, trigger=trigger_updating)
scheduler.add_job(func.send_timetable, trigger=trigger_sending, args=(bot, db, ADMIN))
##----------------------------------Telegram bot------------------------------------##
@dp.message(CommandStart())
async def command_start_handler(message: Message, state=FSMContext) -> None:
global user_info
user_id = str(message.from_user.id)
full_name = message.from_user.full_name
username = message.from_user.username
user_lan = message.from_user.language_code
if not db.user_exist(user_id):
db.insert_user(user_id=user_id, full_name=full_name, username=username)
if user_id in user_info:
user_lan = user_info[user_id]
hello_msg = func.text_update(languages[user_lan]["hello_msg"], {"name123": full_name})
await message.answer(hello_msg)
univer_btns = func.univer_btns()
await message.answer(languages[user_lan]["choose_univer"], reply_markup=univer_btns)
return
elif user_lan in languages:
func.add_user_language(user_id=user_id, language=user_lan)
user_info = func.get_user_lan_data()
hello_msg = func.text_update(languages[user_lan]["hello_msg"], {"name123": full_name})
await message.answer(hello_msg)
univer_btns = func.univer_btns()
await message.answer(languages[user_lan]["choose_univer"], reply_markup=univer_btns)
return
await ask_change_language(message)
@dp.message(Command("language"))
async def ask_change_language(message: Message) -> None:
lan_btn_data = languages["languages"]
lan_btn = func.get_inline(lan_btn_data, row=2)
await message.answer(text=languages["uz"]["choose_lan"], reply_markup=lan_btn)
@dp.message(Command("feedback"))
async def send_feedback(message: Message, state=FSMContext) -> None:
user_id = str(message.from_user.id)
user_lan = user_info[user_id]
fedback_msg = languages[user_lan]["feedback"]
back_btn = func.reply_key([languages[user_lan]["back"]])
await message.answer(fedback_msg, reply_markup=back_btn)
await state.set_state(User.feedback)
@dp.message(Command("cancel"))
async def cancel(message: Message, state: FSMContext) -> None:
await state.clear()
await message.reply("Canceled ✅")
##------------------------------callback queries---------------------#
@dp.callback_query(F.data == "stats")
async def send_stats(call: CallbackQuery):
stats = func.get_stats(database=db)
await call.message.answer(stats)
@dp.callback_query(F.data == "send_msg_all")
async def send_message_all(call: CallbackQuery, state:FSMContext):
user_id = str(call.message.chat.id)
user_lan = user_info[user_id]
msg = languages[user_lan]["ask_admin_msg"]
await call.message.answer(msg)
await state.set_state(Admin.message)
@dp.callback_query(F.data == "update_timetable")
async def update_screenshots(call: CallbackQuery):
func.update_screenshots()
await call.message.answer(f"Timetable will be updated ✅")
@dp.callback_query(F.data == "get_users_db")
async def get_user_db(call: CallbackQuery):
func.save_user_database(database=db)
user_db_path = "./data/users.csv"
user_db = types.input_file.FSInputFile(user_db_path)
await call.message.answer_document(user_db)
@dp.callback_query(F.data.startswith("change_lan_"))
async def change_language(call: CallbackQuery):
global user_info
chosen_lan = call.data.split("_")[-1]
user_id = call.message.chat.id
func.add_user_language(user_id=user_id, language=chosen_lan)
user_info = func.get_user_lan_data()
changed_success = languages[chosen_lan]["lan_changed_success"]
await call.message.edit_text(changed_success)
ask_start = languages[chosen_lan]["ask_to_start"]
await call.message.answer(ask_start)
@dp.callback_query(F.data.startswith("choose_univer_"))
async def choose_university(call: CallbackQuery, state: FSMContext):
user_id = str(call.message.chat.id)
user_lan = user_info[user_id]
univer = call.data.split("_")[-1]
db.insert_univer(user_id=user_id, university=univer)
ask_group = languages[user_lan]["ask_group"]
await call.message.edit_text(ask_group)
groups_btns = func.get_groups_btn(univer)
msg_ids = []
for group_name, btns in groups_btns.items():
msg = await call.message.answer(group_name, reply_markup=btns)
msg_ids.append(msg.message_id)
await state.set_data({"msg_ids": msg_ids})
@dp.callback_query(F.data.startswith("choose_gp&"))
async def choose_group(call: CallbackQuery, state: FSMContext):
user_id = str(call.message.chat.id)
user_lan = user_info[user_id]
group_name = call.data.split("&")[-1]
db.insert_group(user_id=user_id, group=group_name)
univer_btn_ids = await state.get_data() ##deleting last button messages
for i in univer_btn_ids["msg_ids"]:
await bot.delete_message(call.message.chat.id, i)
msg = languages[user_lan]["successfull"]
await call.message.answer(msg)
univer, group = db.get_univer_group(user_id)
photo_path = f"./screenshots/{univer}/{group}.png"
photo = types.input_file.FSInputFile(photo_path)
msg = languages[user_lan]["timetable_week"]
timetable_btn = func.reply_key([languages[user_lan]["timetable"]])
await call.message.answer_photo(photo=photo, caption=msg, reply_markup=timetable_btn)
##-------------------reply-keyboard----------------------------#
@dp.message(lambda message: message.from_user.id == ADMIN and message.text == "Admin")
async def admin_controls(message: Message) -> None:
user_id = str(message.from_user.id)
user_lan = user_info[user_id]
admin_checked = languages[user_lan]["admin_checked"]
admin_btns = func.get_inline(languages[user_lan]["admin_controls"], row=2)
await message.answer(admin_checked, reply_markup=admin_btns)
@dp.message(Admin.message)
async def get_timatable(message: Message, state: FSMContext) -> None:
all_users = db.get_all_users()
error_msgs = {}
for user in all_users:
user = user[0]
exception = await send_rassilka(message, user)
if exception:
if str(exception) not in error_msgs:
error_msgs[str(exception)] = 1
else:
error_msgs[str(exception)] += 1
msg = []
for exeption_text, number in error_msgs.items():
msg.append(f"{exeption_text}: {number}")
if len(msg) != 0:
await bot.send_message(ADMIN, "\n".join(msg))
async def send_rassilka(message, i):
try:
caption_entities = message.caption_entities
entities = message.entities
if message.content_type == "text":
# text
tex = message.text
await bot.send_message(i, tex, entities=entities)
elif message.content_type == "photo":
# photo
capt = message.caption
photo = message.photo[-1].file_id
await bot.send_photo(i, photo, caption=capt, caption_entities=caption_entities)
elif message.content_type == "video":
# video
capt = message.caption
photo = message.video.file_id
await bot.send_video(i, photo, caption=capt, caption_entities=caption_entities)
elif message.content_type == "audio":
# audio
capt = message.caption
photo = message.audio.file_id
await bot.send_audio(i, photo, caption=capt, caption_entities=caption_entities)
elif message.content_type == "voice":
# voice
capt = message.caption
photo = message.voice.file_id
await bot.send_voice(i, photo, caption=capt, caption_entities=caption_entities)
elif message.content_type == "animation":
# animation
capt = message.caption
photo = message.animation.file_id
await bot.send_animation(i, photo, caption=capt, caption_entities=caption_entities)
elif message.content_type == "document":
# document
capt = message.caption
photo = message.document.file_id
await bot.send_document(i, photo, caption=capt, caption_entities=caption_entities)
elif message.content_type == "video_note":
# rounded video
video = message.video_note.file_id
await bot.send_video_note(i, video)
return
except Exception as e:
return e
@dp.message(lambda message: message.text in languages["timetable"])
async def get_timatable(message: Message) -> None:
user_id = str(message.from_user.id)
user_lan = user_info[user_id]
univer, group_name = db.get_univer_group(user_id)
photo_path = f"./screenshots/{univer}/{group_name}.png"
photo = types.input_file.FSInputFile(photo_path)
msg = languages[user_lan]["timetable_week"]
await message.answer_photo(photo=photo, caption=msg)
@dp.message(User.feedback)
async def test(message: Message, state=FSMContext):
user_id = str(message.from_user.id)
user_lan = user_info[user_id]
if message.text in languages["back"]:
await state.clear()
timetable_btn = func.reply_key([languages[user_lan]["timetable"]])
await message.reply("Ok", reply_markup=timetable_btn)
return
await bot.forward_message(chat_id=FEEDBACK_GROUP, from_chat_id=message.chat.id, message_id=message.message_id)
await bot.send_message(chat_id=FEEDBACK_GROUP, text=user_id)
await message.reply(languages[user_lan]["send"])
@dp.message(lambda message: message.chat.id == FEEDBACK_GROUP)
async def answer_feedbacks(message: types.Message):
try:
if message.from_user.id == ADMIN:
to_user = message.reply_to_message.text
await bot.send_message(to_user, message.text)
else:
await message.reply("You are not an admin.")
except:
return
async def on_startup():
schedule_jobs()
async def main() -> None:
scheduler.start()
func.update_screenshots()
await on_startup()
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())