Initial commit

This commit is contained in:
2026-01-01 03:23:00 +03:00
parent bf04bde890
commit 046a117b09
7 changed files with 65 additions and 27 deletions
+1
View File
@@ -1,5 +1,6 @@
[bot] [bot]
token = "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz" token = "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz"
creator_id = 123456789
[database] [database]
host = "localhost" host = "localhost"
+1 -1
View File
@@ -2,7 +2,7 @@ set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
set shell := ["bash", "-c"] set shell := ["bash", "-c"]
dev: dev:
watchfiles --filter python "python -m trudex.application" src watchfiles --filter python ".venv/Scripts/python -m trudex.application" src
run: run:
python -m trudex python -m trudex
-18
View File
@@ -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())
+43
View File
@@ -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())
+6
View File
@@ -1,3 +1,9 @@
from aiogram import Router from aiogram import Router
from aiogram.filters import CommandStart
from aiogram.types import Message
router = Router() router = Router()
@router.message(CommandStart())
async def start_handler(message: Message) -> None:
await message.answer("Привет! Я бот для тестирования по охране труда.")
+4
View File
@@ -14,6 +14,10 @@ from trudex.infrastructure.utils.config import Config
class DatabaseProvider(Provider): class DatabaseProvider(Provider):
@provide(scope=Scope.APP)
def get_config(self) -> Config:
return Config.from_toml("config.toml")
@provide(scope=Scope.APP) @provide(scope=Scope.APP)
def get_session_maker(self, config: Config) -> async_sessionmaker[AsyncSession]: def get_session_maker(self, config: Config) -> async_sessionmaker[AsyncSession]:
return new_session_maker(config.database.url) return new_session_maker(config.database.url)
+10 -8
View File
@@ -7,6 +7,7 @@ from typing import Self
@dataclass @dataclass
class BotConfig: class BotConfig:
token: str token: str
creator_id: int
@dataclass @dataclass
@@ -30,20 +31,21 @@ class Config:
@classmethod @classmethod
def from_toml(cls, path: str | Path) -> Self: def from_toml(cls, path: str | Path) -> Self:
with open(path, "rb") as f: 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"] bot_data: dict[str, str | int] = data["bot"]
db_data: dict[str, str] = data["database"] db_data: dict[str, str | int] = data["database"]
return cls( return cls(
bot=BotConfig( bot=BotConfig(
token=bot_data["token"] token=str(bot_data["token"]),
creator_id=int(bot_data["creator_id"])
), ),
database=DatabaseConfig( database=DatabaseConfig(
host=db_data["host"], host=str(db_data["host"]),
port=db_data["port"], port=db_data["port"],
user=db_data["user"], user=str(db_data["user"]),
password=db_data["password"], password=str(db_data["password"]),
database=db_data["database"] database=str(db_data["database"])
) )
) )