Timeout Error with Local API Server for Large File Upload in Telegram Bot

I am attempting to upload a sizable file using the local API Server, but I encounter a timeout error during the process. The specific error returned is aiogram.exceptions.TelegramNetworkError: HTTP Client says - Request timeout error. Here’s a portion of my code:

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, FSInputFile
from aiogram.client.session.aiohttp import AiohttpSession

with open('config.json', 'r') as file:
    config = json.load(file)

api_token = config['token']

session_instance = AiohttpSession(
    api=telegram.TelegramAPIServer.from_base('http://127.0.0.1:4200')
)

dispatcher = Dispatcher()

@dispatcher.message()
async def file_sender_handler(message: Message) -> None:
    with open('./files/large_video.mp4', 'rb') as video_file:
        file_to_send = FSInputFile('./files/large_video.mp4')
        await message.reply('Starting file upload...')
        await message.reply_document(file_to_send)

async def run_bot() -> None:
    bot = Bot(token=api_token, default=DefaultBotProperties(parse_mode=ParseMode.HTML), session=session_instance)
    await dispatcher.start_polling(bot, polling_timeout=90)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    asyncio.run(run_bot())

The issue arises when I upload a 1.5 GB file. The program seems to initially copy the file to the local server before it begins the upload to Telegram. My internet connection has a bandwidth of 200 Mbit/s, so the server should be able to process it. However, after approximately 30 to 60 seconds, I receive the timeout error, even though the file manages to upload to Telegram after the program fails. The error traceback includes a TimeoutError occurring during the update processing. Can anyone suggest a solution?

One potential solution could be to adjust the keep-alive timeout settings on your local API server. This timeout manages how long the server maintains a connection open while waiting for additional data. This value could be too low for your transfer, causing the timeout errors. You might also experiment with alternative methods like using asynchronous file reading and writing to improve the file transfer speed, which often helps in handling large file sizes. Double-check for any configuration limitations and ensure your server is optimized for heavy data loads.

try upping the timeout settings or chunking the file to smaller parts, 'cause real big files can sometimes cause bottlenecks, even locally. also, if possible, increase server resources or check server logs for more clues. waht’s your server resource usage when this happens?

hey, maybe the issue is with the aiohttp session’s default timeout. try setting a higher timeout value in the AiohttpSession parameters or during the client session setup. sometimes increasing this helps with large transfers n avoids timeout! hope that works for ya.