Forums

aiogram.exceptions.TelegramNetworkError: HTTP Client says - ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]

Hello everyone, I wrote a telegram bot on the aiogram library, but this error occurs when starting: aiogram.exceptions.TelegramNetworkError: HTTP Client says - ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable].

I understand that this error is related to the proxy, I tried to fix it, but it still does not work. People who understand aiogram, please tell me what to add to my code so that it works

Here is the code:

from aiogram import Bot, Dispatcher, types
import asyncio
import logging
from aiogram import F


dp = Dispatcher()
bot = Bot(token='TOKEN')
logging.basicConfig(level=logging.INFO


@dp.message(F.text)
async def eho_bot(message: types.Message):
    await message.answer(message.text)


async def main():
    await dp.start_polling(bot, skip_updates=True)

if __name__ == "__main__":
    asyncio.run(main())

(I'm uploading for the first time, sorry for the English, I'm using Google translator)

It doesn't look like you're using a proxy here. How did you try to set up a proxy?

That's right, in this code I did not try to set up a proxy, an hour-long search in an attempt to find an answer on the Internet was unsuccessful, in desperation I turned to the gpt chat, but the code that he suggested did not work:

from aiogram import Bot, Dispatcher, types
import asyncio
import logging
from aiogram import F

dp = Dispatcher()
bot = Bot(token='TOKEN')
logging.basicConfig(level=logging.INFO)
proxy_url = 'http://proxy.server:3128'


@dp.message(F.text)
async def eho_bot(message: types.Message):
        await message.answer(message.text)


async def main():
    proxy = types.Proxy(type=types.ProxyType.HTTP, url=proxy_url)
    await bot.set_proxy(proxy)
    await dp.start_polling(bot, skip_updates=True)

if __name__ == "__main__":
        asyncio.run(main())

So I went to the forum in the hope that a person who understands aiogram and faced the same problem will tell you how to solve this error.

What can you see in your logs when you run that code?

When running the second code in the console, an error is generated:

Traceback (most recent call last):

File "/home/BogomDan/bot_2.py", line 23, in < module >

asyncio.run(main())

File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run

return loop.run_until_complete(main)

File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete

return future.result()

File "/home/BogomDan/bot_2.py", line 18, in main

proxy = types.Proxy(type=types.ProxyType.HTTP, url=proxy_url)

AttributeError: module 'aiogram.types' has no attribute 'Proxy'

Check out this help page -- it has different code for setting up the proxy.

it looks like the code from this page is for aiogram 2.x (I'm using aiogram 3.x). Because I get this error:

Traceback (most recent call last): File "/home/BogomDan/bot_2.py", line 9, in <module> bot = Bot(token='6264757222:AAGiHjCBrSve_SxYX5FcrFL6epC0EsOuG8E', proxy=PROXY_URL) TypeError: Bot.init() got an unexpected keyword argument 'proxy'

The documentation for v3.x - https://docs.aiogram.dev/en/dev-3.x/