async def main():
    # Настройка логирования в stdout
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
    )

    bot = Bot(token=config.bot_token.get_secret_value())
    dp = Dispatcher()
    router = setup_routers()
    dp.include_router(router)

    if config.custom_bot_api:
        bot.session.api = TelegramAPIServer.from_base(config.custom_bot_api,
                                                      is_local=True)

    # Регистрация /-команд в интерфейсе
    await set_bot_commands(bot)

    try:
        if not config.webhook_domain:
            await bot.delete_webhook()
            await dp.start_polling(
                bot, allowed_updates=dp.resolve_used_update_types())
        else:
            # Выключаем логи от aiohttp
            aiohttp_logger = logging.getLogger("aiohttp.access")
            aiohttp_logger.setLevel(logging.CRITICAL)

            # Установка вебхука
            await bot.set_webhook(
                url=config.webhook_domain + config.webhook_path,
                drop_pending_updates=True,
                allowed_updates=dp.resolve_used_update_types())

            # Создание запуска aiohttp
            app = web.Application()
            SimpleRequestHandler(dispatcher=dp,
                                 bot=bot).register(app,
                                                   path=config.webhook_path)
            runner = web.AppRunner(app)
            await runner.setup()
            site = web.TCPSite(runner,
                               host=config.app_host,
                               port=config.app_port)
            await site.start()

            # Бесконечный цикл
            await asyncio.Event().wait()
    finally:
        await bot.session.close()
Пример #2
0
def include_routers(dp: Dispatcher):
    dp.include_router("bot.handlers.start:router")
    dp.include_router("bot.handlers.new_chat_members:router")
    dp.include_router("bot.handlers.added_to_group:router")
    dp.include_router("bot.handlers.settings.settings:router")
    dp.include_router("bot.handlers.settings.callbacks:router")
    dp.include_router("bot.handlers.captcha:router")
Пример #3
0
async def run_polling():
    dp = Dispatcher()
    for router in all_routers:
        dp.include_router(router)
    bot = Bot(TELEGRAM_BOT_TOKEN)
    await dp.start_polling(bot)