Search code examples
pythontelegram-bot

Telegram Bot Local API Server TimeOut when sending big file


I'm trying to send a large file using the local API Server. But when the file is uploaded I get the error aiogram.exceptions.TelegramNetworkError: HTTP Client says - Request timeout error.

import asyncio
import logging
import sys
import json
from aiogram import Bot, Dispatcher, html
from aiogram.client.default import DefaultBotProperties
from aiogram.client import telegram
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import Message, BufferedInputFile, InputFile, FSInputFile, URLInputFile
from aiogram.client.session.aiohttp import AiohttpSession
with open('secrets.json', 'r') as f:
    secrets=json.loads(f.read())
token = secrets['token']
session = AiohttpSession(
    api=telegram.TelegramAPIServer.from_base('http://127.0.0.1:4200')
)
dp = Dispatcher()

@dp.message()
async def echo_handler(message: Message) -> None:
    """
    Handler will forward receive a message back to the sender

    By default, message handler will handle all message types (like a text, photo, sticker etc.)
    """
    with open("./storage/Background Main.mov", 'rb') as f:
        inputFile=FSInputFile("./storage/Background Main.mov")

        await message.answer('sending test')
        await message.answer_document(inputFile)


async def main() -> None:
    bot = Bot(token=token, default=DefaultBotProperties(parse_mode=ParseMode.HTML), session=session)
    await dp.start_polling(bot, polling_timeout=90)
if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    asyncio.run(main())

I wrote this small program to try to reproduce the error on aoigram library. Initially I used Telebot. So, I'm just trying to send a 1.5gb file. When my program tries to send the file, it seems to copy the file first to the local server, and then the local server starts uploading to the telegram server. I see the network load at this time(i have 200mbit/s bandwith). But after 30-60 seconds, the error occurs, however, even after the program crashes, the file will be uploaded to Telegram and will arrive in a message. The full error code in python is:

INFO:aiogram.event:Update id=871987320 is not handled. Duration 60500 ms by bot id=8178713988
ERROR:aiogram.event:Cause exception while process update id=871987320 by bot id=8178713988
TelegramNetworkError: HTTP Client says - Request timeout error
Traceback (most recent call last):
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\client\session\aiohttp.py", line 181, in make_request
    async with session.post(
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiohttp\client.py", line 1353, in __aenter__
    self._resp = await self._coro
                 ^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiohttp\client.py", line 684, in _request
    await resp.start(conn)
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiohttp\client_reqrep.py", line 994, in start
    with self._timer:
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiohttp\helpers.py", line 713, in __exit__
    raise asyncio.TimeoutError from None
TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 309, in _process_update
    response = await self.feed_update(bot, update, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 158, in feed_update
    response = await self.update.wrap_outer_middleware(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\middlewares\error.py", line 25, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\middlewares\user_context.py", line 56, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\fsm\middleware.py", line 42, in __call__
    return await handler(event, data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 276, in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\router.py", line 146, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\router.py", line 141, in _wrapped
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\router.py", line 166, in _propagate_event
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\event\telegram.py", line 121, in trigger
    return await wrapped_inner(event, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\dispatcher\event\handler.py", line 43, in call
    return await wrapped()
           ^^^^^^^^^^^^^^^
  File "G:\Python\TelegramProject\timeout_test.py", line 54, in echo_handler
    await message.answer_document(inputFile)
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\methods\base.py", line 84, in emit
    return await bot(self)
           ^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\client\bot.py", line 488, in __call__
    return await self.session(self, method, timeout=request_timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\client\session\base.py", line 254, in __call__
    return cast(TelegramType, await middleware(bot, method))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "g:\Python\TelegramProject\venv\Lib\site-packages\aiogram\client\session\aiohttp.py", line 186, in make_request
    raise TelegramNetworkError(method=method, message="Request timeout error")
aiogram.exceptions.TelegramNetworkError: HTTP Client says - Request timeout error

I am using Win10, https://github.com/tdlib/telegram-bot-api command line argumens is "telegram-bot-api.exe --api-id=ID --api-hash="HASH" --local -p 4200 -s 4201 -c 100 --max-webhook-connections=250 -l log.txt -v 2"

I tried to send a request to the local server manually, and it responded to me. That is, the server is alive. If I set -v 2, then you can see this in the logs. If I set 3, then the log file is too big and it is too difficult to understand something.Logs


Solution

  • Increase timeout (or read_timeout) in send_document method.