Files
app_umbrix/lib/telegram-utils.ts

28 lines
789 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Утилита для работы с Telegram WebApp - убирает дубликаты кода
import type { TelegramUserData } from '@/types/telegram';
export const getTelegramData = (): TelegramUserData => {
const telegramWebApp = (window as any).Telegram?.WebApp;
const user = telegramWebApp?.initDataUnsafe?.user;
return {
webApp: telegramWebApp || null,
telegramId: user?.id || null,
telegramUsername: user?.username || null,
firstName: user?.first_name || null,
lastName: user?.last_name || null,
};
};
export const closeTelegramWebApp = (message?: string) => {
const { webApp } = getTelegramData();
if (webApp) {
if (message) {
webApp.showAlert(message, () => webApp.close());
} else {
webApp.close();
}
}
};