- Inline mode (INLINE_MODE_ENABLED): "@bot <link>" sends the cached file by file_id when the user's limits admit it without payment, and otherwise one result that opens the bot with /start il_<token> for the link. An inline send counts as a download when Telegram reports it chosen (inline feedback). The bot asks Telegram for inline_query and chosen_inline_result updates. - README, OPERATIONS and ARCHITECTURE describe the new flows, Stars, inline mode and every new setting; docs/SCREENS.md regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
274 lines
13 KiB
Markdown
274 lines
13 KiB
Markdown
# The Noderunners Download Bot
|
|
|
|
A Telegram bot that downloads video and audio from links, in private chats
|
|
and in groups. Written in Go on top of yt-dlp.
|
|
|
|
> Status: Phase 5 of 8 (core bot, payments, cache, scale-out, groups).
|
|
> The Mini App and more languages follow. See
|
|
> [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md) for the decisions made so far.
|
|
|
|
## What works today
|
|
|
|
- Send a link in a private chat and get the file. The first link shows a
|
|
card with title, thumbnail, duration and format buttons. After that the
|
|
bot starts right away with the best quality that fits the user's limit,
|
|
or with the default set in /quality ("Ask every time" brings the card
|
|
back). Up to five links per message; a link sent a moment ago gets
|
|
"already sent" with a button to send it again.
|
|
- In groups: `/dl <link>`, or reply `/dl` to a message with a link. Buttons
|
|
only work for the person who asked. Private chats are never quoted;
|
|
groups are.
|
|
- One status message per download, edited at most every 2.5 seconds:
|
|
downloading with a progress bar (the queue position only when the wait
|
|
is worth mentioning), processing, sending. Once the file is in the chat
|
|
the status message is deleted, so the file is the only message left.
|
|
- The file comes with a clean caption (title, uploader, length, a short
|
|
source link without tracking parameters, optionally "via @bot"), a
|
|
thumbnail, and buttons for the MP3, another quality, the thumbnail, and
|
|
Delete. Errors come with a next step: try again, a smaller quality,
|
|
upgrade, or the app. Passing failures are retried once by themselves.
|
|
- A database backed queue with leases, one automatic retry and cancel.
|
|
- Free tier limits: size, duration, daily quota, one active job.
|
|
- Payments in sats over Lightning (LNbits): day, week and month passes,
|
|
credits, "pay for this file only" and "skip the queue". Paid downloads
|
|
get higher limits and a priority lane, with one slot always kept for free
|
|
users.
|
|
- Telegram Stars as a second way to pay for passes and credits, with
|
|
prices of their own, in the chat and in the Mini App (`STARS_ENABLED`).
|
|
- A referral program in sats: random invite codes, a reward once an
|
|
invited user's first Lightning purchase settles, and a daily cap. Next
|
|
to it, Telegram's own affiliate program for Stars spent in the Mini App
|
|
([docs/AFFILIATE.md](docs/AFFILIATE.md)).
|
|
- Inline mode (`INLINE_MODE_ENABLED`): `@bot <link>` in any chat sends a
|
|
cached file when the user's limits allow it, or opens the bot for it.
|
|
- Money is safe: append-only ledger, idempotent settlement, reconciliation
|
|
at startup, automatic refunds for failed downloads.
|
|
- Gift codes and tips.
|
|
- A smart cache: repeat downloads are instant, two people asking for the
|
|
same video share one download, lower qualities are made from a stored
|
|
copy. A cache hit is checked against exactly the same limits as a fresh
|
|
download.
|
|
- Playlists (each item its own job), subtitles, download history.
|
|
- Scale out: stateless workers that pull jobs over HTTPS with no inbound
|
|
ports, per-worker revocable tokens, tags to route sites to the right
|
|
network, drain, automatic requeue when a worker dies.
|
|
- SQLite or Postgres, local disk or S3, chosen by configuration.
|
|
- Groups: a group wallet and group pass, spending rules, admin-only
|
|
settings with inline toggles, allowed and blocked sites, optional link
|
|
detection, per-member usage.
|
|
- A Mini App inside Telegram: paste a link, watch and cancel the queue,
|
|
buy a pass or credits with a QR invoice that notices the payment, resend
|
|
from history, manage groups (settings, wallet, spending rules, usage by
|
|
member) and personal preferences. It is embedded in the core binary.
|
|
- English, Spanish and Dutch throughout: messages, buttons, command menus,
|
|
the bot's profile texts and the Mini App, with numbers, sizes and dates
|
|
in the language's own format. Each user's Telegram language is
|
|
followed, and `/language` overrides it; `/grouplang` sets a group's.
|
|
- Live widgets in private chats (Bot API 10.4): the download status as a
|
|
progress bar with a cancel button, and the invoice with its QR code, a
|
|
countdown and the paid state. When Telegram refuses a widget the bot
|
|
uses the ordinary message, without anyone noticing.
|
|
- Prometheus metrics on the core and every worker, example alert rules,
|
|
dashboard notes and a load test.
|
|
- Friendly, translated error messages. Raw tool output stays in the logs.
|
|
|
|
## Quick start with docker compose
|
|
|
|
```sh
|
|
cp .env.example .env # set BOT_TOKEN, OWNER_IDS, WORKER_A_TOKEN, WORKER_B_TOKEN
|
|
docker compose up -d --build
|
|
```
|
|
|
|
This runs the core and two workers. [docs/DEPLOY.md](docs/DEPLOY.md) is a
|
|
step by step guide to a production setup with TLS, payments, the Mini App
|
|
and monitoring in under an hour; [docs/OPERATIONS.md](docs/OPERATIONS.md)
|
|
covers Postgres, S3, 2 GB uploads and the other choices.
|
|
|
|
## Quick start from source
|
|
|
|
You need Go 1.26.11 or newer (older Go toolchains download it on their
|
|
own), plus `yt-dlp` and `ffmpeg` on the PATH.
|
|
|
|
```sh
|
|
# 1. Create a bot with @BotFather and copy its token.
|
|
cp .env.example .env
|
|
$EDITOR .env # set BOT_TOKEN
|
|
|
|
# 2. Build and run.
|
|
make build
|
|
./bin/core
|
|
```
|
|
|
|
Open your bot in Telegram, press Start and send it a link.
|
|
|
|
To use the bot in a group, add it to the group. Group privacy mode can stay
|
|
on: the bot only needs to see commands.
|
|
|
|
### Check that it is alive
|
|
|
|
```sh
|
|
curl localhost:8080/healthz
|
|
# {"status":"ok"}
|
|
```
|
|
|
|
## Commands
|
|
|
|
| Command | What it does |
|
|
| --- | --- |
|
|
| `/start` | Welcome message, with a language picker the first time |
|
|
| `/help` | A short introduction and your limits, with sections for commands, account and groups |
|
|
| `/settings` | Your settings in one place (private chat): format, audio only, ask every time, language, pass, balance, invites, "Add me to a group" |
|
|
| `/dl <link>` | Download a video. In groups also works as a reply to a link. |
|
|
| `/audio <link>` | Download the audio as MP3 |
|
|
| `/thumb <link>` | Send the thumbnail |
|
|
| `/subs <link>` | Pick a subtitle language and get the file |
|
|
| `/info <link>` | Show formats and sizes without downloading |
|
|
| `/fresh <link>` | Download again instead of using a saved copy |
|
|
| `/history` | Your past downloads, five per page: send again, the thumbnail, remove one or clear all |
|
|
| `/queue` | Your downloads in progress |
|
|
| `/cancel` | Cancel your downloads |
|
|
| `/account` | Your pass, balance and usage today (private chat) |
|
|
| `/limits` | The limits that apply to you right now |
|
|
| `/buy` | Buy a pass or credits (private chat) |
|
|
| `/credits` | Balance and top up (private chat) |
|
|
| `/redeem <code>` | Use a gift code (private chat) |
|
|
| `/referral` | Your invite link, what it earned, share, copy and QR buttons (private chat) |
|
|
| `/donate` | Send a tip (private chat) |
|
|
| `/quality` | Your default format: ask every time, best that fits your limit, a quality, or audio only |
|
|
| `/language` | Change the bot's language |
|
|
| `/privacy`, `/terms`, `/support` | Short texts (private chat) |
|
|
|
|
In groups:
|
|
|
|
| Command | Who | What it does |
|
|
| --- | --- | --- |
|
|
| `/groupwallet`, `/grouppass` | anyone | Group balance and pass, with a link to top up |
|
|
| `/settings` | admins | Quick switches |
|
|
| `/grouplang <code>` | admins | Language of the bot in the group |
|
|
| `/autodetect on\|off` | admins | Fetch pasted links without a command |
|
|
| `/allow <site>`, `/block <site>` | admins | Site lists |
|
|
| `/spending <rule>` | admins | Who may spend group credits |
|
|
| `/groupstats` | admins | Usage by member, last 7 days |
|
|
|
|
Owner commands (private chat, only for the IDs in `OWNER_IDS`):
|
|
|
|
| Command | What it does |
|
|
| --- | --- |
|
|
| `/purge <link>` | Take a video down: delete its files, block it for good |
|
|
| `/cache` | Cache size, hit rate and volume saved |
|
|
| `/workers` | Workers, their state, load, tags and yt-dlp version |
|
|
| `/drain <worker>` | Let a worker finish its jobs and take no new ones |
|
|
| `/ytdlp update` | Update yt-dlp on every worker now |
|
|
| `/stats`, `/queue all` | Numbers (revenue by payment method included), and every active job |
|
|
| `/user <id>`, `/group <id>` | Look up a user or a group |
|
|
| `/ban <id>`, `/unban <id>` | Ban a user |
|
|
| `/grant`, `/credit`, `/refund` | Passes, credits and gift codes. `/refund <purchase ID>` returns a Stars payment. |
|
|
| `/blocklist` | Block sites or single videos |
|
|
| `/broadcast <text>` | Message every user, after a confirmation |
|
|
| `/maint on\|off` | Maintenance mode |
|
|
| `/reload i18n` | Reload translations from `LOCALES_DIR` |
|
|
|
|
### Turning on payments
|
|
|
|
Payments are off until LNbits is configured. You need an LNbits wallet and
|
|
its **invoice key** (not the admin key: the bot never needs to spend).
|
|
|
|
```sh
|
|
LNBITS_URL=https://your-lnbits.example.com
|
|
LNBITS_INVOICE_KEY=...
|
|
# Optional but recommended, for instant settlement and the wallet button:
|
|
PUBLIC_URL=https://bot.example.com
|
|
LNBITS_WEBHOOK_SECRET=$(openssl rand -hex 16)
|
|
```
|
|
|
|
Without `PUBLIC_URL` payments still work: invoices are settled by polling
|
|
LNbits every few seconds.
|
|
|
|
## Configuration
|
|
|
|
Everything is configured through environment variables. `.env.example`
|
|
lists all of them with comments. A TOML file with the same settings is
|
|
optional, see `config.example.toml`; the environment overrides the file.
|
|
|
|
The settings you are most likely to touch:
|
|
|
|
| Variable | Default | Meaning |
|
|
| --- | --- | --- |
|
|
| `BOT_TOKEN` | none | Token from @BotFather. Required. |
|
|
| `TELEGRAM_MODE` | `polling` | `polling` or `webhook` |
|
|
| `WORKER_CONCURRENCY` | `2` | Downloads running at the same time |
|
|
| `FREE_MAX_SIZE_MB` | `50` | Size cap per download |
|
|
| `FREE_MAX_DURATION` | `10m` | Duration cap per download |
|
|
| `FREE_ACTIVE_JOBS` | `1` | Active downloads per user |
|
|
| `FREE_DAILY_DOWNLOADS` | `5` | Downloads per day on the free tier |
|
|
| `PRICE_PASS_DAY` / `_WEEK` / `_MONTH` | `700` / `3000` / `9000` | Pass prices in sats |
|
|
| `PRICE_CREDIT_DOWNLOAD` | `50` | Cost of a download paid with credits |
|
|
| `QUEUE_FREE_MAX_LEN` | `20` | Most queued free jobs |
|
|
| `DB_DSN` | `data/dlbot.db` | SQLite file |
|
|
| `PUBLIC_URL` | none | HTTPS address of the core, for webhooks, wallet links and the Mini App |
|
|
| `MINIAPP_ENABLED` | `false` | Turn the Mini App on, see [docs/OPERATIONS.md](docs/OPERATIONS.md#the-mini-app) |
|
|
| `LOG_LEVEL` | `info` | `debug`, `info`, `warn`, `error` |
|
|
|
|
## Development
|
|
|
|
```sh
|
|
make check # gofmt, go vet, tests with the race detector
|
|
make test # tests only
|
|
make e2e # real yt-dlp against a public test file, needs network
|
|
make test-postgres PG_DSN=postgres://... # the same suite on Postgres
|
|
make acceptance # kill a worker mid-job, watch the other finish
|
|
make i18n-check # translation completeness and consistency only
|
|
make miniapp # rebuild the Mini App front end after changing web/miniapp
|
|
make miniapp-e2e # click through the Mini App and the widgets in headless Chromium
|
|
make loadtest # many simulated users against a real core and workers
|
|
```
|
|
|
|
The tests need neither a bot token nor network access. They run the real
|
|
bot against a fake Bot API server (`internal/tg/fakeserver`) and a scripted
|
|
yt-dlp.
|
|
|
|
Go is all you need to build: the Mini App's compiled front end is committed
|
|
in `web/miniapp/dist` and embedded in the core. Node.js is only needed to
|
|
change it. A test fails when `dist` is older than its sources.
|
|
|
|
## Documentation
|
|
|
|
- [docs/DEPLOY.md](docs/DEPLOY.md): from nothing to production in under an hour.
|
|
- [docs/OPERATIONS.md](docs/OPERATIONS.md): deploying and running it.
|
|
- [docs/MONITORING.md](docs/MONITORING.md): metrics, dashboards, alerts, load test.
|
|
- [docs/WORKERS.md](docs/WORKERS.md): adding download workers, tags, drain.
|
|
- [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md): how the pieces fit.
|
|
- [docs/TRANSLATING.md](docs/TRANSLATING.md): editing a catalog, adding a language.
|
|
- [docs/AFFILIATE.md](docs/AFFILIATE.md): opening Telegram's affiliate program for the bot.
|
|
- [docs/SCREENS.md](docs/SCREENS.md): every screen of the bot in English, Spanish and Dutch, generated by a test.
|
|
- [docs/UPDATE_NOTES.md](docs/UPDATE_NOTES.md): what the latest round of changes found and changed.
|
|
- [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md): decisions and verified facts.
|
|
|
|
## Layout
|
|
|
|
```
|
|
cmd/core the bot gateway, with an embedded worker by default
|
|
cmd/worker the standalone download worker
|
|
cmd/faketelegram a fake Bot API for development and acceptance runs
|
|
cmd/loadtest simulated users for load tests
|
|
deploy Dockerfile, systemd units, acceptance script
|
|
internal/bot Telegram handlers, cards, status, delivery
|
|
internal/service download rules shared by all front ends
|
|
internal/entitle who pays and which limits apply (pure function)
|
|
internal/billing wallets, ledger, passes, invoices, LNbits client
|
|
internal/cache file_id and stored-copy cache
|
|
internal/ffmpeg transcoding from a stored copy
|
|
internal/sched the job queue
|
|
internal/worker the download worker
|
|
internal/ytdlp yt-dlp and ffprobe wrapper
|
|
internal/db migrations and queries
|
|
internal/storage file storage backends
|
|
internal/i18n translations and formatting
|
|
internal/widget the HTML widgets (status, invoice) and their limits
|
|
internal/metrics Prometheus metrics of the core and the worker
|
|
internal/api HTTP: health, payment webhook, worker protocol, Mini App API
|
|
web/miniapp the Mini App front end (Vite, React) and its embedded build
|
|
locales translation catalogs
|
|
docs architecture and assumptions
|
|
```
|