From acfc7bf22e9f520fd05522f2e850481588e95860 Mon Sep 17 00:00:00 2001 From: emmatveev Date: Sat, 11 Nov 2023 13:37:50 +0300 Subject: [PATCH] yandex code --- helpers/alice.py | 18 +++++++++--------- helpers/answer.py | 15 +++++++++++++-- helpers/mongo.py | 6 ++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/helpers/alice.py b/helpers/alice.py index ce48632..ffe9eea 100644 --- a/helpers/alice.py +++ b/helpers/alice.py @@ -37,10 +37,10 @@ class Processor: self.user_id = None self.message = data['request']['original_utterance'].lower() - def get_lesson_for_user(self, hse_id: int): - user = mongo.users_collection.find_one({"hse_id": hse_id}) + def get_lesson_for_user(self, chat_id: int): + user = mongo.users_collection.find_one({"chat_id": chat_id}) t = now(user) - for lesson in mongo.lessons_collection.find({"hse_user_id": hse_id, "begin": {"$gte": t}}).sort([("begin", 1)]): + for lesson in mongo.lessons_collection.find({"user_email": user['email'], "begin": {"$gte": t}}).sort([("begin", 1)]): return lesson return None @@ -48,7 +48,7 @@ class Processor: logging.info("user %s is saying\"%s\"", self.user_id, self.message) if "что ты умеешь" in self.message or "помощь" in self.message: return { - "text": "Я буду тебе подсказывать расписание занятий из РУЗа. Чтобы подключить меня к своему расписанию, зайди в бота, нажми на кнопку \"Подключение Алисы\" и назови мне код из сообщения по одной цифре." + "text": "Я буду тебе подсказывать расписание занятий из РУЗа. Чтобы подключить меня к своему расписанию, зайди в бота, нажми на кнопку \"Подключение Алисы\" и назови мне фразу из сообщения." } if self.data['session']['new']: if self.user_id is None: @@ -60,7 +60,7 @@ class Processor: "text": "Привет! Я буду тебе подсказывать расписание занятий из РУЗа. Чтобы подключить меня к своему расписанию, зайди в бота, нажми на кнопку \"Подключение Алисы\" и назови мне код из сообщения по одной цифре." } else: - lesson = self.get_lesson_for_user(user['hse_id']) + lesson = self.get_lesson_for_user(user['chat_id']) if lesson is None: return { "text": f"В ближайшее время у тебя нет пар", @@ -71,9 +71,9 @@ class Processor: "end_session": True } else: - hse_id = try_parse(self.message) + code = self.message try: - user = mongo.users_collection.find_one({"hse_id": hse_id}) + user = mongo.users_collection.find_one({"yandex_code": code}) except ValueError: return { "text": "Извини, не могу разобрать код, назови его еще раз" @@ -83,9 +83,9 @@ class Processor: "text": "Извини, не могу разобрать код, назови его еще раз" } else: - mongo.users_collection.update_one({"hse_id": int(hse_id)}, {"$set": {"yandex_id": self.user_id}}) + mongo.users_collection.update_one({"yandex_code": code}, {"$set": {"yandex_id": self.user_id, "yandex_code": None}}) bot.send_message(user['chat_id'], "Алиса успешно подключена!") - lesson = self.get_lesson_for_user(user['hse_id']) + lesson = self.get_lesson_for_user(user['chat_id']) if lesson is None: return { "text": f"Отлично, теперь я могу подсказывать тебе расписание. В ближайшее время у тебя нет пар", diff --git a/helpers/answer.py b/helpers/answer.py index 6be6dbb..c233b7c 100644 --- a/helpers/answer.py +++ b/helpers/answer.py @@ -1,4 +1,5 @@ import datetime +from random import choice from telebot.types import Message, ReplyKeyboardRemove @@ -8,6 +9,7 @@ from helpers import get_next_daily_notify_time from helpers.keyboards import main_keyboard, notify_keyboard, yes_no_keyboard, again_keyboard, no_daily_notify, \ campus_keyboard, daily_notify_type, notify_type, first_lesson_notify from helpers.mongo import mongo +from helpers.platform import platform from helpers.ruz import ruz @@ -34,7 +36,8 @@ class Answer: 'daily_notify_today': True, 'first_lesson_notify': None, 'last_schedule_fetch': datetime.datetime.now(), - 'yandex_id': None + 'yandex_id': None, + 'yandex_code': None, } mongo.users_collection.insert_one(user) self.user = user @@ -143,7 +146,15 @@ class Answer: return elif self.message_text == "Подключение Алисы": if self.user['yandex_id'] is None: - text = "Для того, чтобы подключить Яндекс.Алису, вызови навык \"Расписание вышки\" и назови этот код: " + str(self.user['hse_id']) + words = platform.get_config('words') + while True: + random_words = [choice(words) for _ in range(4)] + code = ' '.join(random_words) + u = mongo.users_collection.find_one({"yandex_code": code}) + if u is None: + break + mongo.users_collection.update_one({'chat_id': self.user['chat_id']}, {"$set": {"yandex_code": code}}) + text = "Для того, чтобы подключить Яндекс.Алису, вызови навык \"Расписание вышки\" и назови этот код: " + code else: text = "Янедкс.Алиса уже подключена. Чтобы узнать ближайшую пару, вызови навык \"Расписание вышки\"" else: diff --git a/helpers/mongo.py b/helpers/mongo.py index 8389afc..56a9b86 100644 --- a/helpers/mongo.py +++ b/helpers/mongo.py @@ -15,6 +15,12 @@ class Mongo: self.users_collection.create_index([ ("chat_id", 1) ]) + self.users_collection.create_index([ + ("yandex_id", 1) + ]) + self.users_collection.create_index([ + ("yandex_code", 1) + ]) self.users_collection.create_index([ ("notify_minutes", 1) ])