From b11574c1882c39f65d3b38e8b3c9ba07d1a572fd Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 30 Mar 2023 00:53:29 +0300 Subject: [PATCH] new notify --- daemons/fetch.py | 3 ++- daemons/notify.py | 34 ++++++++++++++++++++++++++++ helpers/answer.py | 53 ++++++++++++++++++++++++++++++++++++++++---- helpers/keyboards.py | 18 +++++++++++++++ helpers/models.py | 1 + 5 files changed, 104 insertions(+), 5 deletions(-) diff --git a/daemons/fetch.py b/daemons/fetch.py index ad3875b..7491f39 100644 --- a/daemons/fetch.py +++ b/daemons/fetch.py @@ -54,7 +54,8 @@ def fetch_schedule_for_user(user: User): ), "building": element['building'], "lecturer": element['lecturer'], - "notified": False + "notified": False, + "notified_today": False }) saved_ids.append(result.inserted_id) else: diff --git a/daemons/notify.py b/daemons/notify.py index 2467656..9a371ee 100644 --- a/daemons/notify.py +++ b/daemons/notify.py @@ -59,6 +59,40 @@ def process(): {"$set": {"next_daily_notify_time": user_model.next_daily_notify_time + datetime.timedelta(days=1)}} ) + for user in mongo.users_collection.find({"first_lesson_notify": {"$ne": None}, "hse_id": {"$ne": None}}): + time_now = now(UserSchema().load(user)) + for lesson in mongo.lessons_collection.find({ + "hse_user_id": user["hse_id"], + "begin": {"$lte": time_now + datetime.timedelta(minutes=user["first_lesson_notify"])}, + "notified_today": {"$ne": True} + }).sort("begin"): + ans = f"📅 {lesson['begin'].strftime('%d.%m')}\n" + ans += f"📚 {lesson['discipline']}\n" + ans += f"🏢 {lesson['building']}, {lesson['auditorium']}\n" + ans += f"🕑 {lesson['begin'].strftime('%H:%M')} - {lesson['end'].strftime('%H:%M')}\n" + ans += f"🧑‍🏫 {(lesson['lecturer'] or 'Неизвестно')}\n" + if lesson.get('link', None): + ans += f"🔗 {lesson['link']}" + try: + mess = "Пары начутся через " + if user['first_lesson_notify'] == 30: + mess += "30 минут" + elif user['first_lesson_notify'] == 60: + mess += "1 час" + elif user['first_lesson_notify'] == 4 * 60: + mess += "4 часа" + else: + mess += "12 часов" + mess += "!\n\nТвоя первая пара:\n\n" + ans + bot.send_message( + user["chat_id"], + mess + ) + except ApiTelegramException: + pass + mongo.lessons_collection.update_many({"begin": {"$gte": time_now.date(), "$lt": (time_now + datetime.timedelta(days=1)).date()}}, {"$set": {"notified_today": True}}) + break + def notify(): while True: diff --git a/helpers/answer.py b/helpers/answer.py index 946e88e..71e4fc9 100644 --- a/helpers/answer.py +++ b/helpers/answer.py @@ -4,7 +4,7 @@ from daemons.bot import bot from daemons.fetch import fetch_schedule_for_user from helpers import get_next_daily_notify_time from helpers.keyboards import main_keyboard, notify_keyboard, yes_no_keyboard, again_keyboard, groups_keyboard, \ - no_daily_notify, student_or_teacher_keyboard, campus_keyboard, daily_notify_type + no_daily_notify, student_or_teacher_keyboard, campus_keyboard, daily_notify_type, notify_type, first_lesson_notify from helpers.models import UserSchema, User from helpers.mongo import mongo from helpers.ruz import ruz @@ -188,10 +188,10 @@ class Answer(BaseAnswer): elif message.text == "Уведомления о парах": bot.send_message( user.chat_id, - "Выбери за сколько минут мне нужно напомнить тебе о предстоящей паре", - reply_markup=notify_keyboard() + "Я умею уведомлять о каждой паре и о первой паре. Что хочешь настроить?", + reply_markup=notify_type() ) - self.set_state(user, "wait_for_notify") + self.set_state(user, "notify_type") return elif message.text == "Ежедневные уведомления": bot.send_message( @@ -213,6 +213,51 @@ class Answer(BaseAnswer): reply_markup=main_keyboard() ) + def handle_state_notify_type(self, message: Message, user: User): + text = message.text + if text == "О каждой паре": + bot.send_message( + user.chat_id, + "Выбери за сколько минут мне нужно напомнить тебе о предстоящей паре", + reply_markup=notify_keyboard() + ) + self.set_state(user, "wait_for_notify") + elif text == "О первой паре": + bot.send_message( + user.chat_id, + "Выбери за сколько минут мне нужно напоминать тебе о первой паре", + reply_markup=first_lesson_notify() + ) + self.set_state(user, "wait_for_first_notify") + elif text == "Назад": + self.set_state(user, "ready") + bot.send_message( + user.chat_id, + text, + reply_markup=main_keyboard() + ) + else: + bot.send_message(user.chat_id, "Используй кнопки!", reply_markup=notify_type()) + + def handle_state_wait_for_first_notify(self, message: Message, user: User): + text = message.text + if text == "30 минут": + time_notify = 30 + elif text == "1 час": + time_notify = 60 + elif text == "4 часа": + time_notify = 4 * 60 + elif text == "12 часов": + time_notify = 12 * 60 + elif text == "Не уведомлять": + time_notify = None + else: + bot.send_message(user.chat_id, "Используй кнопки!", reply_markup=first_lesson_notify()) + return + mongo.users_collection.update_one({"chat_id": user.chat_id}, {"$set": {"first_lesson_notify": time_notify}}) + self.set_state(user, "ready") + bot.send_message(user.chat_id, "Запомнил!", reply_markup=main_keyboard()) + def handle_state_wait_for_daily_notify_type(self, message: Message, user: User): text = message.text if text == "Текущий день": diff --git a/helpers/keyboards.py b/helpers/keyboards.py index a954cb1..98ca989 100644 --- a/helpers/keyboards.py +++ b/helpers/keyboards.py @@ -70,3 +70,21 @@ def daily_notify_type(): kb.row("Следующий день") kb.row("Не уведомлять") return kb + + +def notify_type(): + kb = telebot.types.ReplyKeyboardMarkup(True, False) + kb.row("О каждой паре") + kb.row("О первой паре") + kb.row("Назад") + return kb + + +def first_lesson_notify(): + kb = telebot.types.ReplyKeyboardMarkup(True, False) + kb.row("30 минут") + kb.row("1 час") + kb.row("4 часа") + kb.row("12 часов") + kb.row("Не уведомлять") + return kb diff --git a/helpers/models.py b/helpers/models.py index 1f124ad..905681b 100644 --- a/helpers/models.py +++ b/helpers/models.py @@ -19,6 +19,7 @@ class User: is_teacher: bool = False campus: str = "Москва" daily_notify_today: bool = True + first_lesson_notify: Optional[float] = None class Meta: unknown = EXCLUDE