The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telegram): send_image uploads still went out on the short read timeout
Drexuxux57a807373d49
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.pyindex d3f48a90481..19e6c923eda 100644--- a/plugins/platforms/telegram/adapter.py+++ b/plugins/platforms/telegram/adapter.py@@ -7275,6 +7275,7 @@ class TelegramAdapter(BasePlatformAdapter):"photo": image_url,"caption": caption[:1024] if caption else None,"reply_to_message_id": reply_to_id,+ "read_timeout": _MEDIA_SEND_READ_TIMEOUT,**photo_thread_kwargs,**self._notification_kwargs(metadata),},@@ -7317,6 +7318,7 @@ class TelegramAdapter(BasePlatformAdapter):"photo": image_data,"caption": caption[:1024] if caption else None,"reply_to_message_id": reply_to_id,+ "read_timeout": _MEDIA_SEND_READ_TIMEOUT,**upload_thread_kwargs,**self._notification_kwargs(metadata),},diff --git a/tests/gateway/test_telegram_media_read_timeout.py b/tests/gateway/test_telegram_media_read_timeout.pynew file mode 100644index 00000000000..941faa5b775--- /dev/null+++ b/tests/gateway/test_telegram_media_read_timeout.py@@ -0,0 +1,135 @@+"""Every Telegram media upload passes the long media read timeout.++Telegram processes an uploaded photo/video/document server-side before it+answers, so the wait for the response is unrelated to how fast the bytes went+out and can outlast the short read timeout the rest of the Bot API is tuned+for. ``_MEDIA_SEND_READ_TIMEOUT`` is the longer budget; a media send that+misses it fails on a slow upload and falls back to posting the bare URL as+text, so the picture never arrives as a picture.++``send_image`` was the gap: both of its ``send_photo`` calls -- the URL send+and the byte-upload fallback that handles up to 10MB -- went out on the short+timeout. Both are driven for real below and asserted on the ``read_timeout``+that actually reaches the Bot API.+"""++from __future__ import annotations++import sys+from unittest.mock import AsyncMock, MagicMock++import pytest+++def _ensure_telegram_mock():+ """Install mock telegram modules only when the real library is absent.++ Probe with a real import rather than inspecting ``sys.modules``: when PTB+ is installed but not yet imported, a ``sys.modules`` check alone would+ install the mock first and poison every later test that needs the real+ library (``tests/test_telegram_polling_progress_ptb.py`` exercises the+ genuine ``BaseRequest``).+ """+ try:+ import telegram # noqa: F401++ return+ except ImportError:+ pass+ telegram_mod = MagicMock()+ telegram_mod.ext.ContextTypes.DEFAULT_TYPE = type(None)+ telegram_mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"+ for name in ("GROUP", "SUPERGROUP", "CHANNEL", "PRIVATE"):+ setattr(telegram_mod.constants.ChatType, name, name.lower())+ for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):+ sys.modules.setdefault(name, telegram_mod)+++_ensure_telegram_mock()++from gateway.config import PlatformConfig # noqa: E402+from plugins.platforms.telegram import adapter as tg # noqa: E402+from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402+++def adapter():+ a = TelegramAdapter(PlatformConfig(enabled=True, token="fake-token"))+ a._bot = MagicMock()+ a._metadata_thread_id = lambda metadata: None+ a._thread_kwargs_for_send = lambda *args, **kwargs: {}+ a._notification_kwargs = lambda metadata: {}+ a._reply_to_message_id_for_send = lambda *args, **kwargs: None++ async def _direct(fn, payload, *args, **kwargs):+ return await fn(**payload)++ a._send_with_dm_topic_reply_anchor_retry = _direct+ return a+++def _stub_download(monkeypatch, size: int):+ """Make the fallback's SSRF-safe client return ``size`` bytes."""++ class _Resp:+ content = b"x" * size++ def raise_for_status(self):+ return None++ class _Client:+ async def __aenter__(self):+ return self++ async def __aexit__(self, *exc):+ return False++ async def get(self, url):+ return _Resp()++ import tools.url_safety as url_safety++ monkeypatch.setattr(url_safety, "create_ssrf_safe_async_client", lambda **kw: _Client())+++async def test_send_image_url_path_uses_media_read_timeout(adapter):+ calls = []++ async def _photo(**kwargs):+ calls.append(kwargs)+ msg = MagicMock()+ msg.message_id = 1+ return msg++ adapter._bot.send_photo = AsyncMock(side_effect=_photo)++ result = await adapter.send_image("123", "https://example.com/pic.png", caption="hi")++ assert result.success+ assert calls[0]["read_timeout"] == tg._MEDIA_SEND_READ_TIMEOUT+++async def test_send_image_upload_fallback_uses_media_read_timeout(adapter, monkeypatch):+ """The >5MB fallback is the slowest send in the file — it needs it most."""+ _stub_download(monkeypatch, 8 * 1024 * 1024)+ calls = []++ async def _photo(**kwargs):+ calls.append(kwargs)+ if len(calls) == 1:+ raise RuntimeError("Photo too big for a URL send")+ msg = MagicMock()+ msg.message_id = 2+ return msg++ adapter._bot.send_photo = AsyncMock(side_effect=_photo)++ result = await adapter.send_image("123", "https://example.com/big.png", caption="hi")++ assert result.success+ assert len(calls) == 2, "expected the byte-upload fallback to run"+ upload = calls[1]+ assert isinstance(upload["photo"], (bytes, bytearray))+ assert upload["read_timeout"] == tg._MEDIA_SEND_READ_TIMEOUT