使用Telegram本地API发送大文件时出现请求超时错误
通过Telegram本地API Server发送大文件时的超时问题
问题描述
使用aiogram库编写程序,通过Telegram本地API Server发送1.5GB的.mov文件时,触发超时错误:aiogram.exceptions.TelegramNetworkError: HTTP Client says - Request timeout error.
程序执行时,文件先复制到本地服务器,再由本地服务器上传至Telegram服务器,能看到网络负载,但30-60秒后触发超时错误。不过程序崩溃后,文件仍会成功上传到Telegram。
完整报错堆栈
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
测试代码
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())
环境配置
- 系统:Win10
- Telegram本地API Server启动参数:
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 - 手动向本地服务器发送请求可正常响应,服务器运行状态正常。
内容的提问来源于stack exchange,提问作者Mikhail Byran
相关产品推荐
相关产品推荐

