summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2020-02-11 22:52:36 +0400
committerAndrew <saintruler@gmail.com>2020-02-11 22:52:36 +0400
commit7b4099a416cdcfe6f253b72a26741203b6926929 (patch)
tree9e2f66c70f463c3d9aa0d5901e6df74eaeb4637b
parentbc2124fbfa33b0aa5969c86523afb4f404e21ee5 (diff)
Added binding to attempts API and integrated it into bot.
-rw-r--r--backend_api.py18
-rw-r--r--states.py50
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