weed-sim / src /client /telegram.ts
Yufok1
Port WEED-SIM from Reddit/Devvit to Telegram Mini App on HF Space
3f61b9c
Raw
History Blame Contribute Delete
977 Bytes
// Minimal typed access to the Telegram WebApp bridge (loaded via telegram-web-app.js).
// Everything is null-guarded so the game still runs in a plain browser (dev mode).
export type TelegramInvoiceStatus = 'paid' | 'cancelled' | 'failed' | 'pending';
type TelegramWebApp = {
initData: string;
ready: () => void;
expand: () => void;
openInvoice: (url: string, callback?: (status: TelegramInvoiceStatus) => void) => void;
};
export function telegramWebApp(): TelegramWebApp | null {
return (window as { Telegram?: { WebApp?: TelegramWebApp } }).Telegram?.WebApp ?? null;
}
/** Auth header for every API call; empty outside Telegram (server dev mode allows that). */
export function tgHeaders(): Record<string, string> {
const initData = telegramWebApp()?.initData ?? '';
return initData ? { 'X-Telegram-Init-Data': initData } : {};
}
export function bootTelegram(): void {
const app = telegramWebApp();
if (!app) return;
app.ready();
app.expand();
}