135 lines
4.6 KiB
Python
135 lines
4.6 KiB
Python
import asyncio
|
|
from datetime import datetime, timedelta
|
|
|
|
import environs
|
|
from aiogram import Bot, Dispatcher, F
|
|
from aiogram.client.default import DefaultBotProperties
|
|
from aiogram.enums import ParseMode
|
|
from aiogram.filters import Command
|
|
from aiogram.types import Message
|
|
|
|
from additional_functions import (
|
|
off_notification,
|
|
on_notification,
|
|
set_group,
|
|
timetable_day,
|
|
timetable_for_day,
|
|
)
|
|
from DB import UseDB
|
|
from use_database import detect_group_to_update
|
|
|
|
env = environs.Env()
|
|
env.read_env()
|
|
TOKEN = env.str('TOKEN')
|
|
dp = Dispatcher()
|
|
|
|
|
|
@dp.message(Command("start"))
|
|
async def command_start(msg: Message):
|
|
await msg.answer(
|
|
"Приветствую!\nЭтот бот предназначен для просмотра расписания занятий "
|
|
"для любой группы СибГУ, включая сегодня, завтра и послезавтра.\n"
|
|
"Для начала, пожалуйста, укажите номер вашей группы с помощью команды "
|
|
"/set_group номер_группы.\nНомер группы доступен по ссылке: "
|
|
"timetable.pallada.sibsau.ru/timetable/group/номер_группы.\n"
|
|
"После настройки вы сможете легко узнать расписание на интересующий вас день.\n"
|
|
"Также предоставляется возможность подписки на уведомления о расписании, "
|
|
"используя команду /on_notification часы:минуты, чтобы получать напоминания в "
|
|
"удобное для вас время."
|
|
)
|
|
|
|
|
|
@dp.message(Command("set_group"))
|
|
async def command_set_group(msg: Message):
|
|
if len(msg.text.split()) == 2:
|
|
await set_group(msg)
|
|
else:
|
|
await msg.answer(
|
|
"Введённая вами команда содержит ошибку. "
|
|
"Правильный формат команды: /set_group номер_группы."
|
|
)
|
|
|
|
|
|
@dp.message(Command("on_notification"))
|
|
async def command_on_notification(msg: Message):
|
|
if len(msg.text.split()) == 2:
|
|
await on_notification(msg)
|
|
else:
|
|
await msg.answer(
|
|
"Введённая вами команда содержит ошибку. "
|
|
"Правильный формат команды: /on_notification часы:минуты."
|
|
)
|
|
|
|
|
|
@dp.message(Command("off_notification"))
|
|
async def command_off_notification(msg: Message):
|
|
await off_notification(msg)
|
|
|
|
|
|
@dp.message(F.text == "сегодня")
|
|
@dp.message(Command("today"))
|
|
async def command_today(msg: Message):
|
|
await timetable_for_day(msg)
|
|
|
|
|
|
@dp.message(F.text == "завтра")
|
|
@dp.message(Command("tomorrow"))
|
|
async def command_tomorrow(msg: Message):
|
|
await timetable_for_day(msg, 1)
|
|
|
|
|
|
@dp.message(F.text == "послезавтра")
|
|
@dp.message(Command("the_day_after_tomorrow"))
|
|
async def command_the_day_after_tomorrow(msg: Message):
|
|
await timetable_for_day(msg, 2)
|
|
|
|
|
|
async def send_notification(bot: Bot) -> None:
|
|
"""
|
|
Функция отправляющая уведомления по прошествию времени
|
|
|
|
:param bot: Bot - бот, с помощью которого можно отправлять сообщения
|
|
|
|
:return: None
|
|
"""
|
|
while True:
|
|
db = UseDB("users")
|
|
responses = db.find_document({})
|
|
for response in responses:
|
|
_, user_id, group_id, notification = response.values()
|
|
now = datetime.now()
|
|
if now.hour == notification.hour and now.minute == notification.minute:
|
|
await timetable_day(user_id, group_id, bot)
|
|
await asyncio.sleep(59)
|
|
|
|
|
|
async def update_data() -> None:
|
|
"""
|
|
Функция базу каждый день в полночь
|
|
|
|
:return: None
|
|
"""
|
|
while True:
|
|
now = datetime.now()
|
|
next_midnight = (
|
|
now + timedelta(days=1)
|
|
).replace(
|
|
hour=0,
|
|
minute=0,
|
|
second=0,
|
|
microsecond=0
|
|
)
|
|
await asyncio.sleep((next_midnight - now).total_seconds())
|
|
detect_group_to_update()
|
|
|
|
|
|
async def main() -> None:
|
|
bot = Bot(TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
|
|
_ = asyncio.create_task(send_notification(bot))
|
|
_ = asyncio.create_task(update_data())
|
|
await dp.start_polling(bot)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|