-
Notifications
You must be signed in to change notification settings - Fork 57
feat(tma): enhance tma overview page, add page describing TMA <> telegram client communication #1977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
coalus
wants to merge
4
commits into
ton-org:main
Choose a base branch
from
coalus:feat/tma-platform-fundamentals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(tma): enhance tma overview page, add page describing TMA <> telegram client communication #1977
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| --- | ||
| title: "Apps communication" | ||
| --- | ||
|
|
||
| import { Aside } from '/snippets/aside.jsx'; | ||
|
|
||
| The Telegram client and a Mini App communicate through a message-passing interface. The exact transport depends on the platform, but the protocol is the same: the Mini App sends **methods** to the Telegram client, and the Telegram client sends **events** back to the Mini App. | ||
|
|
||
| ## Methods and events | ||
|
|
||
| Two terms describe the communication directions: | ||
|
|
||
| - **Methods** — calls sent from the Mini App to the Telegram client. A method triggers an action in the native app (for example, showing a popup or expanding the viewport). | ||
| - **Events** — signals sent from the Telegram client to the Mini App. An event notifies the Mini App that something happened (for example, the viewport changed or a button was pressed). | ||
|
|
||
| Internally both are messages, but "methods" and "events" distinguish the direction throughout the Mini Apps platform. | ||
|
|
||
| ## Transport by platform | ||
|
|
||
| The transport mechanism varies by Telegram client: | ||
|
|
||
| | Platform | Transport | Details | | ||
| | -------------------------------------------------- | --------------------------------------- | ------------------------------------------------------------------------------------ | | ||
| | Web (Telegram Web A, Web K) | `window.parent.postMessage` | The Mini App runs in an `<iframe>`. Messages are JSON strings passed between frames. | | ||
| | Desktop and mobile (Android, iOS, macOS, tdesktop) | `window.TelegramWebviewProxy.postEvent` | Telegram injects a global function into the WebView. | | ||
| | Windows Phone | `window.external.notify` | Same JSON format as the web version. | | ||
|
|
||
| ## Calling methods | ||
|
|
||
| ### Web | ||
|
|
||
| The web version displays the Mini App in an `<iframe>`. Communication uses `window.parent.postMessage` with a JSON string: | ||
|
|
||
| ```typescript | ||
| const data = JSON.stringify({ | ||
| eventType: 'web_app_setup_back_button', | ||
| eventData: { is_visible: true }, | ||
| }); | ||
|
|
||
| window.parent.postMessage(data, 'https://web.telegram.org'); | ||
| ``` | ||
|
|
||
| Pass a [target origin](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#targetorigin) to restrict which parent frame can receive the message. Use `https://web.telegram.org` as the default. | ||
|
|
||
| ### Desktop and mobile | ||
|
|
||
| Desktop and mobile clients inject `window.TelegramWebviewProxy.postEvent` into the WebView: | ||
|
|
||
| ```typescript | ||
| window.TelegramWebviewProxy.postEvent( | ||
| 'web_app_setup_back_button', | ||
| JSON.stringify({ is_visible: true }) | ||
| ); | ||
| ``` | ||
|
|
||
| ### Windows Phone | ||
|
|
||
| The Windows Phone client provides `window.external.notify` with the same JSON format as the web version: | ||
|
|
||
| ```typescript | ||
| window.external.notify(JSON.stringify({ | ||
| eventType: 'web_app_setup_back_button', | ||
| eventData: { is_visible: true }, | ||
| })); | ||
| ``` | ||
|
|
||
| ## Listening to events | ||
|
|
||
| ### Web | ||
|
|
||
| The parent iframe sends events via `window.postMessage`. Add a `message` listener: | ||
|
|
||
| ```typescript | ||
| window.addEventListener('message', ({ data }) => { | ||
| const { eventType, eventData } = JSON.parse(data); | ||
| console.log(eventType, eventData); | ||
| }); | ||
| ``` | ||
|
|
||
| <Aside | ||
| type="caution" | ||
| > | ||
| In production, validate that the `message` event originates from the Telegram application and that `data` is a string before parsing. | ||
| </Aside> | ||
|
|
||
| ### Desktop, mobile, and Windows Phone | ||
|
|
||
| These platforms deliver events by calling a globally defined function: | ||
|
|
||
| | Platform | Function path | | ||
| | ---------------------------- | --------------------------------------- | | ||
| | Telegram Desktop | `window.TelegramGameProxy.receiveEvent` | | ||
| | Telegram for iOS and Android | `window.Telegram.WebView.receiveEvent` | | ||
| | Windows Phone | `window.TelegramGameProxy_receiveEvent` | | ||
|
|
||
| All functions share the same signature: | ||
|
|
||
| ```typescript | ||
| type ReceiveEvent = (eventType: string, eventData: unknown) => void; | ||
| ``` | ||
|
|
||
| To handle events on all platforms, assign a handler function to all three paths. | ||
|
|
||
| ## Using the SDK | ||
|
|
||
| The [@tma.js/sdk](https://www.npmjs.com/package/@tma.js/sdk) package abstracts away platform differences and provides a unified API for calling methods and listening to events: | ||
|
|
||
| ```typescript | ||
| import { postEvent, on } from '@tma.js/sdk'; | ||
|
|
||
| // Call a method | ||
| postEvent('web_app_set_header_color', { color_key: 'bg_color' }); | ||
|
|
||
| // Listen to an event | ||
| const removeListener = on('viewport_changed', payload => { | ||
| console.log('Viewport changed:', payload); | ||
| }); | ||
| ``` | ||
|
|
||
| For the full list of available methods and events, see the [@tma.js/sdk documentation](https://docs.telegram-mini-apps.com/packages/tma-js-sdk). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -722,8 +722,10 @@ TES | |
| Testgiver | ||
| testnet | ||
| Testnet | ||
| tdesktop | ||
| Tezos | ||
| timepoint | ||
| tma | ||
| TMA | ||
| TMAs | ||
| tock | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.