初始化 正如 @telegram-apps/bridge 文档 中所述,必须手动定义 事件处理程序,才能开始监听 Telegram 迷你应用程序事件。 此外,在我们的代码中,我们希望确保在调用某个方法时,该方法要么有效,要么我们 会收到该方法无效的通知。 为此,有必要了解当前使用的 Telegram Mini Apps 版本。 为满足这些要求,软件包提供了 init 函数,该函数接受具有以下结构的对象 : - interface Options {* d$ }, H- T0 ^7 U; U7 ]4 y
- /**
+ R" }# o( [& B1 T - * True if the SDK should accept styles sent from the Telegram
& _9 Y5 k' ?! e0 p8 r - * application.: ?8 ?" U5 K7 I% x8 u5 ^
- * @default true C; h8 x! M. r6 ]7 y0 \/ ]$ |: n
- */
2 n. o8 B9 ?6 t4 R" P& N - acceptCustomStyles?: boolean;
: K0 q( z7 k5 G2 C4 j) f9 ^ - /**
/ R: c7 t) a) m4 g - * The maximum supported Mini Apps version.; b" d" p7 Z, r1 M w
- * @default Extracted using the `retrieveLaunchParams`
{7 J) U" h) |0 G7 u7 b4 k& J - * function.
/ y" W' W+ j9 a% ]. @* a" a4 \8 C. h - * @see retrieveLaunchParams
4 M/ f8 k! w6 f6 k - */+ X$ w: v: _/ ?, g; }& k
- version?: Version;0 p2 }; d S; O/ t
- /**
$ M5 v5 T p0 C' n# G' Q e - * Custom postEvent function.
9 b0 R. V+ ] f/ b7 L - *
4 R' }# M/ x3 w5 q1 @ - * Passing the "strict" value creates a function that always
- O9 a4 s9 d$ G5 U+ U# R - * checks if the specified call is supported by the current Mini
3 ? S; e8 x. O8 { - * Apps version. If the method is unsupported, an error
' g7 x k W& d! ~; z& ` - * will be thrown.
5 A- z; x* H; X) S. [: z - *9 Q7 R' v+ e+ {- K8 N; ?2 A& J2 ~+ z
- * Passing the "non-strict" value creates a postEvent function5 h1 ^( q$ \" C% `( a% l
- * that doesn't throw errors but warns about missing method3 G Q+ ?# z8 W$ o/ C
- * support.( o: V5 ~& |) J9 j- Q( F* i4 ?
- *
; G% I% L! ^+ ]* G' ~ - * @default 'strict'
: D% a- l4 D0 ]. e - * @see createPostEvent* ?' M- g$ C; |: K" w. {8 E
- */
* V$ V) u+ N2 ^) f+ t$ o; E - postEvent?: PostEventFn | 'strict' | 'non-strict';
1 e4 ^" D9 z4 Q# i8 T - }
复制代码在大多数情况下,开发人员不需要使用这些选项。 调用该函数后,将配置软件包的全局依赖关系,并创建 Telegram Mini Apps 事件处理程序。 - import { init } from '@telegram-apps/sdk';
; Q' C* O6 L: i% J - & R5 p# J0 A: i( a/ Y
- init();
复制代码 ; e; e$ d( Q: K! t! b
Q- N6 p2 O4 P% A0 m) W |