Skip to content

feat(telegram): Mini App chat bridge — initData auth, update webhook, chat proxy - #9812

Closed
benzntech wants to merge 0 commit into
diegosouzapw:release/v3.8.50from
benzntech:feat/telegram-miniapp
Closed

feat(telegram): Mini App chat bridge — initData auth, update webhook, chat proxy#9812
benzntech wants to merge 0 commit into
diegosouzapw:release/v3.8.50from
benzntech:feat/telegram-miniapp

Conversation

@benzntech

Copy link
Copy Markdown
Contributor

Summary

Phase-1 implementation of the Telegram Mini App integration (feasibility analysis: #9810). Adds an inbound Telegram Bot API webhook + a Mini App chat surface that proxies through OmniRoute's existing handleChat pipeline.

What's included

Module Purpose
src/lib/telegram/initData.ts Dependency-free WebApp initData HMAC-SHA256 verification (Bot API spec) + auth_date freshness check
src/lib/telegram/config.ts TELEGRAM_BOT_TOKEN / model / API base / timeout env config, token-format validation
src/lib/telegram/botApi.ts Minimal fetch-based Bot API client: sendMessage, editMessageText, setWebhook
src/lib/telegram/chatProxy.ts Per-user OmniRoute API key mapping (telegram:<userId>) + prompt proxy through handleChat
src/app/api/telegram/update/route.ts Inbound endpoint for BOTH the Bot API webhook (/start + chat replies) and the Mini App direct path (initData HMAC → 401 on mismatch)
src/app/miniapp/page.tsx Telegram WebApp SDK chat UI

Security model

  • Mini App path: initData HMAC verified server-side against TELEGRAM_BOT_TOKEN; tampered payloads → 401. User id extracted from verified initData for key mapping.
  • Webhook path: Telegram only delivers to the registered HTTPS webhook; route gates on token presence (503 when unset).
  • Per-user keys: each Telegram user gets a scoped OmniRoute API key (telegram:<userId>) so existing policy/rate-limit/model-allowlist code applies unchanged.
  • Route added to public prefixes with own-auth only (same pattern as /api/skills/collect/chaos).

Verification

  • telegram-init-data.test.ts — 7/7 pass (valid sig, tamper, wrong token, missing hash, stale auth_date, encoded keys)
  • telegram-botapi.test.ts — 5/5 pass (update parsing, initData end-to-end)
  • tsc --noEmit — 0 errors in new files
  • check-route-validation — PASS (607 routes, all request.json() validated)
  • check-env-doc-sync — ✓ in sync (TELEGRAM_* vars documented)

Known limitations (Phase 2)

  • No streaming to Telegram yet (non-streaming replies; progressive edits planned)
  • Public HTTPS webhook required — self-hosted installs need a tunnel
  • Non-English Mini App UI not yet translated
  • TELEGRAM_BOT_USERNAME deep-link placeholder (uses YOUR_BOT until set)

@benzntech
benzntech requested a review from diegosouzapw as a code owner August 8, 2026 17:42
@diegosouzapw

Copy link
Copy Markdown
Owner

Thanks for the PR. Please address the mandatory items (tests and/or merge blockers) in this branch, then rerun checks before /merge-prs.

@diegosouzapw
diegosouzapw changed the base branch from main to release/v3.8.50 August 9, 2026 01:45
@diegosouzapw

Copy link
Copy Markdown
Owner

Thanks for the PR. Please address the mandatory items (tests and/or merge blockers) in this branch, then rerun checks before /merge-prs.

1 similar comment
@diegosouzapw

Copy link
Copy Markdown
Owner

Thanks for the PR. Please address the mandatory items (tests and/or merge blockers) in this branch, then rerun checks before /merge-prs.

@diegosouzapw

Copy link
Copy Markdown
Owner

Obrigado pelo PR. Mantive a revisão de fix-in-place e não foi possível concluir o ajuste completo aqui:

  • Para os PRs em fork: não consigo aplicar push de correção diretamente na sua branch.
    Por favor, faça um rebase/sync com release/v3.8.50, resolva conflitos se houver, e rode os checks dessa branch.
    Se preferir, posso aplicar a correção na próxima rodada assim que você mandar o branch atualizado ou confirmar que o PR está limpo pra esse merge.

diegosouzapw added a commit that referenced this pull request Aug 9, 2026
* fix(deps): bump transitive deps for 6 Dependabot + remaining audit vulns on main

Same overrides as #9464 (ip-address, hono, fast-uri, socket.io-parser, undici)
applied directly to main. Also covers brace-expansion (scoped), js-yaml v4 copies,
and mermaid.

npm audit: 6→0 vulnerabilities.
Closes Dependabot #161-#166.

* fix(deps): bump nanoid, dompurify for 2 new Dependabot alerts (#189, #190)

Bumps: nanoid ^3.3.17 (was transitive, now overridden), dompurify ^3.4.13
(with monaco-editor scoped override). Closes Dependabot #189, #190.

Remaining #182-#188 (js-yaml + mermaid) already closed by #9651 merge —
awaiting Dependabot re-scan.

npm audit → 0 vulnerabilities.

* fix(repo): harden .gitignore to also ignore a _tasks symlink (/_tasks)

_tasks is a SEPARATE nested git repo (gitignored). The pattern _tasks/ (trailing
slash) ignores only a directory, not a SYMLINK named _tasks. A self-referential
_tasks symlink can slip in via git add -A and, once pulled, checkout materializes
it over the real _tasks repo (destroying plans/specs/hands-off). Anchored /_tasks
ignores the symlink too, preventing re-capture.

* feat(telegram): Mini App chat bridge — initData auth, update webhook, chat proxy

Implements the Phase-1 slice of the Telegram Mini App integration
(docs/proposals/TELEGRAM-MINIAPP.md):

- src/lib/telegram/initData.ts — dependency-free WebApp initData HMAC-SHA256
  verification (Telegram Bot API spec), with auth_date freshness check.
- src/lib/telegram/config.ts — TELEGRAM_BOT_TOKEN / model / API base / timeout
  env config; token format validation; enabled gate.
- src/lib/telegram/botApi.ts — minimal fetch-based Bot API client
  (sendMessage, editMessageText, setWebhook) + update shape helpers.
- src/lib/telegram/chatProxy.ts — maps a Telegram user to a per-user
  OmniRoute API key (createApiKey, name telegram:<userId>) and proxies
  prompts through the existing handleChat pipeline.
- src/app/api/telegram/update/route.ts — inbound endpoint serving both the
  Bot API update webhook (/start + chat replies) and the Mini App direct
  path (initData HMAC verified → 401 on mismatch). Public route prefix;
  own auth only.
- src/app/miniapp/page.tsx — Telegram WebApp SDK chat UI.
- Tests: telegram-init-data (7), telegram-botapi (5) — 12/12 pass.
- Env docs: TELEGRAM_* vars in .env.example + ENVIRONMENT.md (sync ✓).
- Route-validation check: PASS (body validated via Zod).

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
Co-authored-by: benzntech <bensonkbmca@gmail.com>
@diegosouzapw
diegosouzapw force-pushed the feat/telegram-miniapp branch from 4f9600b to 5fd2325 Compare August 11, 2026 01:06
@mergify

mergify Bot commented Aug 11, 2026

Copy link
Copy Markdown

⚠️ The sha of the head commit of this PR conflicts with #8875. Mergify cannot evaluate rules on this PR. Once #8875 is merged or closed, Mergify will resume processing this PR. ⚠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants