forked from mrroilead/stars-link-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (66 loc) · 2.12 KB
/
Copy pathmain.py
File metadata and controls
78 lines (66 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Redeploy triggered for new catalog
import asyncio
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.types import BotCommand
from aiogram_i18n import I18nMiddleware
from aiogram_i18n.cores.fluent_compile_core import FluentCompileCore
from bot.commands import (
payment_router,
start_router,
)
from bot.core import config, logger, setup_logging
async def set_bot_commands(bot: Bot) -> None:
commands: list[BotCommand] = [
BotCommand(command="start", description="Start shopping for prompts"),
]
await bot.set_my_commands(commands)
async def setup_i18n() -> I18nMiddleware:
i18n_core: FluentCompileCore = FluentCompileCore(path="locales/{locale}")
await i18n_core.startup()
return I18nMiddleware(core=i18n_core, default_locale="en")
def setup_middlewares(dp: Dispatcher, i18n: I18nMiddleware) -> None:
dp.update.middleware(i18n)
dp.message.middleware(i18n)
dp.callback_query.middleware(i18n)
i18n.setup(dispatcher=dp)
async def main() -> None:
setup_logging()
from bot.core import config
logger.info(f"STARTUP: Loaded {len(config.PRODUCTS)} products")
bot = Bot(
token=config.BOT_TOKEN,
default=DefaultBotProperties(
parse_mode=ParseMode.HTML,
link_preview_is_disabled=True,
),
)
await set_bot_commands(bot)
i18n = await setup_i18n()
dp = Dispatcher()
# Register only necessary routers for MVP
for router in [
start_router,
payment_router,
]:
dp.include_router(router)
setup_middlewares(dp, i18n)
try:
await dp.start_polling(
bot,
polling_timeout=30,
handle_as_tasks=True,
tasks_concurrency_limit=100,
close_bot_session=True,
)
finally:
await i18n.core.shutdown()
await bot.session.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
pass
except Exception as exc:
logger.exception(f"Unexpected error: {exc}")