openclaw - 💡(How to fix) Fix Telegram media download intermittently fails with TypeError: fetch failed in Docker [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#49008Fetched 2026-04-08 00:49:51
View on GitHub
Comments
1
Participants
2
Timeline
1
Reactions
0
Author
Timeline (top)
commented ×1

Telegram DM photo uploads intermittently fail with MediaFetchError at the file download step (https://api.telegram.org/file/bot...), even though Telegram updates and text messages are received normally.

Error Message

Error / Stack (key parts)

Root Cause

Telegram DM photo uploads intermittently fail with MediaFetchError at the file download step (https://api.telegram.org/file/bot...), even though Telegram updates and text messages are received normally.

Fix Action

Fix / Workaround

Local Mitigation Experiment

  • Added 3 retries around fetchRemoteMedia(...) in downloadAndSaveTelegramFile(...).
  • Added fallback to direct https.get when fetch retries still fail.
  • This appears to reduce/absorb intermittent failures.
RAW_BUFFERClick to expand / collapse

Summary

Telegram DM photo uploads intermittently fail with MediaFetchError at the file download step (https://api.telegram.org/file/bot...), even though Telegram updates and text messages are received normally.

Environment

  • OpenClaw: 2026.3.12
  • Runtime: Docker container (bridge network)
  • Channel: Telegram private DM
  • Node.js: v24.14.0
  • DNS: api.telegram.org -> IPv4, dnsResultOrder=ipv4first, autoSelectFamily=true

Observed Behavior

  • Photo sent via Telegram DM -> user sees: Failed to download media. Please try again.
  • Text messages are ingested normally.
  • Telegram update receipt is normal.
  • Failure occurs at file download stage (not getFile metadata stage).

Error / Stack (key parts)

MediaFetchError: Failed to fetch media from https://api.telegram.org/file/bot.../photos/file_2.jpg: TypeError: fetch failed

Call path:

  • fetchRemoteMedia(...)
  • downloadAndSaveTelegramFile(...)
  • resolveMedia(...)
  • processInboundMessage(...)

Reproduction Notes

  • Inside the same container, manual fetch(...) / https.get(...) against the same file_2.jpg URL returns 200 and valid bytes.
  • Therefore this appears intermittent and runtime-path specific, not a permanent network block.

Code Observation

  • getFile() path already includes retry logic.
  • Actual file download path (downloadAndSaveTelegramFile -> fetchRemoteMedia) has no built-in retry.
  • This makes the pipeline vulnerable to transient TypeError: fetch failed.

Local Mitigation Experiment

  • Added 3 retries around fetchRemoteMedia(...) in downloadAndSaveTelegramFile(...).
  • Added fallback to direct https.get when fetch retries still fail.
  • This appears to reduce/absorb intermittent failures.

Requested Upstream Fix

  1. Add built-in retry to Telegram file download path (not just getFile).
  2. Improve diagnostics for fetchRemoteMedia failures:
    • include cause
    • classify socket/DNS/TLS/timeout where possible
  3. Consider fallback transport (https.get or explicit undici strategy) for Telegram file downloads.
  4. Validate whether intermittent fetch failed reproduces in Node 22+/24 with Docker bridge networking.

Expected Behavior

Transient download failures should be retried and usually recovered automatically, rather than surfacing user-facing media download failure on first attempt.

extent analysis

Fix Plan

To address the intermittent MediaFetchError issue, we will implement the following steps:

  • Add built-in retry logic to the Telegram file download path
  • Improve diagnostics for fetchRemoteMedia failures
  • Consider a fallback transport for Telegram file downloads

Code Changes

Here's an example of how you can modify the downloadAndSaveTelegramFile function to include retry logic and a fallback transport:

const fetch = require('node-fetch');
const https = require('https');

async function downloadAndSaveTelegramFile(url, retries = 3) {
  for (let i = 0; i <= retries; i++) {
    try {
      const response = await fetch(url);
      if (response.ok) {
        // Save the file
        return;
      } else {
        throw new Error(`Failed to download file: ${response.status}`);
      }
    } catch (error) {
      if (i < retries) {
        // Retry after a short delay
        await new Promise(resolve => setTimeout(resolve, 500));
      } else {
        // Fallback to https.get if all retries fail
        return downloadAndSaveTelegramFileUsingHttpsGet(url);
      }
    }
  }
}

function downloadAndSaveTelegramFileUsingHttpsGet(url) {
  return new Promise((resolve, reject) => {
    https.get(url, response => {
      if (response.statusCode === 200) {
        // Save the file
        resolve();
      } else {
        reject(new Error(`Failed to download file: ${response.statusCode}`));
      }
    }).on('error', error => {
      reject(error);
    });
  });
}

Verification

To verify that the fix worked, you can test the downloadAndSaveTelegramFile function with a URL that previously failed intermittently. You can also monitor the error logs to see if the number of MediaFetchError instances has decreased.

Extra Tips

  • Make sure to handle errors properly and provide useful diagnostics to help with debugging.
  • Consider using a more robust retry library like retry-as-promised to handle retries and backoff strategies.
  • If you're using a Docker bridge network, ensure that the DNS settings are correct and that the container has access to the necessary resources.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix Telegram media download intermittently fails with TypeError: fetch failed in Docker [1 comments, 2 participants]