Skip to content

Commit

Permalink
Poll retract handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Kopylov committed Feb 6, 2024
1 parent 5e1233c commit 163ccae
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/callbacks/receive_poll_answer.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import logging

from telegram import Update
from telegram.ext import ContextTypes


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,
)
4 changes: 1 addition & 3 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/handlers/game.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
15 changes: 15 additions & 0 deletions src/services/db_service.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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")

0 comments on commit 163ccae

Please sign in to comment.