From 163ccae03bd932af3bb00dd61479417eb73b02b2 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 6 Feb 2024 21:29:39 +0400 Subject: [PATCH] Poll retract handling --- src/callbacks/receive_poll_answer.py | 17 +++++++++++------ src/config.py | 4 +--- src/handlers/game.py | 8 +++++++- src/services/db_service.py | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/callbacks/receive_poll_answer.py b/src/callbacks/receive_poll_answer.py index c928a95..dd2fc88 100644 --- a/src/callbacks/receive_poll_answer.py +++ b/src/callbacks/receive_poll_answer.py @@ -1,3 +1,5 @@ +import logging + from telegram import Update from telegram.ext import ContextTypes @@ -5,17 +7,20 @@ async def receive_poll_answer( update: Update, context: ContextTypes.DEFAULT_TYPE ) -> None: + # TODO save polls to db and query them instead saving them to bot_data """Summarize a users poll vote""" answer = update.poll_answer - if not answer.option_ids: - return # It's retake poll action, ignore it + if not answer.option_ids: # Poll retract, delete previous vote + del context.bot_data[answer.poll_id]["results"][update.effective_user.id] + return if context.bot_data: answered_poll = context.bot_data[answer.poll_id] user_id = update.effective_user.id result = answered_poll["questions"][answer.option_ids[0]] context.bot_data[answer.poll_id]["results"][user_id] = result else: - # failed to save poll answer. - # Ussually happens for polls that are sent to the bot before it started and not updated - # TODO save polls to db and query them instead saving them to bot_data - return + logging.error( + "Failed to save poll answer. Usually happens for polls that are sent to the bot before it " + "started and not updated", + exc_info=True, + ) diff --git a/src/config.py b/src/config.py index 6325802..803cad6 100644 --- a/src/config.py +++ b/src/config.py @@ -11,9 +11,7 @@ DEVELOPER_CHAT_ID = os.getenv("DEVELOPER_CHAT_ID", None) # Database constants -SQLITE_DB_FILE_PATH = os.getenv( - "SQLITE_DB_FILE_PATH", Path().resolve() / "db.sqlite" -) +SQLITE_DB_FILE_PATH = os.getenv("SQLITE_DB_FILE_PATH", Path().resolve() / "db.sqlite") DATE_FORMAT = "%d.%m.%Y %H:%M:%S" # Game constants diff --git a/src/handlers/game.py b/src/handlers/game.py index 1caddcc..75d0aee 100644 --- a/src/handlers/game.py +++ b/src/handlers/game.py @@ -1,6 +1,8 @@ from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import ContextTypes from src import config +from src.data_models.Playroom import Playroom +from src.services.db_service import save_playroom from src.utils import message_is_poll, is_message_from_group_chat @@ -30,5 +32,9 @@ async def game(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: "results": {}, } } - + await save_playroom( + Playroom( + telegram_chat_id=update.effective_chat.id, name=update.effective_chat.title + ) + ) context.bot_data.update(game_metadata) diff --git a/src/services/db_service.py b/src/services/db_service.py index 954ee63..24d459c 100644 --- a/src/services/db_service.py +++ b/src/services/db_service.py @@ -1,3 +1,7 @@ +import logging +import sqlite3 + +from src.data_models.Playroom import Playroom from src.data_models.Record import Record from src.db import execute @@ -14,3 +18,14 @@ async def save_record(record: Record) -> None: record.role, ), ) + + +async def save_playroom(playroom: Playroom) -> None: + """Add a game room to the bot_data""" + try: + await execute( + "INSERT INTO playrooms (id, name) VALUES (?, ?)", + (playroom.telegram_chat_id, playroom.name), + ) + except sqlite3.IntegrityError: + logging.info(f"Playroom {playroom.name} already exists in the database")