export type ComposerMenuButton = { type: 'web_app'; text: string; web_app: { url: string }; }; export function parseComposerMenuButtonJson(raw?: string | null): ComposerMenuButton | null { if (!raw?.trim()) { return null; } try { const parsed = JSON.parse(raw) as unknown; if ( typeof parsed === 'object' && parsed !== null && (parsed as ComposerMenuButton).type === 'web_app' && typeof (parsed as ComposerMenuButton).text === 'string' && typeof (parsed as ComposerMenuButton).web_app?.url === 'string' ) { const button = parsed as ComposerMenuButton; const url = button.web_app.url.trim(); if (!url) { return null; } return { type: 'web_app', text: button.text.trim() || 'App', web_app: { url } }; } } catch { return null; } return null; } export function resolveComposerMenuButton(options: { composerMenuButtonJson?: string | null; composerWebAppUrl?: string | null; }): ComposerMenuButton | null { const fromJson = parseComposerMenuButtonJson(options.composerMenuButtonJson); if (fromJson) { return fromJson; } const url = options.composerWebAppUrl?.trim(); if (url) { return { type: 'web_app', text: 'App', web_app: { url } }; } return null; }