239 lines
10 KiB
Python
239 lines
10 KiB
Python
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
|
||
|
||
|
||
class BaseAnswer:
|
||
def process(self, message: Message):
|
||
user = mongo.users_collection.find_one({"chat_id": message.chat.id})
|
||
if user is None:
|
||
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:
|
||
raise NotImplementedError(f"handled state {user.state} but not implemented!")
|
||
attr(message, user)
|
||
|
||
def set_state(self, user: User, state: str):
|
||
user.state = state
|
||
mongo.users_collection.update_one({"chat_id": user.chat_id}, {"$set": {"state": state}})
|
||
|
||
|
||
class Answer(BaseAnswer):
|
||
|
||
def handle_state_new(self, message: Message, user: User):
|
||
bot.send_message(
|
||
message.chat.id,
|
||
"Привет! Я буду помогать тебе выживать в вышке!\nДля начала пришли мне свое ФИО.",
|
||
)
|
||
self.set_state(user, "wait_for_name")
|
||
|
||
def handle_state_wait_for_name(self, message: Message, user: User):
|
||
data = ruz.find_person(message.text)
|
||
if data is None:
|
||
bot.send_message(
|
||
user.chat_id,
|
||
"В РУЗе какая-то поломка, попробуй еще раз позже."
|
||
)
|
||
return
|
||
if len(data) == 0:
|
||
bot.send_message(user.chat_id, "К сожалению, в РУЗе не нашлось такого студента, попробуй еще раз.")
|
||
return
|
||
user.name = message.text
|
||
mongo.users_collection.update_one(
|
||
{"chat_id": user.chat_id},
|
||
{"$set": {"name": user.name}})
|
||
bot.send_message(
|
||
user.chat_id,
|
||
"Отлично! Теперь выбери из списка свою группу.",
|
||
reply_markup=groups_keyboard(data)
|
||
)
|
||
self.set_state(user, "wait_for_group")
|
||
|
||
def handle_state_wait_for_group(self, message: Message, user: User):
|
||
group = message.text
|
||
data = ruz.find_person(user.name)
|
||
if data is None:
|
||
bot.send_message(
|
||
user.chat_id,
|
||
"В РУЗе какая-то поломка, попробуй еще раз позже."
|
||
)
|
||
return
|
||
for element in data:
|
||
if element['description'] == group:
|
||
user.hse_id = int(element['id'])
|
||
user.group = group
|
||
user.name = element['label']
|
||
break
|
||
if user.group is None:
|
||
bot.send_message(user.chat_id, "Ты ввел что-то неправильно, попробуй еще раз сначала. Введи свое ФИО.")
|
||
self.set_state(user, "wait_for_name")
|
||
return
|
||
mongo.users_collection.update_one({"chat_id": user.chat_id}, {"$set": {
|
||
"hse_id": user.hse_id,
|
||
"group": group,
|
||
"name": user.name
|
||
}})
|
||
bot.send_message(
|
||
user.chat_id,
|
||
"Я нашел тебя в базе РУЗ. Я буду подсказывать тебе расписание, а также уведомлять о предстоящих парах.",
|
||
)
|
||
success = fetch_schedule_for_user(user.hse_id)
|
||
if success:
|
||
lessons = mongo.get_today_lessons(user)
|
||
if len(lessons) == 0:
|
||
bot.send_message(
|
||
user.chat_id,
|
||
"Сегодня у тебя нет пар.",
|
||
reply_markup=main_keyboard()
|
||
)
|
||
else:
|
||
bot.send_message(
|
||
user.chat_id,
|
||
ruz.schedule_builder(lessons),
|
||
reply_markup=main_keyboard()
|
||
)
|
||
self.set_state(user, "ready")
|
||
|
||
def handle_state_ready(self, message: Message, user: User):
|
||
if message.text == "Пары сегодня":
|
||
lessons = mongo.get_today_lessons(user)
|
||
if len(lessons) == 0:
|
||
text = "Сегодня у тебя нет пар."
|
||
else:
|
||
text = ruz.schedule_builder(lessons)
|
||
elif message.text == "Уведомления о парах":
|
||
bot.send_message(
|
||
user.chat_id,
|
||
"Выбери когда мне нужно тебе написать о предстоящей паре",
|
||
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 == "Сброс настроек":
|
||
bot.send_message(user.chat_id, "Ты уверен что хочешь сбросить все настройки и больше не получать уведомления?", reply_markup=yes_no_keyboard())
|
||
self.set_state(user, "reset")
|
||
return
|
||
else:
|
||
text = "Я не понимаю такой команды, используй кнопки."
|
||
bot.send_message(
|
||
user.chat_id,
|
||
text,
|
||
reply_markup=main_keyboard()
|
||
)
|
||
|
||
def handle_state_wait_for_notify(self, message: Message, user: User):
|
||
text = message.text
|
||
if text == "Не уведомлять":
|
||
user.notify_minutes = None
|
||
elif text == "5 минут":
|
||
user.notify_minutes = 5
|
||
elif text == "10 минут":
|
||
user.notify_minutes = 10
|
||
elif text == "15 минут":
|
||
user.notify_minutes = 15
|
||
elif text == "20 минут":
|
||
user.notify_minutes = 20
|
||
else:
|
||
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"Принято! Я не уведомлять тебя."
|
||
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})
|
||
bot.send_message(user.chat_id, "Настройки сброшены, ждем твоего возвращения", reply_markup=again_keyboard())
|
||
elif message.text == "Нет":
|
||
bot.send_message(user.chat_id, "Возращаюсь к прежнему режиму", reply_markup=main_keyboard())
|
||
self.set_state(user, "ready")
|
||
else:
|
||
bot.send_message(user.chat_id,
|
||
"Я не понимаю, используй кнопки",
|
||
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()
|