diff --git a/daemons/notify.py b/daemons/notify.py index 27f4917..bbe074c 100644 --- a/daemons/notify.py +++ b/daemons/notify.py @@ -36,16 +36,20 @@ def process(): time_now = datetime.datetime.now() for user in mongo.users_collection.find({"next_daily_notify_time": {"$lte": time_now}}): user_model = UserSchema().load(user) - if time_now.weekday() != 6: - lessons = mongo.get_today_lessons(user_model) + deny_weekday = 6 if user_model.daily_notify_today else 5 + if time_now.weekday() != deny_weekday: + if user_model.daily_notify_today: + lessons = mongo.get_today_lessons(user_model) + else: + lessons = mongo.get_tomorrow_lessons(user_model) if len(lessons) == 0: - text = "Сегодня у тебя нет пар, отдыхай." + text = f"{'Сегодня' if user_model.daily_notify_today else 'Завтра'} у тебя нет пар, отдыхай." else: text = ruz.schedule_builder(lessons) try: bot.send_message( user["chat_id"], - "Уведомляю о занятиях! Твое расписание на сегодня:\n" + text + f"Уведомляю о занятиях! Твое расписание на {'сегодня' if user_model.daily_notify_today else 'завтра'}:\n" + text ) except: pass diff --git a/helpers/answer.py b/helpers/answer.py index ef2bdad..00ddcfe 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 + no_daily_notify, student_or_teacher_keyboard, campus_keyboard, daily_notify_type from helpers.models import UserSchema, User from helpers.mongo import mongo from helpers.ruz import ruz @@ -196,10 +196,10 @@ class Answer(BaseAnswer): elif message.text == "Ежедневные уведомления": bot.send_message( user.chat_id, - "Каждый день (кроме воскресенья) я буду присылать тебе расписание на день. Пришли мне в формате чч:мм время, в которое ты хочешь получать расписание. Например, 09:30", - reply_markup=no_daily_notify() + "Я могу присылать тебе расписание на текущий день или на следующий. Как ты хочешь чтобы я тебя уведомлял?", + reply_markup=daily_notify_type() ) - self.set_state(user, "wait_for_daily_notify") + self.set_state(user, "wait_for_daily_notify_type") return elif message.text == "Сброс настроек": bot.send_message(user.chat_id, "Ты уверен что хочешь сбросить все настройки и больше не получать уведомления?", reply_markup=yes_no_keyboard()) @@ -213,6 +213,45 @@ class Answer(BaseAnswer): reply_markup=main_keyboard() ) + def handle_state_wait_for_daily_notify_type(self, message: Message, user: User): + text = message.text + if text == "Текущий день": + bot.send_message( + user.chat_id, + "Каждый день (кроме воскресенья) я буду присылать тебе расписание на текущий день. Пришли мне в формате чч:мм время, в которое ты хочешь получать расписание. Например, 09:30", + reply_markup=no_daily_notify() + ) + mongo.users_collection.update_one( + {"chat_id": user.chat_id}, + {"$set": {"daily_notify_today": True, "state": "wait_for_daily_notify"}} + ) + elif text == "Следующий день": + bot.send_message( + user.chat_id, + "Каждый день (кроме субботы) я буду присылать тебе расписание на следующий день. Пришли мне в формате чч:мм время, в которое ты хочешь получать расписание. Например, 20:30", + reply_markup=no_daily_notify() + ) + mongo.users_collection.update_one( + {"chat_id": user.chat_id}, + {"$set": {"daily_notify_today": False, "state": "wait_for_daily_notify"}} + ) + elif text == "Не уведомлять": + bot.send_message( + user.chat_id, + "Принято! Я не уведомлять тебя.", + reply_markup=main_keyboard() + ) + mongo.users_collection.update_one( + {"chat_id": user.chat_id}, + {"$set": {"next_daily_notify_time": None, "daily_notify_time": None, "state": "ready"}} + ) + else: + bot.send_message( + user.chat_id, + "Ты прислал мне что-то неправильное, используй кнопки. Как ты хочешь чтобы я тебя уведомлял?", + reply_markup=daily_notify_type() + ) + def handle_state_wait_for_notify(self, message: Message, user: User): text = message.text if text == "Не уведомлять": @@ -258,16 +297,15 @@ class Answer(BaseAnswer): def handle_state_wait_for_daily_notify(self, message: Message, user: User): text = message.text if text == "Не уведомлять": - mongo.users_collection.update_one( - {"chat_id": user.chat_id}, - {"$set": {"daily_notify_time": None, "next_daily_notify_time": None}} - ) bot.send_message( user.chat_id, "Принято! Я не буду присылать тебе ежедневные уведомления.", reply_markup=main_keyboard() ) - self.set_state(user, "ready") + mongo.users_collection.update_one( + {"chat_id": user.chat_id}, + {"$set": {"daily_notify_time": None, "next_daily_notify_time": None, "state": "ready"}} + ) return if not self._validate_time(text): bot.send_message( diff --git a/helpers/keyboards.py b/helpers/keyboards.py index 38e44d3..fcbafd7 100644 --- a/helpers/keyboards.py +++ b/helpers/keyboards.py @@ -62,3 +62,11 @@ def no_daily_notify(): kb = telebot.types.ReplyKeyboardMarkup(True, False) kb.row("Не уведомлять") return kb + + +def daily_notify_type(): + kb = telebot.types.ReplyKeyboardMarkup(True, False) + kb.row("Текущий день") + kb.row("Следующий день") + kb.row("Не уведомлять") + return kb diff --git a/helpers/models.py b/helpers/models.py index d2e6d48..1f124ad 100644 --- a/helpers/models.py +++ b/helpers/models.py @@ -18,6 +18,7 @@ class User: next_daily_notify_time: Optional[datetime.datetime] = None is_teacher: bool = False campus: str = "Москва" + daily_notify_today: bool = True class Meta: unknown = EXCLUDE