68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
|
|
from aiogram import Bot, Dispatcher
|
|
from aiogram.client.default import DefaultBotProperties
|
|
from aiogram.enums import ParseMode
|
|
from aiogram.types import (
|
|
BotCommand,
|
|
BotCommandScopeAllGroupChats,
|
|
BotCommandScopeAllPrivateChats,
|
|
)
|
|
|
|
from app.config import Settings
|
|
from app.database import Database
|
|
from app.handlers import create_router
|
|
from app.scheduler import daily_report_loop, stop_task
|
|
|
|
|
|
async def main() -> None:
|
|
settings = Settings.from_env()
|
|
logging.basicConfig(
|
|
level=getattr(logging, settings.log_level, logging.INFO),
|
|
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
|
)
|
|
|
|
database = Database(settings.database_path)
|
|
await database.initialize()
|
|
bot = Bot(
|
|
token=settings.bot_token,
|
|
default=DefaultBotProperties(parse_mode=ParseMode.HTML),
|
|
)
|
|
dispatcher = Dispatcher()
|
|
dispatcher.include_router(create_router(database, settings))
|
|
|
|
await bot.set_my_commands(
|
|
[
|
|
BotCommand(command="start", description="Открыть главное меню"),
|
|
BotCommand(command="month", description="Итог за месяц"),
|
|
BotCommand(command="total", description="Итог за всё время"),
|
|
BotCommand(command="analytics", description="Расходы по категориям"),
|
|
BotCommand(command="recent", description="Последние записи"),
|
|
BotCommand(command="family", description="Семейный бюджет"),
|
|
BotCommand(command="debt", description="Общий долг"),
|
|
BotCommand(command="categories", description="Категории и подкатегории"),
|
|
BotCommand(command="help", description="Как пользоваться ботом"),
|
|
],
|
|
scope=BotCommandScopeAllPrivateChats(),
|
|
)
|
|
await bot.set_my_commands(
|
|
[BotCommand(command="family_chat", description="Подключить семейный чат")],
|
|
scope=BotCommandScopeAllGroupChats(),
|
|
)
|
|
|
|
report_task = asyncio.create_task(daily_report_loop(bot, database))
|
|
try:
|
|
await dispatcher.start_polling(
|
|
bot,
|
|
allowed_updates=dispatcher.resolve_used_update_types(),
|
|
)
|
|
finally:
|
|
await stop_task(report_task)
|
|
await bot.session.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|