Skip to content

fix(tools): wire Telegram proxy to Bot in send_message so media uploads stop timing out#27701

Closed
leolulu wants to merge 1 commit into
NousResearch:mainfrom
leolulu:fix/telegram-media-proxy
Closed

fix(tools): wire Telegram proxy to Bot in send_message so media uploads stop timing out#27701
leolulu wants to merge 1 commit into
NousResearch:mainfrom
leolulu:fix/telegram-media-proxy

Conversation

@leolulu

@leolulu leolulu commented May 18, 2026

Copy link
Copy Markdown

Summary

In environments that require a proxy to reach api.telegram.org, the send_message tool fails to deliver any media files (photos, videos, audio, documents) because the Bot instance it creates has no proxy configured — unlike the gateway adapter which correctly uses one.

Problem

The gateway adapter (gateway/platforms/telegram.py) resolves the proxy via resolve_proxy_url("TELEGRAM_PROXY") and passes it to HTTPXRequest, so all gateway replies work fine behind a proxy.

However, the send_message tool (tools/send_message_tool.py) creates its own separate Bot instance without any proxy:

# send_message_tool.py — _send_telegram()
bot = Bot(token=token)  # direct connection to api.telegram.org — no proxy

This bot is used for both text and media sends. In proxy-dependent environments, the TCP connection to api.telegram.org cannot be established directly, causing every send_message call to timeout. The gateway itself is unaffected because it uses a different, properly proxied Bot instance.

Fix

Reuse the same proxy resolution chain as the gateway adapter:

_proxy_url = resolve_proxy_url("TELEGRAM_PROXY", target_hosts=["api.telegram.org"])
if _proxy_url:
    _req = HTTPXRequest(proxy=_proxy_url, connect_timeout=10.0, read_timeout=20.0, write_timeout=20.0)
    bot = Bot(token=token, request=_req)
else:
    bot = Bot(token=token)  # no proxy needed

Timeout values match the gateway adapter (telegram.py lines 1219–1221). When no proxy is configured, behavior is unchanged.

Testing

Verified locally with TELEGRAM_PROXY:

Scenario Before After
send_message with .ogg audio timeout ✅ delivered
send_message with .html file timeout ✅ delivered
send_message with .md file timeout ✅ delivered
Gateway auto-reply (text + media) already worked unchanged

Related

The _send_telegram function in send_message_tool.py created a Bot without
proxy configuration, causing all media file uploads to timeout in
environments that require a proxy to reach api.telegram.org.

The gateway adapter (telegram.py) already resolves the proxy via
resolve_proxy_url('TELEGRAM_PROXY') and passes it to HTTPXRequest, but
the send_message tool's one-shot Bot instance was missing this step.

Fix: call resolve_proxy_url('TELEGRAM_PROXY') and create an HTTPXRequest
with the resolved proxy + matching timeouts (connect=10s, read/write=20s)
before passing it to Bot(request=...).

This aligns the send_message tool's Telegram transport with the gateway
adapter, fixing media uploads (photos, videos, audio, documents) for all
proxy-dependent environments.

Closes #9291 (partial — media delivery timeout due to missing proxy)
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter comp/tools Tool registry, model_tools, toolsets labels May 18, 2026
@leolulu leolulu changed the title fix(tools): pass proxy to Telegram Bot in send_message for media uploads fix(tools): wire Telegram proxy to Bot in send_message so media uploads stop timing out May 18, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #20902 (and overlaps with #25419 and #12370) — all address the same root cause: _send_telegram() in send_message_tool.py creates Bot() without proxy config, causing media upload timeouts behind a proxy.

@teknium1

Copy link
Copy Markdown
Contributor

Closing in favor of #25419 — both fix the same bug (send_message tool's standalone Bot() construction bypasses TELEGRAM_PROXY). #25419 was submitted first, fixes BOTH text and media sends, and has a longer narrative on the runner-ref delegation logic. Your media-only fix is a subset of #25419's scope. Thanks for the parallel work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tools Tool registry, model_tools, toolsets P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants