初始化 正如 @telegram-apps/bridge 文档 中所述,必须手动定义 事件处理程序,才能开始监听 Telegram 迷你应用程序事件。 此外,在我们的代码中,我们希望确保在调用某个方法时,该方法要么有效,要么我们 会收到该方法无效的通知。 为此,有必要了解当前使用的 Telegram Mini Apps 版本。 为满足这些要求,软件包提供了 init 函数,该函数接受具有以下结构的对象 : - interface Options {
/ L) f/ q9 W, z) C' ^7 B - /**
6 q4 D7 X3 t3 t9 S& i& v- p - * True if the SDK should accept styles sent from the Telegram
( g; J1 \- H' @1 B, c l1 p - * application.5 A5 C% N& W4 {( P6 M- x" z1 [ Q
- * @default true1 H% ^+ E9 X8 C# |( q; ^& q
- */
1 ^+ l6 d, Q( s2 v1 q, B - acceptCustomStyles?: boolean;
3 Y2 Z1 {* P6 d5 z7 k/ ` - /**
2 p; \6 s" t( I6 T! L; C# t& s - * The maximum supported Mini Apps version.
* Q0 F# V3 F1 N- @& [, u6 ? - * @default Extracted using the `retrieveLaunchParams`
( b* K5 h+ \0 i; t& h0 x - * function.
' `( y* k Y+ A9 @8 _% I - * @see retrieveLaunchParams/ @) I4 n2 [9 L9 N$ d: E$ ^
- */
0 w }- M7 n7 P% i8 [0 g - version?: Version;7 X, [6 h( e) s+ G3 u, r, F3 J( K
- /**3 r- H/ h* z( ]* \. _7 Y, q
- * Custom postEvent function.
7 ^! M: D C5 h; V - *
! _0 N5 n+ y( ]" h" R7 p - * Passing the "strict" value creates a function that always
9 P3 p6 C3 P* s1 A6 B - * checks if the specified call is supported by the current Mini6 r- _( u/ `. U) }, q
- * Apps version. If the method is unsupported, an error) a6 l+ C- A0 V" N
- * will be thrown.
8 K* D$ o/ c/ ]) b6 O7 H - *1 V" o" f" _* B0 j# x# G* `2 C# ^
- * Passing the "non-strict" value creates a postEvent function
/ x8 Z1 V! A, h/ c - * that doesn't throw errors but warns about missing method
; I% l: E, [8 K# x5 b - * support.
, L7 i0 B9 p: I! N: }+ x$ d - *8 I5 ~# e! L8 W/ u" f/ U
- * @default 'strict'. b* h$ j8 G" ~, H
- * @see createPostEvent
3 N" e: r+ Z" Y - */! j9 D7 z" x$ a ?* B5 G. G
- postEvent?: PostEventFn | 'strict' | 'non-strict';/ P) U3 h' G, M, |; a" H( T
- }
复制代码在大多数情况下,开发人员不需要使用这些选项。 调用该函数后,将配置软件包的全局依赖关系,并创建 Telegram Mini Apps 事件处理程序。 - import { init } from '@telegram-apps/sdk';
& U( x4 A7 G9 E# W - " [% G# Y' `( ^/ a5 _) v
- init();
复制代码 % _, A. h- n; l
9 Z7 U" S" {* E: Z2 V9 v/ L" i |