-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pols saving to db (previously was stored in memory), can save pols after reload * setting TZ to GMT by default
- Loading branch information
1 parent
ca180c2
commit 85041d8
Showing
11 changed files
with
245 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
|
||
from telegram import Update | ||
from telegram.ext import ContextTypes | ||
|
||
from src.data_models.Player import Player | ||
from src.data_models.PollResult import PollResult | ||
from src.services.db_service import save_player, save_poll_result, delete_poll_result | ||
|
||
|
||
async def poll_callback_receiver( | ||
update: Update, context: ContextTypes.DEFAULT_TYPE | ||
) -> None: | ||
"""Save or delete poll results in the database.""" | ||
answer = update.poll_answer | ||
user_id = update.effective_user.id | ||
poll_id = int(answer.poll_id) # Convert from telegram string id to int | ||
|
||
if not answer.option_ids: # Poll retraction, delete previous vote | ||
await delete_poll_result(poll_id=poll_id, user_id=user_id) | ||
return | ||
|
||
# Retrieve the selected option's text or index | ||
selected_option_index = answer.option_ids[0] | ||
|
||
await save_poll_result( | ||
PollResult( | ||
poll_id=poll_id, | ||
user_id=user_id, | ||
answer=selected_option_index, | ||
) | ||
) | ||
await save_player( | ||
Player( | ||
telegram_user_id=user_id, | ||
username=update.effective_user.username, | ||
first_name=update.effective_user.first_name, | ||
full_name=update.effective_user.full_name, | ||
last_name=update.effective_user.last_name, | ||
is_bot=update.effective_user.is_bot, | ||
language_code=update.effective_user.language_code, | ||
) | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from pydantic import BaseModel, Field | ||
|
||
from src.config import AppConfig | ||
|
||
|
||
class PollResult(BaseModel): | ||
poll_id: int | ||
user_id: int | ||
answer: int = Field(ge=0, le=len(AppConfig().game_poll_outcomes)) | ||
|
||
def get_answer_as_text(self) -> str: | ||
return AppConfig().game_poll_outcomes[self.answer] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,47 @@ | ||
import asyncio | ||
|
||
from telegram import Update | ||
from telegram.ext import ContextTypes | ||
|
||
from src import config | ||
from src.data_models.Poll import Poll | ||
from src.data_models.Playroom import Playroom | ||
from src.services.db_service import save_playroom | ||
from src.services.db_service import save_playroom, save_poll | ||
from src.config import AppConfig | ||
|
||
|
||
async def game( | ||
update: Update, context: ContextTypes.DEFAULT_TYPE, config: AppConfig = AppConfig() | ||
) -> None: | ||
"""Sends a predefined poll""" | ||
"""Sends a predefined poll and saves its metadata to the database.""" | ||
|
||
questions = config.game_poll_outcomes | ||
|
||
message = await context.bot.send_poll( | ||
update.effective_chat.id, | ||
f"@{update.effective_user.username} want you to record last game. Please choose your outcome:", | ||
f"@{update.effective_user.username} wants you to record the last game. Please choose your outcome:", | ||
questions, | ||
is_anonymous=False, | ||
allows_multiple_answers=False, | ||
disable_notification=True, | ||
) | ||
|
||
# Save some info about the poll the bot_data for later use in receive_poll_answer | ||
game_metadata = { # TODO write it to DB | ||
message.poll.id: { | ||
"questions": questions, | ||
"message_id": message.id, # will be game_id | ||
"chat_id": update.effective_chat.id, | ||
"chat_name": update.effective_chat.title, | ||
"creator_id": update.effective_user.id, | ||
"creator_username": update.effective_user.username, | ||
"results": {}, | ||
} | ||
} | ||
await save_playroom( | ||
Playroom( | ||
telegram_chat_id=update.effective_chat.id, name=update.effective_chat.title | ||
) | ||
await asyncio.gather( | ||
*[ | ||
save_poll( | ||
Poll( | ||
id=message.poll.id, | ||
message_id=message.message_id, # Assuming this maps to the Poll table's message_id | ||
chat_id=update.effective_chat.id, | ||
chat_name=update.effective_chat.title, | ||
creator_id=update.effective_user.id, | ||
creator_username=update.effective_user.username, | ||
) | ||
), | ||
save_playroom( | ||
Playroom( | ||
telegram_chat_id=update.effective_chat.id, | ||
name=update.effective_chat.title, | ||
) | ||
), | ||
update.effective_message.delete(), | ||
] | ||
) | ||
context.bot_data.update(game_metadata) | ||
|
||
# Delete command message | ||
await update.effective_message.delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.