此软件包只能在 Telegram 应用程序中使用。 由于非 Telegram 环境缺乏 Telegram 特有的特性,调用 retrieveLaunchParams 或 postEvent 等方法会导致错误。 不过,该软件包提供了 mockTelegramEnv 函数,用于模仿 Telegram 提供的环境 。 它可以帮助开发人员在 BotFather 中创建迷你 应用程序记录,甚至无需创建记录即可开始开发应用程序。 该函数接受原始或解析格式的发射参数。 下面就是一个例子: - import { mockTelegramEnv, parseInitData } from '@telegram-apps/sdk';
: ?4 s/ N5 a3 `+ H0 j
1 ?8 ~% | W/ P5 r q/ h) h- const initDataRaw = new URLSearchParams([( ]6 m$ }& U6 U6 @) Z, N5 E
- ['user', JSON.stringify({3 S7 c) v1 H1 j* Q
- id: 99281932,( l w% _7 R1 B: U6 L
- first_name: 'Andrew',
5 E. W! }6 J% l; }; D6 d& g - last_name: 'Rogue',- L! I5 J: e, z- |7 l0 I8 g
- username: 'rogue',
7 _" c6 A9 w8 v" s- E. G, U - language_code: 'en',( x: B- r- z8 R3 n+ h' m
- is_premium: true,1 J; S6 n0 ^! E
- allows_write_to_pm: true,1 C" h( l- P/ W* v3 _6 x0 Y% }
- })],$ K# R# t4 r, y9 {! z8 r
- ['hash', '89d6079ad6762351f38c6dbbc41bb53048019256a9443988af7a48bcad16ba31'],
/ |& o$ h7 x2 Y9 @' U - ['auth_date', '1716922846'],9 u# N8 l9 z* y2 ?& X/ a
- ['start_param', 'debug'],' M) _( v) K& u3 k& @# n; L1 L
- ['chat_type', 'sender'],
4 J8 t& [0 Q3 I# ]2 Y5 n - ['chat_instance', '8428209589180549439'],
8 h+ c; L5 L) ~5 X, `- Z* j8 F - ]).toString();# }4 r' P! |- U" u8 F, A
- 8 k/ _5 j U G) T+ m
- mockTelegramEnv({ K$ o! B5 w$ ]
- themeParams: {
9 Q' z( e, M0 D; D+ Z7 m+ u - accentTextColor: '#6ab2f2',
d; V! `' ^$ y- G0 j: y - bgColor: '#17212b',
7 k% O3 J1 o6 q4 x - buttonColor: '#5288c1',+ O( W$ M; M# A& d
- buttonTextColor: '#ffffff',
6 G" c9 C! s' u; G6 P) y1 g/ Y - destructiveTextColor: '#ec3942',' l) `' {5 r* M+ j
- headerBgColor: '#17212b',2 ~% n: M8 u+ m6 J
- hintColor: '#708499',
% b7 b; T# N$ \1 D. P2 { - linkColor: '#6ab3f3', f+ e: ^& w5 y) T- F6 I" _. f
- secondaryBgColor: '#232e3c',) j# M' Q! [( ?3 |9 I5 U
- sectionBgColor: '#17212b',
( t9 l8 L* l; y- O( E0 y - sectionHeaderTextColor: '#6ab3f3',2 _$ T1 V# {% ]9 ^9 B
- subtitleTextColor: '#708499',
; Q: ]$ h k% @7 C5 C# Z# M! ~6 j - textColor: '#f5f5f5',3 P+ |1 [- p( p! m
- },) q' } \9 B. @
- initData: parseInitData(initDataRaw)," @0 q& l& h5 c
- initDataRaw,
1 h5 J' C1 \/ \! I0 o6 [' v/ c - version: '7.2',0 r0 f+ B) _) |% Z6 {: q
- platform: 'tdesktop',
' o; f* Q! N1 e7 Z5 ~. y( {3 b. i$ y - });
复制代码WARNING 请注意,此功能只能模仿 Telegram 环境行为。 它不会发送任何真正的 请求,也不会执行只能在 Telegram 应用程序中看到的操作。 2 V: R/ N4 b8 |- C V1 I
|