From 046a117b09559b88f8812d9d34bfcff679488c7a Mon Sep 17 00:00:00 2001 From: kolo Date: Thu, 1 Jan 2026 03:23:00 +0300 Subject: [PATCH] Initial commit --- config.example.toml | 1 + justfile | 2 +- src/trudex/__main__.py | 18 ---------- src/trudex/application/__main__.py | 43 +++++++++++++++++++++++ src/trudex/application/bot/handlers.py | 6 ++++ src/trudex/infrastructure/di.py | 4 +++ src/trudex/infrastructure/utils/config.py | 18 +++++----- 7 files changed, 65 insertions(+), 27 deletions(-) delete mode 100644 src/trudex/__main__.py create mode 100644 src/trudex/application/__main__.py diff --git a/config.example.toml b/config.example.toml index 517019f..a85fbf9 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,5 +1,6 @@ [bot] token = "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz" +creator_id = 123456789 [database] host = "localhost" diff --git a/justfile b/justfile index 569df4a..e8c867f 100644 --- a/justfile +++ b/justfile @@ -2,7 +2,7 @@ set windows-shell := ["powershell.exe", "-NoLogo", "-Command"] set shell := ["bash", "-c"] dev: - watchfiles --filter python "python -m trudex.application" src + watchfiles --filter python ".venv/Scripts/python -m trudex.application" src run: python -m trudex diff --git a/src/trudex/__main__.py b/src/trudex/__main__.py deleted file mode 100644 index a7daa55..0000000 --- a/src/trudex/__main__.py +++ /dev/null @@ -1,18 +0,0 @@ -import asyncio -import logging - -from aiogram import Bot, Dispatcher - -from trudex.infrastructure.utils.config import Config - - -async def main(): - logging.basicConfig(level=logging.INFO) - - config = Config.from_toml("config.toml") - - logging.info("Бот запущен") - - -if __name__ == "__main__": - asyncio.run(main()) diff --git a/src/trudex/application/__main__.py b/src/trudex/application/__main__.py new file mode 100644 index 0000000..e344ca8 --- /dev/null +++ b/src/trudex/application/__main__.py @@ -0,0 +1,43 @@ +import asyncio +import logging + +from aiogram import Bot, Dispatcher +from aiogram.client.default import DefaultBotProperties +from aiogram.enums import ParseMode +from dishka import make_async_container +from dishka.integrations.aiogram import setup_dishka + +from trudex.application.bot.handlers import router +from trudex.infrastructure.di import DatabaseProvider +from trudex.infrastructure.utils.config import Config + + +async def main() -> None: + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" + ) + + config = Config.from_toml("config.toml") + + bot = Bot( + token=config.bot.token, + default=DefaultBotProperties(parse_mode=ParseMode.HTML) + ) + + dp = Dispatcher() + dp.include_router(router) + + container = make_async_container(DatabaseProvider()) + setup_dishka(container, dp) + + logging.info("Бот запущен") + + try: + await dp.start_polling(bot) + finally: + await bot.session.close() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/src/trudex/application/bot/handlers.py b/src/trudex/application/bot/handlers.py index 1e22eb6..d66aea4 100644 --- a/src/trudex/application/bot/handlers.py +++ b/src/trudex/application/bot/handlers.py @@ -1,3 +1,9 @@ from aiogram import Router +from aiogram.filters import CommandStart +from aiogram.types import Message router = Router() + +@router.message(CommandStart()) +async def start_handler(message: Message) -> None: + await message.answer("Привет! Я бот для тестирования по охране труда.") diff --git a/src/trudex/infrastructure/di.py b/src/trudex/infrastructure/di.py index beff360..0536352 100644 --- a/src/trudex/infrastructure/di.py +++ b/src/trudex/infrastructure/di.py @@ -14,6 +14,10 @@ from trudex.infrastructure.utils.config import Config class DatabaseProvider(Provider): + @provide(scope=Scope.APP) + def get_config(self) -> Config: + return Config.from_toml("config.toml") + @provide(scope=Scope.APP) def get_session_maker(self, config: Config) -> async_sessionmaker[AsyncSession]: return new_session_maker(config.database.url) diff --git a/src/trudex/infrastructure/utils/config.py b/src/trudex/infrastructure/utils/config.py index 8cc30a7..5ffb45c 100644 --- a/src/trudex/infrastructure/utils/config.py +++ b/src/trudex/infrastructure/utils/config.py @@ -7,6 +7,7 @@ from typing import Self @dataclass class BotConfig: token: str + creator_id: int @dataclass @@ -30,20 +31,21 @@ class Config: @classmethod def from_toml(cls, path: str | Path) -> Self: with open(path, "rb") as f: - data: dict[str, dict[str, str]] = tomllib.load(f) + data: dict[str, dict[str, str | int]] = tomllib.load(f) - bot_data: dict[str, str] = data["bot"] - db_data: dict[str, str] = data["database"] + bot_data: dict[str, str | int] = data["bot"] + db_data: dict[str, str | int] = data["database"] return cls( bot=BotConfig( - token=bot_data["token"] + token=str(bot_data["token"]), + creator_id=int(bot_data["creator_id"]) ), database=DatabaseConfig( - host=db_data["host"], + host=str(db_data["host"]), port=db_data["port"], - user=db_data["user"], - password=db_data["password"], - database=db_data["database"] + user=str(db_data["user"]), + password=str(db_data["password"]), + database=str(db_data["database"]) ) )