Skip to content

Commit

Permalink
Fix image bug better config (#12)
Browse files Browse the repository at this point in the history
* drawing works fine

* ref cleaning

* fixing bug with image and better config

* placeholder image
  • Loading branch information
Alex-Kopylov authored Feb 27, 2024
1 parent 59e8a9b commit ca180c2
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 60 deletions.
Binary file added assets/le_pol_face.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ python-dotenv = "^1.0.1"
python-telegram-bot = "^20.7"
pydantic = "^2.5.3"
cairosvg = "^2.7.1"
pydantic-settings = "^2.2.1"


[build-system]
Expand Down
Empty file added src/__init__.py
Empty file.
10 changes: 5 additions & 5 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
Application,
)

from src import config
from src.config import AppConfig
from src import handlers
from src.db import close_db
from src.get_handlers import get_handlers


def main() -> None:
if not config.TELEGRAM_BOT_TOKEN:
raise ValueError("TELEGRAM_BOT_TOKEN env variable" "wasn't purposed.")
def main(config: AppConfig = AppConfig()) -> None:
if not config.telegram_bot_token:
raise ValueError("telegram_bot_token env variable" "wasn't purposed.")

application = Application.builder().token(config.TELEGRAM_BOT_TOKEN).build()
application = Application.builder().token(config.telegram_bot_token).build()
application.add_handlers(get_handlers())
application.add_error_handler(handlers.error_handler)
# send a message to the developer when the bot is ready
Expand Down
70 changes: 41 additions & 29 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
import os
from pathlib import Path
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import Tuple
from pathlib import Path

from dotenv import load_dotenv

load_dotenv()

class AppConfig(BaseSettings):
# Define each configuration item with type annotations
telegram_bot_token: str | None = Field(default=None, env="TELEGRAM_BOT_TOKEN")
developer_chat_id: str | None = Field(default=None, env="DEVELOPER_CHAT_ID")
sqlite_db_file_path: Path | str = Field(
default=Path().resolve() / "db.sqlite", env="SQLITE_DB_FILE_PATH"
)
date_format: str = "%d.%m.%Y %H:%M:%S"

TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", None)
DEVELOPER_CHAT_ID = os.getenv("DEVELOPER_CHAT_ID", None)
# Game constants
max_votes: int = 10
max_hitler_voters: int = 1
min_hitler_voters: int = 1
max_liberal_voters: int = 6
min_liberal_voters: int = 3
max_fascist_voters: int = 3
min_fascist_voters: int = 1

# Database constants
SQLITE_DB_FILE_PATH = os.getenv("SQLITE_DB_FILE_PATH", Path().resolve() / "db.sqlite")
DATE_FORMAT = "%d.%m.%Y %H:%M:%S"
# Game poll outcomes
game_poll_outcomes: Tuple[str, ...] = (
"👀 SPECTATOR | NOT A PLAYER 👀",
"I'm Canceler Hitler",
"I'm Dead Hitler",
"I'm Hitler Loser",
"I'm Hitler Winner",
"I'm Liberal Winner",
"I'm Liberal Loser",
"I'm Fascistic Winner",
"I'm Fascistic Loser",
)

# Game constants
MAX_VOTES = 10
MAX_HITLER_VOTERS = 1
MIN_HITLER_VOTERS = 1
MAX_LIBERAL_VOTERS = 6
MIN_LIBERAL_VOTERS = 3
MAX_FASCIST_VOTERS = 3
MIN_FASCIST_VOTERS = 1
# Colors
liberal_color: str = "#61C8D9"
liberal_color_stroke: str = "#38586D"
fascist_color: str = "#E66443"
fascist_color_stroke: str = "#7A1E16"
stroke_size: str = "12" # TODO: pydantic int setter, str getter

GAME_POLL_OUTCOMES: Tuple = (
"👀 SPECTATOR | NOT A PLAYER 👀",
"I'm Canceler Hitler",
"I'm Dead Hitler",
"I'm Hitler Loser",
"I'm Hitler Winner",
"I'm Liberal Winner",
"I'm Liberal Loser",
"I'm Fascistic Winner",
"I'm Fascistic Loser",
)
class Config:
# Optional: control the source of environment variables
env_file = ".env"
env_file_encoding = "utf-8"
6 changes: 3 additions & 3 deletions src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from collections.abc import Iterable
from typing import Any, LiteralString
import aiosqlite
from src import config
from src.config import AppConfig


async def get_db() -> aiosqlite.Connection:
async def get_db(config: AppConfig = AppConfig()) -> aiosqlite.Connection:
if not getattr(get_db, "db", None):
db = await aiosqlite.connect(
config.SQLITE_DB_FILE_PATH, timeout=60 * 60 * 24 * 1 # 1 day
config.sqlite_db_file_path, timeout=60 * 60 * 24 * 1 # 1 day
)
get_db.db = db

Expand Down
12 changes: 7 additions & 5 deletions src/handlers/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from telegram import Update
from telegram.constants import ParseMode

from src import config
from src.config import AppConfig

from telegram.ext import ContextTypes


async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
async def error_handler(
update: object, context: ContextTypes.DEFAULT_TYPE, config: AppConfig = AppConfig()
) -> None:
# set a higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)

Expand All @@ -38,13 +40,13 @@ async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> N
f"<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n"
f"<pre>{html.escape(tb_string)}</pre>"
)
if config.DEVELOPER_CHAT_ID:
if config.developer_chat_id:
# Finally, send the message
await context.bot.send_message(
chat_id=config.DEVELOPER_CHAT_ID, text=message, parse_mode=ParseMode.HTML
chat_id=config.developer_chat_id, text=message, parse_mode=ParseMode.HTML
)
else:
logger.error(
"DEVELOPER_CHAT_ID env variable wasn't purposed. Please set it to your chat id if you want to "
"developer_chat_id env variable wasn't purposed. Please set it to your chat id if you want to "
"receive error messages in telegram chat."
)
7 changes: 5 additions & 2 deletions src/handlers/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from src import config
from src.data_models.Playroom import Playroom
from src.services.db_service import save_playroom
from src.config import AppConfig


async def game(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
async def game(
update: Update, context: ContextTypes.DEFAULT_TYPE, config: AppConfig = AppConfig()
) -> None:
"""Sends a predefined poll"""

questions = config.GAME_POLL_OUTCOMES
questions = config.game_poll_outcomes

message = await context.bot.send_poll(
update.effective_chat.id,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from src.data_models.Game import Game
from src.data_models.Record import Record
from src.services.db_service import save_record, save_game
from src.services.draw_result_image import draw_result_image
from src.services.db_service import save_record, save_game
from src.utils import message_is_poll, is_message_from_group_chat


Expand Down
Loading

0 comments on commit ca180c2

Please sign in to comment.