Files
balance_bot/main.py
2026-01-23 04:45:55 +07:00

72 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from asyncio import run, sleep
from sys import exit
from bot import *
from configs.config import Webhook
from database import db
from middleware import logger
async def on_startup() -> None:
"""Действия при запуске бота."""
# Создание логера
logger.setup()
# Создание базы данных
await db.setup()
# Настройка информации о боте
await BotInfo.setup(bots=bot)
# Настройка middleware
setup_middlewares(
dp=dp,
bot=bot,
channel_ids=[],
)
# Подключение роутеров
dp.include_router(router)
# Вывод информации о боте
BotInfo.start_info_out()
async def run_polling() -> None:
"""Запуск в режиме polling."""
try:
await on_startup()
await dp.start_polling(bot, allowed_updates=dp.resolve_used_update_types())
finally:
await bot.session.close()
async def run_webhook() -> None:
"""Запуск в режиме webhook."""
app: WebhookApp = WebhookApp(host=Webhook.WEBHOOK_HOST, port=Webhook.WEBHOOK_PORT)
try:
await on_startup()
await app.start()
# держим процесс живым
while True:
await sleep(3600)
finally:
await app.stop()
await bot.session.close()
async def main() -> None:
# Запуск в нужном режиме
if Webhook.WEBHOOK:
await run_webhook()
else:
await run_polling()
if __name__ == "__main__":
try:
run(main())
except (KeyboardInterrupt, SystemExit):
logger.info("❌ Бот остановлен!")
exit(0)