tgbridge: support a self-hosted bot api server, and say when a file is too big

The cloud api refuses getFile over 20 MB, so an oversize video or gif never
reached ffmpeg and the bridge blamed the fetch. Six of the nine media
failures in the last month were that; the other three were a dropped read.

- config.local_api() turns TELEGRAM_API_BASE_URL into base_url/base_file_url/
  local_mode. Both directions read it, or only one of them migrates.
- in local mode getFile hands back a path on disk and ptb reads it directly,
  so there is no 20 MB cap and no http transfer to drop.
- MAX_INGEST_BYTES keeps that from being a way to pull a gigabyte into the
  bot's memory; falls back to stat() when the server omits file_size.
- explicit read_timeout: ptb defaults to 5s, which is what "Timed out" was.
  one retry covers a dropped read. BadRequest subclasses NetworkError, so it
  is re-raised first or an oversize rejection gets re-sent.
- the note now distinguishes too-large from could-not-fetch, and the warning
  records which attachment and which message.

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
esefandClaude Opus 5 committed 2026-10-31 13:36:11 +03:00
1 parent 460f4bf78c
commit 78c755a7a2
4 files changed
+87 -12

No files matched your search

+15
View File
@@ -117,3 +117,18 @@ def load_routes() -> list[Route]:
def avatar_channel_id() -> int | None:
return _int_or_none(os.getenv("AVATAR_CACHE_CHANNEL_ID"))
def local_api() -> dict:
"""-> bot kwargs for a self-hosted bot api server, {} for the cloud one.
A local server lifts telegram's 20 MB getFile cap and hands back a path on
disk instead of a url, which ptb reads for itself once local_mode is on.
Both directions read this or only one of them migrates.
"""
base = os.getenv("TELEGRAM_API_BASE_URL", "").strip().rstrip("/")
if not base:
return {}
return {"base_url": f"{base}/bot",
"base_file_url": f"{base}/file/bot",
"local_mode": True}