Feat: minimal configuration

This commit is contained in:
2024-12-26 13:39:26 +03:00
parent 3e81929747
commit dae0df1a63
8 changed files with 73 additions and 1 deletions

27
main.py Normal file
View File

@@ -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))

View File

@@ -0,0 +1 @@
from nwxraybot.config import Settings

0
nwxraybot/bot.py Normal file
View File

View File

@@ -3,4 +3,19 @@ from pydantic_settings import BaseSettings
class Settings(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"

View File

@@ -0,0 +1 @@
from nwxraybot.handlers.hello import HelloHandler

View File

@@ -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")

View File

@@ -0,0 +1 @@
from nwxraybot.meta.router import Handler

14
nwxraybot/meta/router.py Normal file
View File

@@ -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