[Bug]: Photo/document download fails with a SOCKS proxy — file-download-service always uses HttpsProxyAgent
What is broken?
Sending a photo (or a document / media group) while TELEGRAM_PROXY_URL points to a SOCKS proxy fails with "🔴 Failed to download photo". The log shows:
FetchError: request to https://api.telegram.org/file/bot…/photos/file_0.jpg failed,
reason: Proxy connection ended before receiving CONNECT response
Regular Bot API calls keep working, because the grammY client picks the right agent for socks* URLs — only the raw file download breaks. Voice messages also keep working (they use their own downloader).
Steps to reproduce
- Set
TELEGRAM_PROXY_URL=socks5h://127.0.0.1:1080(an SSH tunnel; any SOCKS4/5 proxy shows the same). - Send a photo to the bot.
- The bot replies "⏳ Downloading photo..." and then "🔴 Failed to download photo"; the log contains the
FetchErrorabove.
OS
macOS
Additional context (optional)
Root cause. In src/app/services/file-download-service.ts (still unchanged on main), when proxyUrl is set the agent is unconditionally:
const { HttpsProxyAgent } = await import("https-proxy-agent");
fetchOptions.agent = new HttpsProxyAgent(config.telegram.proxyUrl);
HttpsProxyAgent speaks HTTP CONNECT, so pointing it at a SOCKS server fails the handshake — an HTTP CONNECT request sent into a SOCKS port gets the connection dropped ("Proxy connection ended before receiving CONNECT response"). Reproduced independently: curl -x http://<socks-host>:1080 https://api.telegram.org/ fails the same way, curl --socks5-hostname works.
src/bot/telegram-client-options.ts already handles this correctly by checking proxyUrl.startsWith("socks") and choosing SocksProxyAgent, and voice-handler.ts does the same for its downloader. So photos, documents, media groups and file-browser downloads (all callers of downloadTelegramFile) fail under a SOCKS proxy, while voice messages work.
Suggested fix (mirrors telegram-client-options.ts):
if (config.telegram.proxyUrl) {
const proxyUrl = config.telegram.proxyUrl;
if (proxyUrl.startsWith("socks")) {
const { SocksProxyAgent } = await import("socks-proxy-agent");
fetchOptions.agent = new SocksProxyAgent(proxyUrl);
} else {
const { HttpsProxyAgent } = await import("https-proxy-agent");
fetchOptions.agent = new HttpsProxyAgent(proxyUrl);
}
}
Verified locally: with this change the same SOCKS tunnel that failed before now reaches api.telegram.org (a fake file_path returns a clean HTTP 404 instead of the proxy error).
Seen on 0.24.1; the code on main is unchanged. Happy to open a PR.
Update: confirmed the suggested fix end to end. Applied the same agent selection (socks → SocksProxyAgent) to an installed 0.24.1 build, restarted the bot, and photo download through the SOCKS tunnel now works in real use — photos previously failing with the proxy error above now reach the model as expected. Voice messages were unaffected even before the fix, matching the analysis.
Fixed in https://github.com/grinev/opencode-telegram-bot/commit/7b3a42cb6fc4f7990cafa4458402df16bdb4811b