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
+139
View File
@@ -516,3 +516,142 @@ Read from the LNbits source at `v0.12.12` (the target) and `v1.7.1` (current).
change at runtime.
- `/privacy`, `/terms` and `/support` are short texts in the catalog.
Replace them with your own before running a public bot.
## Phase 6
### Verified Telegram Mini App facts
Checked against https://core.telegram.org/bots/webapps on 2026-11-18.
- The page must load `https://telegram.org/js/telegram-web-app.js` in its
head. It provides `window.Telegram.WebApp`.
- `initData` is a query string. It is valid when
`hex(HMAC_SHA256(key=HMAC_SHA256(key="WebAppData", msg=bot_token), msg=data_check_string))`
equals its `hash` field, where the check string is every other field as
`key=value`, sorted by key, joined with line feeds.
- `start_param` in initData carries the `startapp` value of a
`https://t.me/<bot>?startapp=<value>` link. Such links open the bot's
main Mini App, which is set in @BotFather. No Bot API method sets it.
- A launch from an inline keyboard `web_app` button has no start
parameter. Those buttons work in private chats only.
- Theme colors arrive as CSS variables (`--tg-theme-...`), safe areas as
`--tg-safe-area-inset-...` and `--tg-content-safe-area-inset-...`.
- `openLink` opens a URL in the system browser. The page does not say that
it accepts schemes other than http and https.
### Authentication
- Every API call carries `Authorization: tma <initData>`. The core
validates it on each request. There is no session, cookie or token of
our own, so a call can never be more trusted than Telegram's signature.
- initData older than 24 hours is refused, and so is one dated more than
five minutes ahead. Telegram does not refresh initData while an app
stays open, so an app left open for a day has to be opened again. A
shorter window would log people out mid-use; no window would make a
leaked string valid forever.
- The check string is tried as documented (all fields but `hash`), and
also without `signature`. Either way only the bot token can produce the
hash.
- A call without valid initData gets `401` and one sentence in the
language of the browser. A banned user gets `403`.
- The API exists only when `MINIAPP_ENABLED=true`. That setting requires
an https `PUBLIC_URL` and the core refuses to start otherwise (plain
http is accepted for localhost, for development).
### What the app can do, and where the result goes
- A web page cannot receive files. Downloads started in the app, and
resends from its history, post their status message and file in the
user's private chat with the bot. The app says so and shows progress on
the Queue tab.
- Downloads from the app are personal: they resolve entitlement like a
private chat (own pass, own credits, free), never a group's wallet.
- The front end is never trusted. It shows every format and every
product; the core applies limits and prices when the call arrives. A
price sent by the client is ignored for passes, and credits must be an
amount the billing service accepts.
- The shop sells passes and credits. "Pay for this file only" needs a
refused download behind it and stays in the chat; "skip the queue" is
the Prioritize button on the Queue tab. Donations stay in the chat.
- Buy, step 1: the targets offered are the user and the groups they
administer. The API accepts any group the bot is in, the same rule as in
chat, where anybody may pay for a group.
- Payment detection: the app asks `GET /app/api/purchase/<id>` every 2.5
seconds, and each call asks LNbits. This works without a webhook. A
paid purchase also produces the usual "Paid" message in the chat.
- The QR code is made by the core and sent as a PNG data URL, so the app
ships no QR library.
- "Open in wallet" goes through the existing `/pay/<id>` page with
`openLink`, because `openLink` is only documented for web addresses.
- Queue and history are polled (queue every 2 seconds while visible).
There is no push channel; polling is simple and the numbers are small.
- "Audio only" on the Me tab is the default format MP3. Turning it off
returns to "ask every time".
- Rate limits: link lookups share the chat's budget (`RATE_USER_PER_MINUTE`),
starts and resends get twice that, invoices 10 per minute per user.
### Groups in the app
- Telegram has no "list my groups" call. The Groups tab shows groups where
the bot is present, has seen the user at least once, and Telegram
confirms the user as an admin right now (cached for five minutes, like
in chat).
- Every group endpoint checks admin status again. A demoted admin loses
access; a group the bot left is "not found" for everybody.
- Settings are saved as a whole and validated field by field. Site lists
accept a host name or a pasted link and store the host.
- Size and duration caps for a group can only be set here. They can lower
the tier's limit, never raise it; a higher value is stored as the tier's.
- Usage by member covers the last 7 days.
### Front end
- Vite, React and TypeScript, no router and no UI library. About 77 KB of
JavaScript after gzip.
- The build output `web/miniapp/dist` is committed, so `go build` and
`go install` work without Node.js. `make miniapp` rebuilds it. A Go test
fails when it is older than its sources. The Docker build rebuilds it.
- File names are stable (`assets/app.js`) to keep diffs small. The core
sends `Cache-Control: no-cache` with an ETag of the content, so clients
revalidate and pick up a new build at once.
- A Content-Security-Policy allows scripts from the app itself and
telegram.org only, and framing by Telegram's web clients only.
- The lookup card in the app has no thumbnail. An image loaded by the
browser would reveal the user's IP address to the video site; in chat the
thumbnail is fetched by Telegram's servers instead.
- All text comes from the catalog over the API. A test fails when the app
asks for a key the catalog lacks, or when markup contains a literal
sentence.
- Right to left: the direction comes from the language (`ar`, `he`, `fa`,
`ur` and a few more are known as RTL), and the style sheet uses logical
properties. No RTL catalog ships yet, so this is tested by forcing the
direction in the browser test.
- The bot's menu button is left alone. Replacing it with the app would
hide the command list, which is translated per language.
- The built-in catalogs decide the language list on the Me tab. The app
follows the user's language setting, or the Telegram language when none
is set, and reloads its texts when it changes.
### What could and could not be run here
- The API tests run on SQLite and Postgres like the rest of the suite.
- The browser test (`make miniapp-e2e`) ran here in headless Chromium:
lookup and download, cancel, resend, buying a pass, group settings,
group top-up, preferences, a deep link, forced RTL, and the notice
outside Telegram. It is not part of `make check`, because it needs a
browser.
- The app was not opened inside a real Telegram client. The bridge script
is replaced by a stand-in in the browser test. Theme colors, the back
button and haptics follow the documentation but were not seen on a
phone.
- `npm audit` reports an advisory for `picomatch`, a dependency of Vite.
It is a build tool and is not part of what the core serves.
### Fixed on the way
- A worker used one scratch directory per job. When a download failed and
the retry started on another slot of the same worker before the first
slot had finished reporting, the first slot's cleanup deleted the
retry's file, and the job failed with an internal error. It showed up as
a rare test failure on a busy machine. Every attempt now has its own
directory, and a test holds the two slots in exactly that order.