feat(*): init
This commit is contained in:
33
backend/src/bot/__init__.py
Normal file
33
backend/src/bot/__init__.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import asyncio
|
||||
import contextlib
|
||||
|
||||
from rich import traceback
|
||||
|
||||
from utils.logging import logger, setup_logging
|
||||
|
||||
setup_logging()
|
||||
|
||||
|
||||
async def runner() -> None:
|
||||
from . import handlers # noqa: PLC0415
|
||||
from .common import bot, dp # noqa: PLC0415
|
||||
|
||||
dp.include_routers(handlers.router)
|
||||
|
||||
await bot.delete_webhook(drop_pending_updates=True)
|
||||
await dp.start_polling(bot)
|
||||
|
||||
|
||||
def plugins() -> None:
|
||||
traceback.install(show_locals=True)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
plugins()
|
||||
|
||||
logger.info("Starting...")
|
||||
|
||||
with contextlib.suppress(KeyboardInterrupt):
|
||||
asyncio.run(runner())
|
||||
|
||||
logger.info("[red]Stopped.[/]")
|
||||
4
backend/src/bot/__main__.py
Normal file
4
backend/src/bot/__main__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from . import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
15
backend/src/bot/common.py
Normal file
15
backend/src/bot/common.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from aiogram import Bot, Dispatcher
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
from aiogram.fsm.storage.memory import MemoryStorage
|
||||
|
||||
from utils import env
|
||||
|
||||
bot = Bot(
|
||||
token=env.bot.token.get_secret_value(),
|
||||
default=DefaultBotProperties(parse_mode="HTML"),
|
||||
)
|
||||
storage = MemoryStorage()
|
||||
dp = Dispatcher(storage=storage)
|
||||
|
||||
|
||||
__all__ = ["bot", "dp"]
|
||||
1
backend/src/bot/factories/__init__.py
Normal file
1
backend/src/bot/factories/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
1
backend/src/bot/filters/__init__.py
Normal file
1
backend/src/bot/filters/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
7
backend/src/bot/handlers/__init__.py
Normal file
7
backend/src/bot/handlers/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from aiogram import Router
|
||||
|
||||
from . import initialize, start
|
||||
|
||||
router = Router()
|
||||
|
||||
router.include_routers(start.router, initialize.router)
|
||||
3
backend/src/bot/handlers/initialize/__init__.py
Normal file
3
backend/src/bot/handlers/initialize/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .initializer import router
|
||||
|
||||
__all__ = ["router"]
|
||||
18
backend/src/bot/handlers/initialize/initializer.py
Normal file
18
backend/src/bot/handlers/initialize/initializer.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from aiogram import Bot, Router, types
|
||||
|
||||
from utils.logging import logger
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.startup()
|
||||
async def startup(bot: Bot) -> None:
|
||||
await bot.set_my_commands(
|
||||
[types.BotCommand(command="/start", description="Start bot")]
|
||||
)
|
||||
logger.info(f"[green]Started as[/] @{(await bot.me()).username}")
|
||||
|
||||
|
||||
@router.shutdown()
|
||||
async def shutdown() -> None:
|
||||
logger.info("Shutting down bot...")
|
||||
3
backend/src/bot/handlers/start/__init__.py
Normal file
3
backend/src/bot/handlers/start/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .start import router
|
||||
|
||||
__all__ = ["router"]
|
||||
9
backend/src/bot/handlers/start/start.py
Normal file
9
backend/src/bot/handlers/start/start.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from aiogram import Router, types
|
||||
from aiogram.filters import CommandStart
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.message(CommandStart())
|
||||
async def on_start(message: types.Message) -> None:
|
||||
await message.answer("hi")
|
||||
1
backend/src/bot/keyboards/__init__.py
Normal file
1
backend/src/bot/keyboards/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
pass
|
||||
4
backend/src/utils/__init__.py
Normal file
4
backend/src/utils/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .env import env
|
||||
from .logging import logger
|
||||
|
||||
__all__ = ["env", "logger"]
|
||||
27
backend/src/utils/env.py
Normal file
27
backend/src/utils/env.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from pydantic import AliasChoices, Field, SecretStr
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class BotSettings(BaseSettings):
|
||||
token: SecretStr
|
||||
|
||||
|
||||
class LogSettings(BaseSettings):
|
||||
level: str = "INFO"
|
||||
level_external: str = "WARNING"
|
||||
show_time: bool = False
|
||||
console_width: int = 150
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
bot: BotSettings
|
||||
log: LogSettings
|
||||
|
||||
convex_url: str = Field(validation_alias=AliasChoices("CONVEX_SELF_HOSTED_URL"))
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
case_sensitive=False, env_file=".env", env_nested_delimiter="__", extra="ignore"
|
||||
)
|
||||
|
||||
|
||||
env = Settings() # ty:ignore[missing-argument]
|
||||
35
backend/src/utils/logging.py
Normal file
35
backend/src/utils/logging.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import logging
|
||||
|
||||
from aiogram.dispatcher import router
|
||||
from rich.console import Console
|
||||
from rich.logging import RichHandler
|
||||
from rich.traceback import install
|
||||
|
||||
from .env import env
|
||||
|
||||
console = Console(width=env.log.console_width, color_system="auto", force_terminal=True)
|
||||
|
||||
|
||||
def setup_logging() -> None:
|
||||
logging.basicConfig(
|
||||
level=env.log.level_external,
|
||||
format="",
|
||||
datefmt=None,
|
||||
handlers=[
|
||||
RichHandler(
|
||||
console=console,
|
||||
markup=True,
|
||||
rich_tracebacks=True,
|
||||
enable_link_path=False,
|
||||
tracebacks_show_locals=True,
|
||||
omit_repeated_times=False,
|
||||
show_time=env.log.show_time,
|
||||
tracebacks_suppress=[router],
|
||||
)
|
||||
],
|
||||
)
|
||||
install(console=console, show_locals=True)
|
||||
|
||||
|
||||
logger = logging.getLogger("telegram-casino")
|
||||
logger.setLevel(env.log.level)
|
||||
Reference in New Issue
Block a user