From 282eb837ac480d7e51d26974e6a0a36e6d4e67a5 Mon Sep 17 00:00:00 2001 From: avelytchko <919635+avelytchko@users.noreply.github.com> Date: Fri, 13 Mar 2026 15:22:59 +0100 Subject: [PATCH 1/2] Increase Telegram HTTPXRequest timeouts to improve reliability - Added TELEGRAM_CONNECT_TIMEOUT=30 and TELEGRAM_POOL_TIMEOUT=10 - Set default timeouts for all requests via HTTPXRequest(connect_timeout, pool_timeout, read_timeout, write_timeout) - Explicitly passed connect_timeout to send_video / send_photo / send_media_group calls for consistency - Prevents TimedOut / ConnectTimeout errors during LLM replies and rate-limit messages --- src/main.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index d3d070e..510f72b 100644 --- a/src/main.py +++ b/src/main.py @@ -17,6 +17,7 @@ from telegram.error import TimedOut, NetworkError, TelegramError from telegram.ext import Application, MessageHandler, filters, ContextTypes from telegram.constants import MessageEntityType +from telegram.request import HTTPXRequest from logger import error, info, debug from general_error_handler import error_handler from permissions import inform_user_not_allowed, is_user_or_chat_not_allowed, supported_sites @@ -47,8 +48,10 @@ GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-flash-latest") GROK_API_KEY = os.getenv("GROK_API_KEY") GROK_MODEL = os.getenv("GROK_MODEL", "grok-4-latest") -TELEGRAM_WRITE_TIMEOUT = 8000 -TELEGRAM_READ_TIMEOUT = 8000 +TELEGRAM_CONNECT_TIMEOUT = 30 +TELEGRAM_POOL_TIMEOUT = 10 +TELEGRAM_READ_TIMEOUT = 60 +TELEGRAM_WRITE_TIMEOUT = 60 # Configure Gemini API if GEMINI_API_KEY: @@ -408,6 +411,7 @@ async def send_video(update: Update, video, has_spoiler: bool) -> None: height=height, has_spoiler=has_spoiler, disable_notification=True, + connect_timeout=TELEGRAM_CONNECT_TIMEOUT, write_timeout=TELEGRAM_WRITE_TIMEOUT, read_timeout=TELEGRAM_READ_TIMEOUT, reply_to_message_id=update.message.message_id, @@ -436,6 +440,7 @@ async def send_video(update: Update, video, has_spoiler: bool) -> None: await update.message.chat.send_media_group( media=media_group, disable_notification=True, + connect_timeout=TELEGRAM_CONNECT_TIMEOUT, write_timeout=TELEGRAM_WRITE_TIMEOUT, read_timeout=TELEGRAM_READ_TIMEOUT, ) @@ -468,6 +473,7 @@ async def send_pic(update: Update, pic) -> None: await update.message.chat.send_photo( photo=pic_file, disable_notification=True, + connect_timeout=TELEGRAM_CONNECT_TIMEOUT, write_timeout=TELEGRAM_WRITE_TIMEOUT, read_timeout=TELEGRAM_READ_TIMEOUT, ) @@ -494,6 +500,7 @@ async def send_pic(update: Update, pic) -> None: await update.message.chat.send_media_group( media=media_group, disable_notification=True, + connect_timeout=TELEGRAM_CONNECT_TIMEOUT, write_timeout=TELEGRAM_WRITE_TIMEOUT, read_timeout=TELEGRAM_READ_TIMEOUT, ) @@ -947,7 +954,13 @@ def main(): None """ bot_token = os.getenv("BOT_TOKEN") - application = Application.builder().token(bot_token).build() + request = HTTPXRequest( + connect_timeout=TELEGRAM_CONNECT_TIMEOUT, + pool_timeout=TELEGRAM_POOL_TIMEOUT, + read_timeout=TELEGRAM_READ_TIMEOUT, + write_timeout=TELEGRAM_WRITE_TIMEOUT, + ) + application = Application.builder().token(bot_token).request(request).build() application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # This handler will receive every error which happens in your bot application.add_error_handler(error_handler) From ad1de8380c40ab85a537486f675ec07a7e4ace05 Mon Sep 17 00:00:00 2001 From: avelytchko <919635+avelytchko@users.noreply.github.com> Date: Fri, 13 Mar 2026 15:32:32 +0100 Subject: [PATCH 2/2] Fix linter --- src/main.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main.py b/src/main.py index 510f72b..ab72811 100644 --- a/src/main.py +++ b/src/main.py @@ -48,10 +48,10 @@ GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-flash-latest") GROK_API_KEY = os.getenv("GROK_API_KEY") GROK_MODEL = os.getenv("GROK_MODEL", "grok-4-latest") -TELEGRAM_CONNECT_TIMEOUT = 30 -TELEGRAM_POOL_TIMEOUT = 10 -TELEGRAM_READ_TIMEOUT = 60 -TELEGRAM_WRITE_TIMEOUT = 60 +TELEGRAM_CONNECT_TIMEOUT = 60 +TELEGRAM_POOL_TIMEOUT = 30 +TELEGRAM_READ_TIMEOUT = 120 +TELEGRAM_WRITE_TIMEOUT = 120 # Configure Gemini API if GEMINI_API_KEY: @@ -955,11 +955,11 @@ def main(): """ bot_token = os.getenv("BOT_TOKEN") request = HTTPXRequest( - connect_timeout=TELEGRAM_CONNECT_TIMEOUT, - pool_timeout=TELEGRAM_POOL_TIMEOUT, - read_timeout=TELEGRAM_READ_TIMEOUT, - write_timeout=TELEGRAM_WRITE_TIMEOUT, - ) + connect_timeout=TELEGRAM_CONNECT_TIMEOUT, + pool_timeout=TELEGRAM_POOL_TIMEOUT, + read_timeout=TELEGRAM_READ_TIMEOUT, + write_timeout=TELEGRAM_WRITE_TIMEOUT, + ) application = Application.builder().token(bot_token).request(request).build() application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # This handler will receive every error which happens in your bot