Skip to content

fix: use proxy for send_message_tool Telegram media uploads#20902

Closed
blackdeathdrow wants to merge 1 commit into
NousResearch:mainfrom
blackdeathdrow:fix/telegram-proxy-send-message
Closed

fix: use proxy for send_message_tool Telegram media uploads#20902
blackdeathdrow wants to merge 1 commit into
NousResearch:mainfrom
blackdeathdrow:fix/telegram-proxy-send-message

Conversation

@blackdeathdrow

Copy link
Copy Markdown

What does this PR do?

send_message_tool fails to send photos/media via Telegram when a proxy is configured in ~/.hermes/.env. The gateway adapter works fine because it loads .env and uses HTTPXRequest(proxy=...), but the fallback _send_telegram() function creates a plain Bot(token=token) without any proxy configuration.

Root cause:

  1. .env file is never loaded in send_message_tool.py, so TELEGRAM_PROXY env var is empty
  2. resolve_proxy_url("TELEGRAM_PROXY", ...) returns None because the environment isn't populated
  3. Bot(token=token) is created without proxy → connection to Telegram API fails behind proxy

Solution:

  1. Load .env at module level (lines 18-24): loads once on import, makes all env vars available via os.environ for any function in the module
  2. Resolve and use proxy in _send_telegram(): read TELEGRAM_PROXY from environment using resolve_proxy_url(), create HTTPXRequest(proxy=proxy_url), pass it to Bot(token=token, request=httpx_request)

Related Issue

Fixes # (no issue number yet — please create one if needed)

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • tools/send_message_tool.py: +20 lines, -1 line
    • Added module-level .env loading wrapped in try/except for safety
    • Inside _send_telegram(): proxy resolution via resolve_proxy_url() and HTTPXRequest creation
    • Bot now uses request=bot_request parameter instead of plain constructor

How to Test

  1. Configure TELEGRAM_PROXY=socks5://... in ~/.hermes/.env (or use HTTPS proxy)
  2. Ensure direct Telegram API access is blocked or slow so proxy is required
  3. Run the following Python command to send a photo:
cd ~/.hermes/hermes-agent && python3 << 'EOF'
from tools.send_message_tool import send_message_tool

result = send_message_tool({
    "action": "send",
    "target": "telegram:<chat_id>",
    "message": 
})

import json
print(json.dumps(result, indent=2))
EOF
  1. Verify the photo arrives in Telegram without timeout errors and result["success"] is true.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix: ...)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass (run locally before merging)
  • I've added tests for my changes (should add test in tools/test_send_message_tool.py)
  • I've tested on my platform: Ubuntu 24.04 (WSL)

Documentation & Housekeeping

  • I've updated relevant documentation — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

Before fix: Failed to send media ...jpg: Timed out (no proxy used)
After fix: Photo delivered successfully, Message ID received.

@alt-glitch alt-glitch added type/bug Something isn't working comp/tools Tool registry, model_tools, toolsets platform/telegram Telegram bot adapter P2 Medium — degraded but workaround exists labels May 6, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related: #12370 and #18714 also fix the Telegram proxy missing from send_message_tool. This PR is narrower (only the proxy gap, no WeChat fix), but overlaps significantly with the Telegram proxy portion of #12370.

@blackdeathdrow blackdeathdrow requested a review from a team May 15, 2026 11:54
@blackdeathdrow blackdeathdrow marked this pull request as draft May 15, 2026 12:01
@blackdeathdrow blackdeathdrow force-pushed the fix/telegram-proxy-send-message branch from 7fa29e9 to 69c8f3e Compare May 15, 2026 12:19
@blackdeathdrow blackdeathdrow marked this pull request as ready for review May 15, 2026 12:35
@austinpickett austinpickett requested a review from Copilot May 19, 2026 12:25

@austinpickett austinpickett left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix merge conflicts and resolve copilot comments

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes tools/send_message_tool.py’s Telegram fallback sender so it can upload/send media successfully when a Telegram proxy is configured, aligning its behavior with the gateway adapter’s proxy handling.

Changes:

  • Load the Hermes user .env so TELEGRAM_PROXY is available when the tool runs standalone.
  • In _send_telegram(), resolve TELEGRAM_PROXY via resolve_proxy_url(...) and configure python-telegram-bot’s Bot with an HTTPXRequest that includes the proxy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +18 to +21
# Load .env file to make TELEGRAM_PROXY and other env vars available
try:
from dotenv import load_dotenv
load_dotenv(os.path.expanduser("~/.hermes/.env"), override=True)
Comment on lines +19 to +23
try:
from dotenv import load_dotenv
load_dotenv(os.path.expanduser("~/.hermes/.env"), override=True)
except ImportError:
pass # python-dotenv not required at module level
Comment on lines +714 to +719
# Create Bot with proxy support via HTTPXRequest
request_kwargs = {}
if proxy_url:
request_kwargs["proxy"] = proxy_url
bot_request = HTTPXRequest(**request_kwargs)
bot = Bot(token=token, request=bot_request)
@blackdeathdrow blackdeathdrow force-pushed the fix/telegram-proxy-send-message branch from 69c8f3e to 1e485e6 Compare May 19, 2026 13:48
@blackdeathdrow blackdeathdrow deleted the fix/telegram-proxy-send-message branch May 19, 2026 13:53
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.

4 participants