Phase 2: money

Lightning payments through LNbits, wallets, passes, credits, the
entitlement resolver and priority lanes.

- internal/entitle: pure resolver (one-off, own pass, group pass, group
  credits, own credits, free) with a table-driven suite and an invariant
  check over every input combination
- internal/billing: wallets, append-only ledger with dedupe keys, stacking
  passes, purchases, gift codes, referral reward
- LNbits client verified against v0.12.12 and v1.7.1 source
- Settlement by one conditional UPDATE: idempotent under any mix of
  webhooks, polls and the check button. Webhooks are unsigned, so every
  payment is confirmed with LNbits before it is credited
- Polling fallback, reconciliation of all unsettled invoices at startup,
  late payments still honored, no expiry while LNbits is unreachable
- Charge, quota and job are written in one transaction; failed, cancelled
  and undeliverable downloads are refunded exactly once
- Paid lane first with one slot of the fleet reserved for free users
- Daily download quota and daily volume cap, per-user and per-group rate
  limits
- Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice
  message that flips to paid, over-limit offer (passes or this file only),
  deep links from groups, skip the queue
- Fake LNbits server for tests; end-to-end money flows including a payment
  made while the core was down

Cashu redemption is behind an interface and not implemented.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 08:09:35 +00:00
co-authored by Claude Fable 5.2
parent 3b361c17a1
commit 0d50f453a0
42 changed files with 7040 additions and 140 deletions
+37 -2
View File
@@ -3,7 +3,7 @@
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 1 of 8 (core bot). Payments, the cache, remote workers,
> Status: Phase 2 of 8 (core bot and payments). The cache, remote workers,
> group settings, the Mini App and more languages follow. See
> [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md) for the decisions made so far.
@@ -16,7 +16,14 @@ and in groups. Written in Go on top of yt-dlp.
- One status message, edited live: queued with position and wait,
downloading with a progress bar, processing, sending, done.
- A database backed queue with leases, one automatic retry and cancel.
- Free tier limits: size, duration, one active job, bounded queue.
- 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.
- Money is safe: append-only ledger, idempotent settlement, reconciliation
at startup, automatic refunds for failed downloads.
- Gift codes, referral rewards and tips.
- Friendly, translated error messages. Raw tool output stays in the logs.
## Quick start
@@ -58,6 +65,29 @@ curl localhost:8080/healthz
| `/info <link>` | Show formats and sizes without downloading |
| `/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 and how many joined (private chat) |
| `/donate` | Send a tip (private chat) |
### 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
@@ -75,6 +105,9 @@ The settings you are most likely to touch:
| `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 |
| `LOG_LEVEL` | `info` | `debug`, `info`, `warn`, `error` |
@@ -102,6 +135,8 @@ yt-dlp.
cmd/core the bot gateway (and, for now, the embedded worker)
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/sched the job queue
internal/worker the download worker
internal/ytdlp yt-dlp and ffprobe wrapper