初始化 正如 @telegram-apps/bridge 文档 中所述,必须手动定义 事件处理程序,才能开始监听 Telegram 迷你应用程序事件。 此外,在我们的代码中,我们希望确保在调用某个方法时,该方法要么有效,要么我们 会收到该方法无效的通知。 为此,有必要了解当前使用的 Telegram Mini Apps 版本。 为满足这些要求,软件包提供了 init 函数,该函数接受具有以下结构的对象 : - interface Options {3 U7 D9 G' B B
- /**- _$ F( Q# r8 @( B- ^
- * True if the SDK should accept styles sent from the Telegram
/ I8 n9 |; {$ _( W - * application.2 o( H8 t& ^0 d" W
- * @default true
) m7 B# a; g9 a - */
8 u0 p' M) }& a' \ - acceptCustomStyles?: boolean;
' h5 \7 r0 L" I N; a - /**
0 K2 B3 h- K, U - * The maximum supported Mini Apps version.0 Y2 Z5 b9 K* I( {" y
- * @default Extracted using the `retrieveLaunchParams`( t) ^$ k, E) x0 `" I! R: Z
- * function.8 N9 u9 V: o% O' o
- * @see retrieveLaunchParams
8 j! U# m: h/ K" O! r O - */
" Z1 W$ u5 x0 L/ s2 d7 V: v - version?: Version;
. i2 m' ~4 y6 y* M, C# u% \ - /**
^. r; d. ~5 B - * Custom postEvent function.. H% W. V9 M& x( _$ R/ [, ]
- *
2 R% A7 [& y! K/ E- e: Z- p - * Passing the "strict" value creates a function that always
" O3 x1 `4 }/ Z - * checks if the specified call is supported by the current Mini
2 Q1 E* m, b9 a+ e" ]1 O9 b - * Apps version. If the method is unsupported, an error
7 \* V8 }0 T% V% @+ h - * will be thrown.' t/ d/ e* {9 r9 r w4 v( N
- *9 M9 [8 x) r9 V4 o5 [; z
- * Passing the "non-strict" value creates a postEvent function9 a/ @, ^* N; {) S6 W E
- * that doesn't throw errors but warns about missing method
; z, Z* }5 V2 E$ q' c9 L+ Y - * support.& c% E* X' N& I$ b
- *
# M& e5 X6 ~9 W2 W- D# W - * @default 'strict'
& _7 k. ?+ O% z! L1 H. S% p2 N - * @see createPostEvent* k$ P* ?6 b# O, t# l! B) T& k9 M
- */9 L* O3 E1 @% p: \8 p. {: D1 V3 K
- postEvent?: PostEventFn | 'strict' | 'non-strict';
9 J' F7 N8 K! b$ z( h5 o& }0 Z) Q - }
复制代码在大多数情况下,开发人员不需要使用这些选项。 调用该函数后,将配置软件包的全局依赖关系,并创建 Telegram Mini Apps 事件处理程序。 - import { init } from '@telegram-apps/sdk';
7 s ]2 H/ N- S! C! k5 Z" ^ - % x$ Y V; U6 b; ?9 G
- init();
复制代码
/ u% N p) W: i1 _
9 _1 ^/ S; s* g& h3 J/ b/ T8 h |