From 81c99c6c5f159c11a040e9dcaa6c0e1c0f10f5ec Mon Sep 17 00:00:00 2001 From: Hewston Fox Date: Wed, 18 Mar 2026 02:02:25 +0200 Subject: [PATCH] fix: tg detection --- src/tg/index.ts | 67 ++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/tg/index.ts b/src/tg/index.ts index 20d84d3..b460cf2 100644 --- a/src/tg/index.ts +++ b/src/tg/index.ts @@ -5,34 +5,39 @@ export const STORAGE_KEYS = { } as const; export type StorageKey = (typeof STORAGE_KEYS)[keyof typeof STORAGE_KEYS]; -const MOCKED_START_PARAM = `debug+${Math.random().toString(36).substring(2, 15)}`; +const isRealTelegram = () => { + if (typeof window === "undefined") return false; + const search = location.hash.slice(1) || location.search.slice(1); + return search.includes("tgWebAppData"); +}; -tg.mockTelegramEnv({ - launchParams: { - tgWebAppData: new URLSearchParams({ - user: JSON.stringify({ - id: 1, - first_name: "Pavel", - is_bot: false, - last_name: "Durov", - username: "durov", - language_code: "en", - is_premium: true, - photo_url: - "https://media4.giphy.com/media/v1.Y2lkPTZjMDliOTUyeXF1MzYyY2pwMjR2YWFhNDhqdXBsc216MWo2aW9pczNnNXM2ZmZmbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xUPGcHc4I3wICqp8bu/giphy.gif", - added_to_attachment_menu: false, - allows_write_to_pm: true, - } satisfies tg.User), - hash: "", - signature: "", - auth_date: Date.now().toString(), - }), - tgWebAppStartParam: MOCKED_START_PARAM, - tgWebAppThemeParams: {}, - tgWebAppVersion: "8", - tgWebAppPlatform: "android", - }, -}); +if (!isRealTelegram()) { + tg.mockTelegramEnv({ + launchParams: { + tgWebAppData: new URLSearchParams({ + user: JSON.stringify({ + id: 1, + first_name: "Pavel", + is_bot: false, + last_name: "Durov", + username: "durov", + language_code: "en", + is_premium: true, + photo_url: + "https://media4.giphy.com/media/v1.Y2lkPTZjMDliOTUyeXF1MzYyY2pwMjR2YWFhNDhqdXBsc216MWo2aW9pczNnNXM2ZmZmbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xUPGcHc4I3wICqp8bu/giphy.gif", + added_to_attachment_menu: false, + allows_write_to_pm: true, + } satisfies tg.User), + hash: "", + signature: "", + auth_date: Date.now().toString(), + }), + tgWebAppThemeParams: {}, + tgWebAppVersion: "8", + tgWebAppPlatform: "android", + }, + }); +} type WithChecks = { ifAvailable: (...args: any[]) => { ok: true; data: Result } | { ok: false }; @@ -44,8 +49,6 @@ const isPromise = (value: T | Promise): value is Promise => const promisify = (value: T | Promise): Promise => isPromise(value) ? value : Promise.resolve(value); -const isTMA = () => tg.retrieveLaunchParams()?.tgWebAppStartParam !== MOCKED_START_PARAM; - const fallbackImplementation = < T extends ((...args: any) => any) & WithChecks, Async extends boolean, @@ -58,7 +61,7 @@ const fallbackImplementation = < cb: T, onErr: F, ): Async extends true ? Promise : Awaited => { - if (isTMA()) { + if (isRealTelegram()) { const res = cb.ifAvailable.apply(null, args); const returnValue = (res.ok ? res.data : onErr.apply(null, args)) as Result; if (!async) return returnValue as Async extends true ? Promise : Awaited; @@ -81,7 +84,9 @@ export default { tg.swipeBehavior.disableVertical.ifAvailable(); tg.viewport.expand.ifAvailable(); tg.miniApp.setHeaderColor.ifAvailable("#000000"); - console.log(isTMA() ? "Telegram Mini App initialized" : "TMA Debug mode in Web initialized"); + console.log( + isRealTelegram() ? "Telegram Mini App initialized" : "TMA Debug mode in Web initialized", + ); }, openLink(url: string | URL, options?: tg.OpenLinkOptions) { tg.openLink.ifAvailable(url, options);