启动参数 本节 SDK 涵盖与 launch parameters 相关的主题。 解析要解析作为启动参数的值,软件包提供了 parseLaunchParams 方法。 方法接受以字符串或 URLSearchParams 实例形式呈现的查询参数,并返回 LaunchParams 接口。 如果传递的数据无效,则会出错。 Usage Example : - import { parseLaunchParams, launchParamsParser } from '@telegram-apps/sdk';1 Y: i+ w6 l6 D. i& _, k, N* l
1 D. @2 U" I2 u3 z0 Z1 L- parseLaunchParams( M2 b" |5 _2 a0 x1 {
- new URLSearchParams([
! M! j; j' D: |& n - ['tgWebAppVersion', '6.7'],: B2 r$ |- I" L. M( Q9 ~+ G- ^
- ['tgWebAppPlatform', 'tdekstop'],
& I2 A3 u# {3 y! L, u6 m - ['tgWebAppBotInline', '1'], }3 t+ f: B- V9 A. O
- ['tgWebAppData', new URLSearchParams([1 B2 D" A/ Z6 _% w6 C# `" u
- ['query_id', 'AAHdF6IQAAAAAN0XohAOqR8k'],- }1 S/ h: D% q2 { C( L+ }4 G
- ['user', JSON.stringify({( x. x; C( o l7 K/ c
- id: 279058397,7 b0 j5 Z% d) ]4 S( E7 u
- first_name: 'Vladislav',
8 T* r4 w, z% G2 k7 f8 c c% k - last_name: 'Kibenko',0 C8 C7 x, E1 i/ i
- username: 'vdkfrost',
2 O, e' B6 [! x3 g - language_code: 'ru',/ Y' n# c* O1 c1 j; V
- is_premium: true,
8 U6 j. h& i+ [; F$ ^2 ]2 M - allows_write_to_pm: true,9 O& C* A2 b6 `& n$ ~
- })],
9 J2 ^4 E: Z% e' }( ?9 p - ['auth_date', '1691441944'],
7 V/ _# u8 \ X" @' ^8 ]4 G% j - ['hash', 'abc'],
+ s1 ~0 M. h5 k( S2 a! R+ s1 N6 n) t - ]).toString()],. j' d1 a+ @ R9 W: o7 [
- ['tgWebAppThemeParams', JSON.stringify({6 R& J2 o. ]: F- h
- bg_color: '#17212b', {- a; v u$ ~& N) u
- button_color: '#5288c1',
3 f' }0 f0 z7 k7 F7 c; H - button_text_color: '#ffffff',6 ?, R) M5 m7 ^! H& I
- hint_color: '#708499',8 v) ]. Y% s. w1 N/ D/ J8 e
- link_color: '#6ab3f3',) u" O( x* d1 s# R- M/ ^- D T8 N4 V. D
- secondary_bg_color: '#232e3c',
2 X# X& q; ?# i3 s4 t8 d4 @+ R' d - text_color: '#f5f5f5',1 l. y2 f; x! s) W: e5 Z8 Y
- })],
# C1 e- U) z+ I - ]),
+ J" V0 k7 a$ S6 ~, G - );
复制代码Expected Result : - const result = {# e3 P o; o/ U/ {$ I( l: T
- botInline: true,
4 ~- B% F1 t1 [ - version: '6.7',4 Q S* ]7 O4 V/ h2 T
- platform: 'tdesktop',
- x) o8 }' S; y - themeParams: {
& {! |) C# E* q - bgColor: '#17212b',- B2 ^* w* I& \' f8 w
- buttonColor: '#5288c1',
3 W4 P* F4 {7 M1 | - buttonTextColor: '#ffffff',3 q* A9 E# O; ~6 O' ^
- hintColor: '#708499',* q1 |/ r( M ~5 i0 G4 @
- linkColor: '#6ab3f3',
# j9 S5 @! C3 H - secondaryBgColor: '#232e3c',6 V- e0 n {- p9 @: Y5 Z
- textColor: '#f5f5f5',
/ X: Q/ F% x* @" r, g - },2 g1 K0 y! E) Y" h
- initDataRaw: 'query_id=AAHdF6IQAAAAAN0XohAOqR8k&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%2C%22allows_write_to_pm%22%3Atrue%7D&auth_date=1691441944&hash=abc',
8 L% `' ^: N# R4 o7 f% D: J) B. g - initData: {
! m. O7 T5 l0 S% } - queryId: 'AAHdF6IQAAAAAN0XohAOqR8k'," x& I8 b: i% T9 ~* k2 j
- authDate: new Date(1691441944000),
; _8 D" \: R" P( j0 Q! _8 e - hash: 'abc',
# M4 E0 J; a+ h) o- {$ H8 M* R4 \ - user: {8 O; d; E/ S0 Y
- id: 279058397,3 y' |& R% }6 u$ [8 Y) C; k
- firstName: 'Vladislav',
" M6 H# g; f' M0 L# I - lastName: 'Kibenko',
" U% `2 R3 R) P4 g; O - username: 'vdkfrost',
5 ?8 Q/ Y6 U' F - languageCode: 'ru',4 R% e2 F2 h' I3 p1 D- S
- isPremium: true,* Z4 \! {- y. l; t! ~
- allowsWriteToPm: true,$ |# k0 V+ D* q6 u
- },
3 _" M7 ]/ k+ X6 d8 Z. I, s - },
6 D' \% ?- A$ g$ h - };
复制代码 序列化要将启动参数对象表示转换为字符串,开发人员应使用 的 serializeLaunchParams 函数: - import { serializeLaunchParams } from '@telegram-apps/sdk';
/ @8 }8 c* u: u( F - % W6 U6 m: X- i
- serializeLaunchParams({
8 e9 U" l2 R) P' ~1 Z# l* p - version: '6.7',! D$ `3 y) V1 S$ S- K
- platform: 'tdesktop',
% w/ E3 d$ c5 F$ e7 f; y* e% V9 _! C - themeParams: {! V4 i$ s0 {+ z# [3 r
- bgColor: '#17212b',, w. Q. t3 ]$ v* u- w- X
- buttonColor: '#5288c1'," q* ^$ t9 G4 y' i; K
- buttonTextColor: '#ffffff',
" I+ z" l9 s6 o2 c& F3 Z - hintColor: '#708499',
* l0 k) P! z) P$ D9 h - linkColor: '#6ab3f3',% N$ {# A. E5 f9 `( J
- secondaryBgColor: '#232e3c'," g7 U! a6 c3 H5 d$ j
- textColor: '#f5f5f5',6 _2 u, C) m* X2 b* ^/ K3 a$ F
- },
5 k5 z' K f" ]* c% y) s - });0 o% V0 i% }# F8 p- b
) u0 N0 i* }/ [ w- // Result:# ^2 n, U$ W+ v. s' Y
- // tgWebAppVersion=6.7
% b) n5 w8 }4 m; q4 J - // &tgWebAppPlatform=tdesktop
! `8 w& q g+ C5 c+ k - // &tgWebAppThemeParams=%7B%22bg_color%22%3A%22%2317212b%22%2C%22button_color%22%3A%22%235288c1%22%2C%22button_text_color%22%3A%22%23ffffff%22%2C%22hint_color%22%3A%22%23708499%22%2C%22link_color%22%3A%22%236ab3f3%22%2C%22secondary_bg_color%22%3A%22%23232e3c%22%2C%22text_color%22%3A%22%23f5f5f5%22%7D
复制代码 检索该软件包可使用 的 retrieveLaunchParams 函数从当前环境中提取启动参数。 如果所有已知数据源都包含无效数据,则 会抛出错误。 - import { retrieveLaunchParams } from '@telegram-apps/sdk';# M8 Q4 Y% u4 Q. Q3 z! Z
: t& f8 i( N# G+ m! E* } A8 r- const launchParams = retrieveLaunchParams();
复制代码
" N9 C$ V; c! Y$ r1 p# X* S- I* J. z/ Q8 e4 Q8 }$ t5 k9 y
|