daily notify
This commit is contained in:
@@ -5,8 +5,9 @@ from telebot.types import Message
|
||||
|
||||
from daemons.bot import bot
|
||||
from daemons.fetch import fetch_schedule_for_user
|
||||
from helpers import now
|
||||
from helpers.keyboards import main_keyboard, notify_keyboard, yes_no_keyboard, again_keyboard, groups_keyboard
|
||||
from helpers import now, get_next_daily_notify_time
|
||||
from helpers.keyboards import main_keyboard, notify_keyboard, yes_no_keyboard, again_keyboard, groups_keyboard, \
|
||||
no_daily_notify
|
||||
from helpers.models import UserSchema, User
|
||||
from helpers.mongo import mongo
|
||||
from helpers.ruz import ruz
|
||||
@@ -19,6 +20,9 @@ class BaseAnswer:
|
||||
user = User(chat_id=message.chat.id)
|
||||
mongo.users_collection.insert_one(UserSchema().dump(user))
|
||||
else:
|
||||
# что-то со временем в маршмеллоу происходит, не знаю как решить
|
||||
if 'next_daily_notify_time' in user:
|
||||
user['next_daily_notify_time'] = str(user['next_daily_notify_time'])
|
||||
user = UserSchema().load(user)
|
||||
attr = getattr(self, "handle_state_" + user.state, None)
|
||||
if attr is None:
|
||||
@@ -121,6 +125,14 @@ class Answer(BaseAnswer):
|
||||
)
|
||||
self.set_state(user, "wait_for_notify")
|
||||
return
|
||||
elif message.text == "Ежедневные уведомления":
|
||||
bot.send_message(
|
||||
user.chat_id,
|
||||
"Каждый день (кроме воскресенья) я буду присылать тебе расписание на день. Пришли мне в формате чч:мм время, в которое ты хочешь получать расписание. Например, 09:30",
|
||||
reply_markup=no_daily_notify()
|
||||
)
|
||||
self.set_state(user, "wait_for_daily_notify")
|
||||
return
|
||||
elif message.text == "Сброс настроек":
|
||||
bot.send_message(user.chat_id, "Ты уверен что хочешь сбросить все настройки и больше не получать уведомления?", reply_markup=yes_no_keyboard())
|
||||
self.set_state(user, "reset")
|
||||
@@ -160,6 +172,58 @@ class Answer(BaseAnswer):
|
||||
bot.send_message(user.chat_id, text, reply_markup=main_keyboard())
|
||||
self.set_state(user, "ready")
|
||||
|
||||
def _validate_time(self, line: str) -> bool:
|
||||
if len(line) != 5:
|
||||
return False
|
||||
if line.count(":") != 1:
|
||||
return False
|
||||
hours, minutes = line.split(":")
|
||||
try:
|
||||
hours = int(hours)
|
||||
minutes = int(minutes)
|
||||
except ValueError:
|
||||
return False
|
||||
if hours < 0 or hours > 23 or minutes < 0 and minutes > 59:
|
||||
return False
|
||||
return True
|
||||
|
||||
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")
|
||||
return
|
||||
if not self._validate_time(text):
|
||||
bot.send_message(
|
||||
user.chat_id,
|
||||
"Ты прислал мне что-то неправильное. Пришли мне в формате чч:мм время, в которое ты хочешь получать расписание. Например, 09:30",
|
||||
reply_markup=no_daily_notify()
|
||||
)
|
||||
return
|
||||
user.daily_notify_time = text
|
||||
next_time = get_next_daily_notify_time(user)
|
||||
bot.send_message(
|
||||
user.chat_id,
|
||||
f"Принято! Буду уведомлять тебя каждый день в {text}.",
|
||||
reply_markup=main_keyboard()
|
||||
)
|
||||
mongo.users_collection.update_one(
|
||||
{"chat_id": user.chat_id},
|
||||
{"$set": {
|
||||
"daily_notify_time": text,
|
||||
"next_daily_notify_time": next_time,
|
||||
"state": "ready"
|
||||
}}
|
||||
)
|
||||
|
||||
def handle_state_reset(self, message: Message, user: User):
|
||||
if message.text == "Да":
|
||||
mongo.users_collection.delete_one({"chat_id": user.chat_id})
|
||||
|
||||
Reference in New Issue
Block a user