daily notify

This commit is contained in:
Administrator
2022-10-21 18:17:41 +03:00
parent 54c47001b7
commit 61436be630
8 changed files with 181 additions and 68 deletions

View File

@@ -1,8 +1,14 @@
import telebot
import datetime
import croniter
import pytz
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, \
no_daily_notify
from helpers.models import UserSchema, User
from helpers.mongo import mongo
from helpers.ruz import ruz
@@ -12,9 +18,21 @@ class BaseAnswer:
def process(self, message: Message):
user = mongo.users_collection.find_one({"chat_id": message.chat.id})
if user is None:
user = User(chat_id=message.chat.id)
cron = croniter.croniter("0 9 * * *", now())
next_date = cron.get_next(datetime.datetime)
next_date = next_date.replace(tzinfo=pytz.timezone('Europe/Moscow'))
user = User(chat_id=message.chat.id, next_notify_time=next_date)
mongo.users_collection.insert_one(UserSchema().dump(user))
else:
if "next_notify_time" not in user:
cron = croniter.croniter("0 9 * * *", now())
next_date = cron.get_next(datetime.datetime)
next_date = next_date.replace(tzinfo=pytz.timezone('Europe/Moscow'))
user["next_notify_time"] = next_date
mongo.users_collection.update_one(
{"chat_id": message.chat.id},
{"$set": {"next_notify_time": next_date}}
)
user = UserSchema().load(user)
attr = getattr(self, "handle_state_" + user.state, None)
if attr is None:
@@ -36,7 +54,6 @@ class Answer(BaseAnswer):
self.set_state(user, "wait_for_name")
def handle_state_wait_for_name(self, message: Message, user: User):
kb = telebot.types.ReplyKeyboardMarkup(True, False)
data = ruz.find_person(message.text)
if data is None:
bot.send_message(
@@ -47,8 +64,6 @@ class Answer(BaseAnswer):
if len(data) == 0:
bot.send_message(user.chat_id, "К сожалению, в РУЗе не нашлось такого студента, попробуй еще раз.")
return
for entity in data:
kb.row(entity['description'])
user.name = message.text
mongo.users_collection.update_one(
{"chat_id": user.chat_id},
@@ -56,7 +71,7 @@ class Answer(BaseAnswer):
bot.send_message(
user.chat_id,
"Отлично! Теперь выбери из списка свою группу.",
reply_markup=kb
reply_markup=groups_keyboard(data)
)
self.set_state(user, "wait_for_group")
@@ -90,55 +105,46 @@ class Answer(BaseAnswer):
)
success = fetch_schedule_for_user(user.hse_id)
if success:
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Пары сегодня")
kb.row("Уведомления")
kb.row("Сброс настроек")
lessons = mongo.get_today_lessons(user)
if len(lessons) == 0:
bot.send_message(
user.chat_id,
"Сегодня у тебя нет пар.",
reply_markup=kb
reply_markup=main_keyboard()
)
else:
bot.send_message(
user.chat_id,
ruz.schedule_builder(lessons),
reply_markup=kb
reply_markup=main_keyboard()
)
self.set_state(user, "ready")
def handle_state_ready(self, message: Message, user: User):
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Пары сегодня")
kb.row("Уведомления")
kb.row("Сброс настроек")
if message.text == "Пары сегодня":
lessons = mongo.get_today_lessons(user)
if len(lessons) == 0:
text = "Сегодня у тебя нет пар."
else:
text = ruz.schedule_builder(lessons)
elif message.text == "Уведомления":
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Не уведомлять")
kb.row("5 минут")
kb.row("10 минут")
kb.row("15 минут")
kb.row("20 минут")
elif message.text == "Уведомления о парах":
bot.send_message(
user.chat_id,
"Выбери когда мне нужно тебе написать о предстоящей паре",
reply_markup=kb
reply_markup=notify_keyboard()
)
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 == "Сброс настроек":
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Да")
kb.row("Нет")
bot.send_message(user.chat_id, "Ты уверен что хочешь сбросить все настройки и больше не получать уведомления?", reply_markup=kb)
bot.send_message(user.chat_id, "Ты уверен что хочешь сбросить все настройки и больше не получать уведомления?", reply_markup=yes_no_keyboard())
self.set_state(user, "reset")
return
else:
@@ -146,7 +152,7 @@ class Answer(BaseAnswer):
bot.send_message(
user.chat_id,
text,
reply_markup=kb
reply_markup=main_keyboard()
)
def handle_state_wait_for_notify(self, message: Message, user: User):
@@ -162,46 +168,71 @@ class Answer(BaseAnswer):
elif text == "20 минут":
user.notify_minutes = 20
else:
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Не уведомлять")
kb.row("5 минут")
kb.row("10 минут")
kb.row("15 минут")
kb.row("20 минут")
bot.send_message(user.chat_id, "Я не понимаю такой команды, используй кнопки.", reply_markup=kb)
bot.send_message(
user.chat_id,
"Я не понимаю такой команды, используй кнопки.",
reply_markup=notify_keyboard()
)
return
mongo.users_collection.update_one({"chat_id": user.chat_id}, {"$set": {"notify_minutes": user.notify_minutes}})
if user.notify_minutes is not None:
text = f"Принято! Буду уведомлять тебя за {text}."
else:
text = f"Принято! Я не уведомлять тебя."
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Пары сегодня")
kb.row("Уведомления")
kb.row("Сброс настроек")
bot.send_message(user.chat_id, text, reply_markup=kb)
bot.send_message(user.chat_id, text, reply_markup=main_keyboard())
self.set_state(user, "ready")
def handle_state_reset(self, message: Message, user: User):
if message.text == "Да":
mongo.users_collection.delete_one({"hse_id": user.hse_id})
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Начать заново")
bot.send_message(user.chat_id, "Настройки сброшены, ждем твоего возвращения", reply_markup=kb)
bot.send_message(user.chat_id, "Настройки сброшены, ждем твоего возвращения", reply_markup=again_keyboard())
elif message.text == "Нет":
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Пары сегодня")
kb.row("Уведомления")
kb.row("Сброс настроек")
bot.send_message(user.chat_id, "Возращаюсь к прежнему режиму", reply_markup=kb)
bot.send_message(user.chat_id, "Возращаюсь к прежнему режиму", reply_markup=main_keyboard())
self.set_state(user, "ready")
else:
kb = telebot.types.ReplyKeyboardMarkup(True, False)
kb.row("Да")
kb.row("Нет")
bot.send_message(user.chat_id,
"Я не понимаю, используй кнопки",
reply_markup=kb)
reply_markup=yes_no_keyboard())
def _check_time_correct(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:
return False
if hours < 0 or hours > 23 or minutes < 0 or minutes > 59:
return False
return True
def handle_state_wait_for_daily_notify(self, message: Message, user: User):
text = message.text
if not self._check_time_correct(text):
bot.send_message(
user.chat_id,
"Ты прислал что-то неправильное. Пришли время в формате чч:мм. Например, 09:30",
reply_markup=no_daily_notify()
)
return
user.notify_daily = message.text
hours, minutes = user.notify_daily.split(':')
cron = croniter.croniter(f"{minutes} {hours} * * *", now())
next_date = cron.get_next(datetime.datetime)
next_date = next_date.replace(tzinfo=pytz.timezone('Europe/Moscow'))
mongo.users_collection.update_one({"chat_id": user.chat_id}, {"$set": {
"notify_daily": message.text,
"next_notify_time": next_date
}})
bot.send_message(
user.chat_id,
f"Принято! Буду каждый день уведомлять тебя в {message.text}",
reply_markup=main_keyboard()
)
self.set_state(user, "ready")
answer = Answer()