---
title: "Telegram Mini App kit"
description: "L1 kit for a clone Mini App under /tg-mini-app, public bot commands, and buyer order status messages in Telegram."
locale: "en"
---
# Telegram Mini App kit

> **Info**
> The kit is brand-free L1 code. Bot username, Mini App short name, tabs and copy overrides come from the clone's ring-config and locale files. Everything is off until a clone turns it on.

### For founders

## What buyers get

- A Mini App at `t.me//` that opens the clone store, cart, checkout and orders inside Telegram, with the Telegram Back and Main buttons.
- Bot commands in a private chat: `/start`, `/orders`, `/stop_orders`, `/start_orders`, `/help`.
- A message from the bot when their order is paid, processing, shipped, completed or canceled, with a button that opens the order in the Mini App. Shipped messages include the tracking number when there is one.

## Turning it on

1. Set `TELEGRAM_MINI_APP_BOT_TOKEN` (or reuse `ADMIN_BOT_TOKEN`) as a runtime secret.
2. In the clone ring-config set `telegramMiniApp.enabled`, `botUsername`, `appShortName` and `tabs`.
3. For bot commands set `telegramMiniApp.publicBot.enabled: true`.
4. For order messages set `features.notifications.telegram.orders.enabled: true`.
5. Ops: the bot webhook must list `message`, `callback_query`, `pre_checkout_query` and `my_chat_member` in `allowed_updates`.

### For developers

## Layout

| Path | Role |
|------|------|
| `features/telegram-mini-app/lib` | WebApp access, start param grammar, paths, config resolver, bot copy helpers |
| `features/telegram-mini-app/hooks` | `useTelegramBackButton`, `useTelegramMainButton` |
| `features/telegram-mini-app/components` | Shell and tab nav |
| `features/telegram-mini-app/services/public-bot.ts` | Public commands on the admin webhook, before the PALADIN whitelist |
| `features/telegram-mini-app/services/order-message-preferences.ts` | Opt-out and reachability in `notification_preferences.telegramOrderMessages` |
| `features/telegram-mini-app/services/bot-membership.ts` | `my_chat_member`: block sets `reachable: false`, unblock sets it back |
| `features/store/lib/store-surface.ts` | Store links per surface (`/store` on web, `/tg-mini-app/store` in the Mini App) |
| `features/store/lib/order-status-events.ts` | `emitOrderStatusChanged(orderId, from, to)` |
| `features/notifications/channels/telegram` | Bot API client, send queue, order message config, template, notifier |
| `locales//telegram-mini-app.json` | Namespace `telegram-mini-app` (5 locales) |

## Start param grammar

`product_`, `vendor_`, `cat_`, `groupbuy[_]`, `orders`, `order_`, `cart`, `vendorpanel`, `ref_`. Ids outside `A-Za-z0-9_-` are base64url encoded with a `b` prefix. The payload is routing only, never auth.

## Order messages

```json
{
  "features": {
    "notifications": {
      "telegram": {
        "orders": {
          "enabled": false,
          "statuses": ["paid", "processing", "shipped", "completed", "canceled"]
        }
      }
    }
  }
}
```

Flow:

1. `StoreOrdersService.adminUpdateOrderStatus` is the single write path for `orders.status`. It reads the prior status, writes, then calls `emitOrderStatusChanged`. Listeners run after the current task and never block or fail the write.
2. The notifier checks the flag and status list, a bot token, the buyer's linked numeric id (`users.communication.telegramId`, else the id saved when they used the bot), opt-out and reachability.
3. It claims `(order, status)` in `orders.telegramNotifications` inside a transaction, so webhook retries, double emits and multiple pods send at most one message. Later states: `sent`, `unreachable`, `failed`.
4. It sends through an in-process queue: 1 message per second per chat, 30 per second per pod, retries 429 (honours `retry_after`), 5xx and network errors with exponential backoff, up to 4 attempts.
5. A 403 marks the buyer unreachable and stops. `/start_orders` or unblocking the bot (`my_chat_member`) makes them reachable again.

No migration: the log is a JSON key on the order doc and preferences reuse `notification_preferences`.

Copy lives under `telegram-mini-app.orderMessages` and `telegram-mini-app.orderStatus`. Clones override single keys in their own locale files, never a full L1 locale file.

## Vendor messages (v1.1, design only)

A "new paid order" message to the vendor's linked Telegram id, subscribed to the same `order.status.changed` event with `to: paid`. Recipients come from `vendorSettlements[].vendorId` to the vendor entity owner. Inline `callback_query` buttons (mark processing, mark shipped) call the same status write path after checking vendor ownership, which needs `callback_query` in `allowed_updates`. Vendors cannot change order status anywhere in L1 today, so these Telegram buttons will be the first way a vendor changes an order status. Not built.

## Tests

`__tests__/features/telegram-mini-app`, `__tests__/features/notifications/telegram`, `__tests__/features/store/store-surface.test.ts`, `__tests__/api/telegram/admin-bot-webhook-drop.test.ts`, `__tests__/lib/i18n/message-scopes.test.ts`.
