From 31fab4c19000b15f0dbd2295492e23162965112a Mon Sep 17 00:00:00 2001 From: Shakti Prasad Mohapatra Date: Wed, 22 Jul 2026 13:25:30 +0530 Subject: [PATCH] fix: pass explicit basename to send_document and fix silent error handling in Telegram media send MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three fixes in the Telegram _send_telegram function: 1. **Explicit filename for send_document:** Added filename=os.path.basename(media_path) to all three send_document call sites (primary send, thread-not-found retry, and caption-parse retry). Without an explicit filename, python-telegram-bot derives the filename from the file object, which can be a temp path or truncated — causing silent delivery failure on some platforms. 2. **Caption-fallback warning propagation:** The caption-fallback failure handler (when the file is gone and the caption text is sent alone) logged the error via logger.warning but never appended it to the warnings list, making the failure invisible to the agent. Now warnings.append() is called alongside the log. 3. **Voice/audio handling in caption parse retry:** The caption parse retry block was missing the voice and audio type checks that exist in the primary try block and the thread-not-found retry block. If an audio or voice file encountered a caption parse error, it would fall through to send_document instead of send_voice/send_audio, potentially failing or misrepresenting the media type. Fixes #67552 --- tools/send_message_tool.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/send_message_tool.py b/tools/send_message_tool.py index 6cccee7eddfb9..140bc27a6d25f 100644 --- a/tools/send_message_tool.py +++ b/tools/send_message_tool.py @@ -1302,6 +1302,7 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No "Telegram caption-fallback send failed for missing media: %s", _sanitize_error_text(_cap_err), ) + warnings.append(_sanitize_error_text(_cap_err)) continue ext = os.path.splitext(media_path)[1].lower() @@ -1342,7 +1343,7 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No ) else: last_msg = await bot.send_document( - chat_id=int_chat_id, document=f, **media_kwargs + chat_id=int_chat_id, document=f, filename=os.path.basename(media_path), **media_kwargs ) except Exception as media_err: if _is_telegram_thread_not_found(media_err) and media_kwargs.get("message_thread_id"): @@ -1373,7 +1374,7 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No ) else: last_msg = await bot.send_document( - chat_id=int_chat_id, document=f, **media_kwargs + chat_id=int_chat_id, document=f, filename=os.path.basename(media_path), **media_kwargs ) elif media_kwargs.get("parse_mode") and ( "parse" in str(media_err).lower() @@ -1402,9 +1403,17 @@ async def _send_telegram(token, chat_id, message, media_files=None, thread_id=No last_msg = await bot.send_video( chat_id=int_chat_id, video=f, **media_kwargs ) + elif ext in _VOICE_EXTS and is_voice: + last_msg = await bot.send_voice( + chat_id=int_chat_id, voice=f, **media_kwargs + ) + elif ext in _TELEGRAM_SEND_AUDIO_EXTS: + last_msg = await bot.send_audio( + chat_id=int_chat_id, audio=f, **media_kwargs + ) else: last_msg = await bot.send_document( - chat_id=int_chat_id, document=f, **media_kwargs + chat_id=int_chat_id, document=f, filename=os.path.basename(media_path), **media_kwargs ) else: raise