Phase 6: Mini App

A Telegram Mini App served by the core under /app/, embedded in the binary.

- API under /app/api/ with initData validated (HMAC, age) on every call
- Pages: Home, Queue, Buy (target, product, invoice), History, Groups,
  group detail (Overview, Settings, Wallet, Usage) and Me
- Every action runs through the same service, billing and scheduler code
  as the chat, so limits, prices and admin checks are decided on the server
- Texts come from the catalog over the API; layout mirrors for RTL
- Front end in web/miniapp (Vite, React, TypeScript); the build is
  committed and guarded by a source hash test; the Docker image rebuilds it
- MINIAPP_ENABLED with an https PUBLIC_URL; entry points from /app, group
  settings and the over-limit offer
- Browser test (make miniapp-e2e) against a core in test mode
- Fix: each download attempt gets its own scratch directory, so a retry
  cannot lose its file to the cleanup of the failed attempt

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 10:27:20 +00:00
co-authored by Claude Fable 5.2
parent 51371da0e0
commit 4f11f939a0
55 changed files with 7069 additions and 6 deletions
+58 -1
View File
@@ -49,7 +49,8 @@ phase; sections for later phases say so.
| `internal/db` | Schema migrations and queries for SQLite and Postgres. |
| `internal/i18n` | go-i18n wrapper, locale aware formatting. |
| `internal/tg` | Narrow Bot API interface, error helpers, the fake Bot API server. |
| `internal/api` | HTTP server: health check, Telegram webhook. |
| `internal/api` | HTTP server: health check, payment webhook, worker protocol, Mini App API. |
| `web/miniapp` | The Mini App front end (Vite, React, TypeScript) and its build, embedded with `go:embed`. |
| `locales` | Translation catalogs, embedded in the binary. |
Dependencies point one way: `bot` uses `service`, `service` uses `sched`,
@@ -351,6 +352,58 @@ Admin-only actions are checked in one place each: `requireAdmin` for
commands and `cbSetting` for buttons, both through `groups.Admins`, which
asks Telegram and caches the answer.
## The Mini App
The Mini App is a second front end to the same rules. It is a single page
app, built once and embedded in the core binary, served under `/app/`, with
a JSON API under `/app/api/`.
```
browser (inside Telegram) core
index.html, app.js, index.css <---- GET /app/... embedded files
Authorization: tma <initData> ----> /app/api/... api.MiniApp
validate initData (HMAC, age)
load user, refuse banned
call bot.App*, service, billing
```
**Authentication.** Telegram gives the app an `initData` string signed with
a key derived from the bot token. The app sends it with every call and
`api.ValidateInitData` checks it every time. There are no sessions and no
cookies, so there is nothing to steal besides a string that expires. The
user ID comes from the validated string and from nowhere else.
**Nothing is decided in the browser.** The app shows prices, limits and
buttons, but every operation goes through the code the chat uses:
| App action | Runs |
| --- | --- |
| Look a link up | `bot.AppProbe`: `service.Gate`, rate limit, `service.Probe` |
| Start a download, resend | `bot.AppDownload`, `bot.AppResend`: `service.Preview`, then `service.Start` |
| Cancel | `sched.CancelDelivery`, owner checked |
| Prioritize | credits or a `skip_queue` invoice, like the chat button |
| Buy | `billing.CreatePurchase`; the price comes from the core's price list |
| Payment detection | `billing.Check`, which asks LNbits |
| Group pages | `groups.Admins.IsAdmin` on every call, then the same settings rows |
So a cache hit, a resend or a tampered request meets the same limits and
the same ledger as a message in the chat. Files and status messages always
arrive in the user's private chat with the bot; a web page cannot receive
them.
**Texts.** The app has no text of its own. `GET /app/api/i18n` returns the
`app` table of the catalog plus the tables it shares with the chat
(product names, units, button and setting labels), in the user's language
with the usual fallback, placeholders rewritten from `{{.Name}}` to
`{Name}` and plurals as CLDR forms. The response also carries the text
direction. The style sheet uses logical properties only, so `dir="rtl"`
mirrors the layout.
**Build.** `web/miniapp/dist` is committed, so `go build` needs no Node.js.
The build writes a hash of its sources next to its output and a Go test
recomputes it, which catches a source change committed without a rebuild.
The Docker image rebuilds the front end from source.
## Internationalization
Handlers never contain user-facing text. They call `i18n.Bundle` with a
@@ -376,6 +429,10 @@ all output. URLs are redacted before they are logged.
`tg/fakeserver`, an HTTP server that speaks enough of the Bot API to push
updates in and record every call, including multipart uploads.
- One opt-in test runs the real yt-dlp against a public test file.
- The Mini App API is tested over real HTTP with initData signed like
Telegram signs it. `make miniapp-e2e` additionally opens the built app in
headless Chromium against a core in test mode and clicks through every
flow; it is opt-in because it needs a browser.
- Payments are tested against `billing/lnbitstest`, a fake LNbits that
speaks the real request and response shapes and lets a test pay invoices,
replay or drop webhooks and take the server down.