telegram-mini-app
npx skills add https://github.com/zhondori/skills --skill telegram-mini-app
Agent 安装分布
Skill 文档
Telegram Mini Apps
Web applications displayed inside Telegram’s WebView. Built with standard HTML/CSS/JS. Require a Telegram Bot as host.
Quick Start
1. Setup
<script src="https://telegram.org/js/telegram-web-app.js"></script>
Or with @tma.js/sdk:
npm i @tma.js/sdk
2. Initialize
const tg = window.Telegram.WebApp
tg.ready()
tg.expand()
3. Create App via BotFather
/newbotâ create bot/newappâ create Mini App linked to bot- Set URL via
/myapps> Edit link, or/setmenubutton
Result: https://t.me/{bot}/{app}?startapp={param}
Launch Methods
| Method | Entry Point | Data Flow |
|---|---|---|
| Keyboard Button | web_app type button in reply markup |
sendData() sends to bot, closes app |
| Inline Button | web_app type in inline keyboard |
answerWebAppQuery() via query_id |
| Menu Button | Bot menu (via /setmenubutton) |
Full WebApp API |
| Direct Link | t.me/bot/app?startapp=param |
start_param in init data |
| Main Mini App | Profile button (via BotFather) | Full WebApp API |
| Inline Mode | InlineQueryResultsButton |
switchInlineQuery() |
| Attachment Menu | Chat input attachment | Access to chat and receiver |
Essential Patterns
Theming â Match Telegram’s Look
body {
background: var(--tg-theme-bg_color, #fff);
color: var(--tg-theme-text_color, #000);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
Always provide CSS fallbacks â app may load outside Telegram context during dev.
Listen for theme changes:
tg.onEvent('themeChanged', () => {
document.body.className = tg.colorScheme // "light" | "dark"
})
Safe Areas â Prevent Content Clipping
.app {
padding-top: calc(var(--tg-content-safe-area-inset-top, 0px) + var(--tg-safe-area-inset-top, 0px));
padding-bottom: var(--tg-safe-area-inset-bottom, 0px);
padding-left: var(--tg-safe-area-inset-left, 0px);
padding-right: var(--tg-safe-area-inset-right, 0px);
}
Viewport â Handle Mobile Bottom Sheet
tg.expand() // maximize height
tg.onEvent('viewportChanged', ({ isStateStable }) => {
if (isStateStable) {
// Safe to adjust layout â user stopped dragging
}
})
// Prevent accidental close via swipe on scrollable content
tg.disableVerticalSwipes()
Main Button â Primary CTA
const btn = tg.MainButton
btn.setText('Submit')
btn.show()
btn.onClick(() => {
btn.showProgress(true)
submit().finally(() => btn.hideProgress())
})
Back Button â In-App Navigation
tg.BackButton.show()
tg.BackButton.onClick(() => router.back())
// Hide when on root page
tg.BackButton.hide()
User Identity
const user = tg.initDataUnsafe.user
// { id, first_name, last_name?, username?, language_code?, is_premium?, photo_url? }
// IMPORTANT: always validate initData server-side before trusting user identity
// Send raw init data to your server:
fetch('/api', { headers: { Authorization: `tma ${tg.initData}` } })
Haptic Feedback
tg.HapticFeedback.impactOccurred('medium') // button tap
tg.HapticFeedback.notificationOccurred('success') // action complete
tg.HapticFeedback.selectionChanged() // picker change
Popups
tg.showPopup({
title: 'Confirm',
message: 'Delete this item?',
buttons: [
{ id: 'del', type: 'destructive', text: 'Delete' },
{ id: 'no', type: 'cancel' },
]
}, (id) => { if (id === 'del') doDelete() })
Closing Behavior
tg.enableClosingConfirmation() // ask before close (unsaved changes)
tg.disableClosingConfirmation()
References
Detailed API docs organized by topic. Read as needed.
-
api-methods.md â All 60+ WebApp methods with parameters, version requirements, and code examples. Covers: lifecycle, viewport, navigation, buttons, popups, theming, storage, permissions, sharing, sensors, biometrics, QR, clipboard, fullscreen, orientation, payments.
-
events.md â All events sent from Telegram to the Mini App. Covers: lifecycle, button presses, viewport/layout, theme, popups, permissions, sharing, payments, QR, biometrics, sensors, location, storage, home screen, fullscreen.
-
init-data.md â Init data structure, WebAppUser/WebAppChat objects, HMAC-SHA256 and Ed25519 validation algorithms with code examples, server-side auth patterns.
-
debugging.md â Platform-specific debug setup (Desktop, macOS, Android, iOS), Eruda integration, test environment access, common issues and fixes.
Platform Considerations
- 6 Telegram clients with varying feature support â check
tg.versionbefore using newer APIs - Mobile opens as bottom sheet â call
expand()early, handle unstable viewport - Desktop/Web opens maximized â
expand()is a no-op - Test environment allows HTTP and direct IPs â use for dev, create separate bot there
- HTTPS required in production
- Scaffold new projects with:
npx @tma.js/create-mini-app@latest
Version Compatibility
Use tg.version to check API availability. Key version milestones:
| Version | Notable Features |
|---|---|
| v6.1 | Back button, haptic feedback, invoices, header/bg colors |
| v6.2 | Popups |
| v6.4 | QR scanner, clipboard |
| v6.7 | Inline query switching |
| v6.9 | CloudStorage, write access, phone request, custom methods |
| v6.10 | Settings button |
| v7.2 | BiometricManager |
| v7.7 | Swipe behavior control |
| v7.8 | Share to Story |
| v7.10 | Secondary button, bottom bar color |
| v8.0 | Fullscreen, orientation lock, sensors, location, home screen, emoji status, file download, prepared messages |
| v9.0 | DeviceStorage, SecureStorage |
| v9.1 | Hide keyboard |