From 7b4099a416cdcfe6f253b72a26741203b6926929 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 11 Feb 2020 22:52:36 +0400 Subject: Added binding to attempts API and integrated it into bot. --- backend_api.py | 18 ++++++++++++++++++ states.py | 50 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/backend_api.py b/backend_api.py index 8f678bb..e830609 100644 --- a/backend_api.py +++ b/backend_api.py @@ -75,3 +75,21 @@ def save_state(last_state: int, tg_id: int, user_data: dict) -> Tuple[int, Dict] def get_state(tg_id: int) -> Tuple[int, dict]: logger.debug(f"Trying to get state for user with id={tg_id}") return get_request(f"{BACKEND_URL}/api/state/get/{tg_id}/") + + +def create_attempt(tg_id: int, task_title: str, answer: str): + return post_request(f"{BACKEND_URL}/attempts/", data={ + "profile": json.dumps({"tg_id": tg_id}), + "task": json.dumps({"title": task_title}), + "answer": answer + }) + + +def get_attempts(tg_id: int=None, task_title: str=None): + data = {} + if tg_id is not None: + data["tg_id"] = tg_id + if task_title is not None: + data["task_title"] = task_title + + return get_request(f"{BACKEND_URL}/api/attempts/", data=data) diff --git a/states.py b/states.py index 653f442..e6ae0aa 100644 --- a/states.py +++ b/states.py @@ -13,7 +13,6 @@ from telegram import Update, User, Bot def save_state(func): def wrapper(bot: Bot, update: Update, user_data: dict, *args, **kwargs): - print(user_data) last_state = func(bot, update, user_data, *args, **kwargs) backend_api.save_state(last_state, update.message.from_user.id, user_data) return last_state @@ -101,27 +100,52 @@ class States: @staticmethod @save_state def type_answer(bot: Bot, update: Update, user_data: dict): - update.message.reply_text( - "Вводи свой ответ, я его проверю.", - reply_markup=ReplyKeyboardMarkup(AnsweringKeyboard.get_keyboard()) + status_code, response = backend_api.get_attempts( + tg_id=update.message.from_user.id, + task_title=user_data["chosen_task"] ) - return ANSWERING + if len(response) != 0: + update.message.reply_text( + "Ты уже решил эту задачу! Выбери другую.", + reply_markup=ReplyKeyboardMarkup(ContinueKeyboard.get_keyboard()) + ) + + return TASK_CHOOSING + + else: + update.message.reply_text( + "Вводи свой ответ, я его проверю.", + reply_markup=ReplyKeyboardMarkup(AnsweringKeyboard.get_keyboard()) + ) + return ANSWERING @staticmethod @save_state def accept_answer(bot: Bot, update: Update, user_data: dict): answer = update.message.text - if answer == "хуй": - update.message.reply_text( - "Ты ввел правильный ответ! Возвращайся к другим задачам", - reply_markup=ReplyKeyboardMarkup(ContinueKeyboard.get_keyboard()) - ) + status_code, task = backend_api.get_task(user_data["chosen_task"]) + if status_code == 200: + backend_api.create_attempt(update.message.from_user.id, user_data["chosen_task"], answer) + + if answer == task["answer"]: + update.message.reply_text( + "Ты ввел правильный ответ! Возвращайся к другим задачам", + reply_markup=ReplyKeyboardMarkup(ContinueKeyboard.get_keyboard()) + ) + + return ANSWER_RIGHT + else: + update.message.reply_text( + "К сожалению, твой ответ неверный =(", + reply_markup=ReplyKeyboardMarkup(ContinueKeyboard.get_keyboard()) + ) + + return ANSWER_WRONG - return ANSWER_RIGHT else: update.message.reply_text( - "К сожалению, твой ответ неверный =(", + "Произошла ошибка в работе квиза. Мы уже работаем над её устранением!", reply_markup=ReplyKeyboardMarkup(ContinueKeyboard.get_keyboard()) ) - return ANSWER_WRONG + return ANSWER_RIGHT -- cgit v1.2.3