初始化 正如 @telegram-apps/bridge 文档 中所述,必须手动定义 事件处理程序,才能开始监听 Telegram 迷你应用程序事件。 此外,在我们的代码中,我们希望确保在调用某个方法时,该方法要么有效,要么我们 会收到该方法无效的通知。 为此,有必要了解当前使用的 Telegram Mini Apps 版本。 为满足这些要求,软件包提供了 init 函数,该函数接受具有以下结构的对象 : - interface Options {8 }9 m0 g8 |/ a" I, [5 n2 a
- /**; d( t+ r! |! z) Y! Y" p, i% V8 Q
- * True if the SDK should accept styles sent from the Telegram5 f' s1 K! H6 Y: [0 }; |
- * application.
# h3 w7 e! \, j+ h. x8 t - * @default true; B$ d9 Z. N4 M) A
- */
4 B+ p3 {3 R- l8 f+ q. ^ - acceptCustomStyles?: boolean;* D% p+ V* A2 o @" w
- /**% _+ A) K. ~' G* Y! c6 w+ c9 n1 s: d
- * The maximum supported Mini Apps version.* F- Z! M; z% p! l
- * @default Extracted using the `retrieveLaunchParams`$ t4 V# d+ y" A4 p3 C- ?# k9 t% U
- * function.
" B( c! V$ W: d1 P, q- @ - * @see retrieveLaunchParams
6 E* T; |8 w9 S; M- A& a! ^5 r( b& ]: m$ ] - */; h5 ^" p+ J! O8 i8 i
- version?: Version;
0 r. d# Z- @6 j - /**
2 h5 D8 T2 E& |2 g( M% Y0 M - * Custom postEvent function.
7 v9 Z8 M, v/ v, e' V$ [ - *
. k% h8 [# {" K/ B& k- [) k - * Passing the "strict" value creates a function that always! u( g/ Q0 m8 U) j2 Y: A: c' H
- * checks if the specified call is supported by the current Mini4 S9 i+ ?# R, A8 s, `4 E9 s
- * Apps version. If the method is unsupported, an error$ k1 Q; N4 U& v0 U* M) I
- * will be thrown.
, Q% d( |9 G: g5 d4 V - *
2 C9 z8 o& R. O0 g9 K - * Passing the "non-strict" value creates a postEvent function
+ ?% O) y' [$ I! i - * that doesn't throw errors but warns about missing method$ Q5 Y ?6 N: V3 w, @, _
- * support.
4 p: ^! ~4 t4 o5 ` - *
/ {+ L8 D) u) ` - * @default 'strict'
; [( c, Q6 _4 `0 |9 L0 l) ` - * @see createPostEvent3 |* o$ E6 j+ r0 A" G
- */
@3 C! s3 O4 Z& K - postEvent?: PostEventFn | 'strict' | 'non-strict';) i+ N5 }3 ~+ O3 d: b* P
- }
复制代码在大多数情况下,开发人员不需要使用这些选项。 调用该函数后,将配置软件包的全局依赖关系,并创建 Telegram Mini Apps 事件处理程序。 - import { init } from '@telegram-apps/sdk';' v" L- o& L0 i8 \5 I% A
- 0 \1 Z, f( q: Y) p5 K# C( r( n
- init();
复制代码
: V0 U# f4 |+ V, T- ~0 }
: N \5 W4 U2 v" w$ c/ f |