diff --git a/main.py b/main.py new file mode 100644 index 0000000..ebb0f48 --- /dev/null +++ b/main.py @@ -0,0 +1,27 @@ +import asyncio +import logging +from sys import exit + +import uvloop +from aiogram import Bot, Dispatcher + +from nwxraybot import Settings +from nwxraybot.handlers import HelloHandler +from nwxraybot.models import User + +if __name__ == "__main__": + config = Settings() # Load config from .env + logging.basicConfig(level=logging.DEBUG if config.debug else logging.INFO) + + print(config.postgres_url) + + # Check if bot token is set + if config.bot_token == 'token': + logging.error("Bot token is not set") + exit(1) + + # Start bot + bot = Bot(token=config.bot_token.get_secret_value()) + dp = Dispatcher() + dp.include_routers(HelloHandler(bot)()) + uvloop.run(dp.start_polling(bot, skip_updates=True)) diff --git a/nwxraybot/__init__.py b/nwxraybot/__init__.py index e69de29..c39c74f 100644 --- a/nwxraybot/__init__.py +++ b/nwxraybot/__init__.py @@ -0,0 +1 @@ +from nwxraybot.config import Settings diff --git a/nwxraybot/bot.py b/nwxraybot/bot.py new file mode 100644 index 0000000..e69de29 diff --git a/nwxraybot/config.py b/nwxraybot/config.py index 13d2b06..a707528 100644 --- a/nwxraybot/config.py +++ b/nwxraybot/config.py @@ -3,4 +3,19 @@ from pydantic_settings import BaseSettings class Settings(BaseSettings): - bot_token: SecretStr = Field(..., env="BOT_TOKEN") + bot_token: SecretStr = Field('token', env="BOT_TOKEN") + debug: bool = Field(False, env="DEBUG") + postgres_user: str = Field('user', env="POSTGRES_USER") + postgres_password: SecretStr = Field('password', env="POSTGRES_PASSWORD") + postgres_db: str = Field('db', env="POSTGRES_DB") + postgres_host: str = Field('localhost', env="POSTGRES_HOST") + postgres_port: int = Field(15432, env="POSTGRES_PORT") + + @property + def postgres_url(self) -> str: + return f"postgresql://{self.postgres_user}:{ + self.postgres_password.get_secret_value()}@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}" + + class Config: + env_file = ".env" + env_file_encoding = "utf-8" diff --git a/nwxraybot/handlers/__init__.py b/nwxraybot/handlers/__init__.py new file mode 100644 index 0000000..a03cc0c --- /dev/null +++ b/nwxraybot/handlers/__init__.py @@ -0,0 +1 @@ +from nwxraybot.handlers.hello import HelloHandler diff --git a/nwxraybot/handlers/hello.py b/nwxraybot/handlers/hello.py new file mode 100644 index 0000000..fcf9a4f --- /dev/null +++ b/nwxraybot/handlers/hello.py @@ -0,0 +1,13 @@ +from aiogram import types +from aiogram.filters import Command + +from nwxraybot.meta import Handler + + +class HelloHandler(Handler): + def __init__(self, bot) -> None: + super().__init__(bot) + + @self.router.message(Command("start")) + async def hello(message: types.Message): + await message.reply("Приветствуем в боте NwXray! Здесь вы сможете получить информацию о своем подключении к NwXray") diff --git a/nwxraybot/meta/__init__.py b/nwxraybot/meta/__init__.py new file mode 100644 index 0000000..861c0de --- /dev/null +++ b/nwxraybot/meta/__init__.py @@ -0,0 +1 @@ +from nwxraybot.meta.router import Handler diff --git a/nwxraybot/meta/router.py b/nwxraybot/meta/router.py new file mode 100644 index 0000000..9db9f49 --- /dev/null +++ b/nwxraybot/meta/router.py @@ -0,0 +1,14 @@ +from aiogram import Bot, Router + + +class Handler: + bot: Bot + router: Router + + def __init__(self, bot: Bot) -> None: + assert isinstance(bot, Bot) + self.bot = bot + self.router = Router() + + def __call__(self) -> Router: + return self.router