Files
Media-download-telegram-bot/.env.example
T
MichilisandClaude Fable 5.2 9c9d641eda Phase 3: cache
Repeat downloads are instant, and the cache never bypasses a limit.

- internal/cache: canonical key (extractor + video id + profile, URL hash
  fallback), per-bot file_id layer, stored copy layer, metadata layer,
  TTL by source type, LRU-plus-hits eviction that keeps file_ids, orphan
  sweeper, takedown that deletes, invalidates and blocks
- One admission gate: a cache hit passes the same size, duration, quota,
  volume, active job and queue checks as a fresh download, is judged by
  the real cached file, and costs the same. It only skips the worker
- Singleflight: a second requester joins the job that is queued or running
  for the same result. Each keeps their own caps, checked at delivery
- Derive: lower profiles and audio are transcoded from a stored copy with
  ffmpeg; a higher profile that already fits is served as an alias; a
  failed derive falls back to a download
- Self-healing: a rejected file_id falls back to the stored copy, then to
  a fresh download, and the entry is updated either way
- Private or login-gated content and live streams are never cached
- Playlists as per-item jobs started in turn, /subs, /history with resend,
  /fresh, owner /purge and /cache
- Worker removes its temp files right after the upload

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 08:38:38 +00:00

148 lines
5.3 KiB
Bash

# Copy to .env and fill in. The core reads .env from its working directory
# and never overrides variables that are already set in the environment.
# Structured settings can also live in a TOML file, see config.example.toml.
# --- Telegram ---------------------------------------------------------------
# Token from @BotFather. Required.
BOT_TOKEN=
# Bot API root. Point it at a local Bot API server to send files up to 2 GB.
TELEGRAM_API_URL=https://api.telegram.org
# polling (default) or webhook.
TELEGRAM_MODE=polling
# Public HTTPS base URL of this server. Only for webhook mode. Telegram will
# call <WEBHOOK_URL>/telegram/webhook.
WEBHOOK_URL=
# Random string Telegram echoes back in a header. Only for webhook mode.
WEBHOOK_SECRET=
# Largest file the bot can send, in MB. 50 on the cloud Bot API.
TELEGRAM_MAX_UPLOAD_MB=50
# Goroutines that handle updates.
TELEGRAM_UPDATE_WORKERS=8
# Telegram user IDs allowed to use owner commands, comma separated.
OWNER_IDS=
# Public HTTPS base URL of this server, without a trailing slash. Used for
# payment webhooks, "open in wallet" links and the Mini App. Payments work
# without it (invoices are then settled by polling only).
PUBLIC_URL=
# --- HTTP -------------------------------------------------------------------
# Listen address of the health endpoint and the webhook.
HTTP_LISTEN=:8080
# --- Database ---------------------------------------------------------------
# sqlite or postgres.
DB_DRIVER=sqlite
# File path for sqlite, connection URL for postgres.
DB_DSN=data/dlbot.db
# --- Storage ----------------------------------------------------------------
# local or s3.
STORAGE_BACKEND=local
STORAGE_LOCAL_DIR=data/files
# --- Worker -----------------------------------------------------------------
# Run a download worker inside the core process.
WORKER_EMBEDDED=true
# Downloads running at the same time.
WORKER_CONCURRENCY=2
# Scratch space for downloads. Each worker process needs its own directory.
WORKER_TEMP_DIR=data/tmp
YTDLP_PATH=yt-dlp
FFMPEG_PATH=ffmpeg
# Hard cap for one download attempt.
DOWNLOAD_TIMEOUT=30m
# Cap for one metadata lookup, and how many may run at once.
PROBE_TIMEOUT=45s
PROBE_CONCURRENCY=4
# --- Queue ------------------------------------------------------------------
# Most queued jobs accepted from free users.
QUEUE_FREE_MAX_LEN=20
# A claimed job goes back to the queue when no heartbeat arrives for this long.
QUEUE_LEASE=30s
QUEUE_HEARTBEAT=10s
# Tries per job. 2 means one automatic retry.
QUEUE_MAX_ATTEMPTS=2
# --- Cache ------------------------------------------------------------------
# The cache only makes repeat downloads faster. It never changes limits.
CACHE_ENABLED=true
# Largest total size of stored copies, in MB. 0 means no cap.
CACHE_BUDGET_MB=20000
# How long a result is served before it is fetched again, by kind of site.
CACHE_TTL=168h
CACHE_TTL_STABLE=720h
CACHE_TTL_EPHEMERAL=6h
# yt-dlp extractor name prefixes for the two special classes.
CACHE_STABLE_SITES=youtube,vimeo,archive.org,soundcloud,dailymotion,peertube,bandcamp
CACHE_EPHEMERAL_SITES=twitter,instagram,tiktok,facebook,reddit,twitch,generic
# How long link metadata is remembered, so cards for known links are instant.
CACHE_META_TTL=30m
# Make lower qualities and audio from a stored copy with ffmpeg instead of
# downloading again.
CACHE_DERIVE=true
CACHE_SWEEP_INTERVAL=10m
# --- Free tier limits -------------------------------------------------------
FREE_MAX_SIZE_MB=50
FREE_MAX_DURATION=10m
FREE_ACTIVE_JOBS=1
# Per UTC day. 0 means no cap.
FREE_DAILY_DOWNLOADS=5
FREE_DAILY_MB=250
# Items fetched from one playlist link.
FREE_PLAYLIST_ITEMS=3
# --- Paid limits (passes, credits, one-off payments) ------------------------
# Sizes above TELEGRAM_MAX_UPLOAD_MB need a local Bot API server.
PAID_MAX_SIZE_MB=2000
PAID_MAX_DURATION=3h
PAID_ACTIVE_JOBS=3
PAID_DAILY_DOWNLOADS=100
# The daily volume cap applies to paying users too.
PAID_DAILY_MB=20000
PAID_PLAYLIST_ITEMS=25
# --- Payments (LNbits) ------------------------------------------------------
# Leave LNBITS_URL and LNBITS_INVOICE_KEY empty to run without payments.
LNBITS_URL=
# The wallet's INVOICE key (read only). Never give the bot the admin key.
LNBITS_INVOICE_KEY=
# Random string. It becomes part of the webhook path that LNbits calls:
# <PUBLIC_URL>/lnbits/webhook/<LNBITS_WEBHOOK_SECRET>
LNBITS_WEBHOOK_SECRET=
# How often unsettled invoices are checked, as a fallback for lost webhooks.
LNBITS_POLL_INTERVAL=5s
# How long an invoice can be paid.
INVOICE_EXPIRY=10m
# --- Prices in sats ---------------------------------------------------------
PRICE_PASS_DAY=700
PRICE_PASS_WEEK=3000
PRICE_PASS_MONTH=9000
# Cost of one download when credits pay.
PRICE_CREDIT_DOWNLOAD=50
# "Pay for this file only": base plus a step per started 100 MB.
PRICE_ONE_OFF_BASE=100
PRICE_ONE_OFF_PER_100MB=50
# Moving one waiting free job to the priority lane.
PRICE_SKIP_QUEUE=50
# Top-up and tip amounts offered as buttons.
CREDIT_PACKS=1000,5000,20000
DONATION_AMOUNTS=500,2100,10000
# Credited to the inviter on an invited user's first purchase. 0 turns it off.
REFERRAL_REWARD=200
# --- Rate limits ------------------------------------------------------------
# Links per minute. 0 means no limit.
RATE_USER_PER_MINUTE=10
RATE_GROUP_PER_MINUTE=30
# --- Logging ----------------------------------------------------------------
# debug, info, warn or error.
LOG_LEVEL=info
# json or text.
LOG_FORMAT=json