global update and global fix
This commit is contained in:
46
apps/frontend/lib/bot-menu-button.ts
Normal file
46
apps/frontend/lib/bot-menu-button.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user