ruz-bot/helpers/alice.py
Administrator 209a355e37 alice
2023-04-08 14:19:18 +03:00

59 lines
2.9 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.

from daemons.bot import bot
from helpers.mongo import mongo
class Processor:
def __init__(self, data: dict):
self.data = data
if 'user' in data['session']:
self.user_id = data['session']['user']['user_id']
else:
self.user_id = None
self.message = data['request']['original_utterance'].lower()
def get_lesson_for_user(self, chat_id: int):
...
def process(self) -> dict:
user = None
if self.data['session']['new']:
if self.user_id is not None:
return {}
else:
user = mongo.users_collection.find_one({"yandex_id": self.user_id})
if user is None:
return {
"text": "Привет! Я буду тебе подсказывать расписание занятий из РУЗа. Чтобы подключить меня к своему расписанию, зайди в бота, нажми на кнопку \"Подключение Алисы\" и назови мне код из сообщения."
}
else:
lesson = self.get_lesson_for_user(user['chat_id'])
if lesson is None:
return {
"text": f"В ближайшее время у тебя нет пар",
"end_session": True
}
return {
"text": f"Твое ближайшее занятие {lesson['begin']}: {lesson['discipline']}",
"end_session": True
}
else:
hse_id = self.message.replace(' ', '')
user = mongo.users_collection.find_one({"hse_id": int(hse_id)})
if user is None:
return {
"text": "Извини, не могу разобрать код, назови его еще раз"
}
else:
mongo.users_collection.update_one({"hse_id": int(hse_id)}, {"$set": {"yandex_id": self.user_id}})
bot.send_message(user['chat_id'], "Алиса успешно подключена!")
lesson = self.get_lesson_for_user(user['chat_id'])
if lesson is None:
return {
"text": f"Отлично, теперь я могу подсказывать тебе расписание. В ближайшее время у тебя нет пар",
"end_session": True
}
return {
"text": f"Отлично, теперь я могу подсказывать тебе расписание. Твое ближайшее занятие {lesson['begin']}: {lesson['discipline']}",
"end_session": True
}