socket.gaierror: [Errno 11001] getaddrinfo failed problem with Webhooks

42 Views Asked by At

I am dealing with webhooks on aiogram 3.x I have raised a server on railway, everything works fine (at least when I run on pooling). But when I try to run the code I got from the official aiogram documentation (https://docs.aiogram.dev/en/dev-3.x/dispatcher/webhook.html), I get an error:

C:\Users\Антон Белоусов\Desktop\mars\bot.py:80: DeprecationWarning: Passing `parse_mode`, `disable_web_page_preview` or `protect_content` to Bot initializer is deprecated. This arguments will be removed in 3.5.0 version
Use `default=DefaultBotProperties(...)` instead.
  bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
ERROR:asyncio:unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-1' coro=<_run_app() done, defined at c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\web.py:303> exception=gaierror(11001, 'getaddrinfo failed')>
Traceback (most recent call last):
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\web.py", line 544, in run_app
    loop.run_until_complete(main_task)
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in 
run_until_complete
    return future.result()
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\web.py", line 448, in _run_app
    await site.start()
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\site-packages\aiohttp\web_runner.py", 
line 119, in start
    self._server = await loop.create_server(
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1471, in create_server
    infos = await tasks.gather(*fs)
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1408, in _create_server_getaddrinfo
    infos = await self._ensure_resolved((host, port), family=family,
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 1404, in _ensure_resolved
    return await loop.getaddrinfo(host, port, family=family, type=type,
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 860, in 
getaddrinfo
    return await self.run_in_executor(
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "c:\Users\Антон Белоусов\AppData\Local\Programs\Python\Python310\lib\socket.py", line 955, in getaddrinfo  
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

My code:

import logging
import sys
from os import getenv

from aiohttp import web

from aiogram import Bot, Dispatcher, Router, types
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import Message
from aiogram.utils.markdown import hbold
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application

TOKEN = getenv("BOT_TOKEN")
WEB_SERVER_HOST = getenv("API")
WEB_SERVER_PORT = 8081
WEBHOOK_PATH = f"/webhook/{getenv('BOT_TOKEN')}"
BASE_WEBHOOK_URL = "https://curvy-vase-production.up.railway.app/"

router = Router()

@router.message(CommandStart())
async def command_start_handler(message: Message) -> None:
    await message.answer(f"Hello, {hbold(message.from_user.full_name)}!")


@router.message()
async def echo_handler(message: types.Message) -> None:
    try:
        await message.send_copy(chat_id=message.chat.id)
    except TypeError:
        await message.answer("Nice try!")


async def on_startup(bot: Bot) -> None:
    await bot.set_webhook(f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}")


def main() -> None:
    dp = Dispatcher()
    dp.include_router(router)
    dp.startup.register(on_startup)
    bot = Bot(TOKEN, parse_mode=ParseMode.HTML)

    app = web.Application()

    webhook_requests_handler = SimpleRequestHandler(
        dispatcher=dp,
        bot=bot
    )

    webhook_requests_handler.register(app, path=WEBHOOK_PATH)
    setup_application(app, dp, bot=bot)
    web.run_app(app, host=WEB_SERVER_HOST, port=WEB_SERVER_PORT)


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    main()

I try to run the code I got from the official aiogram documentation (https://docs.aiogram.dev/en/dev-3.x/dispatcher/webhook.html), I get the error

0

There are 0 best solutions below