ruz-bot/helpers/answer.py
Administrator e5398fd843 again
2022-10-23 19:16:49 +03:00

244 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import datetime
import pytz
from telebot.types import Message
from daemons.bot import bot
from daemons.fetch import fetch_schedule_for_user
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
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)
mongo.users_collection.insert_one(UserSchema().dump(user))
else:
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):
if message.text == "Начать заново":
bot.send_message(
message.chat.id,
"Привет! Я буду помогать тебе выживать в вышке!\nДля начала пришли мне свое ФИО.",
)
return
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 _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})
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())
answer = Answer()