UNPKG

tg-mini-app-binder

Version:

A modular and extendable wrapper for the Telegram Mini App API to simplify interactions between your web app and the Telegram client.

47 lines (44 loc) 1.68 kB
/** * @class LinksModule * @description Manages opening external links, invoices, and other Telegram links from the Mini App. */ export default class LinksModule { /** * @param {object} tg - The Telegram WebApp object. */ constructor(tg) { this.tg = tg; } /** * Opens a link within the Telegram app. * The link should be a t.me link, for example, a link to another Mini App or a user profile. * @param {string} url - The full t.me URL to open. * @example * // Opens another Mini App * openTelegramLink('https://t.me/your_bot_username/your_app_name'); * @example * // Opens a user profile * openTelegramLink('https://t.me/username'); */ openTelegramLink(url) { if (!url || !url.startsWith('https://t.me/')) { console.error("Invalid Telegram link. URL must start with 'https://t.me/'."); // You might want to show this error to the user in the UI return; } this.tg.openTelegramLink(url); } /** * Opens an invoice by its URL. * When the invoice is closed, the bot will receive a service message about a successful payment. * @param {string} url - The invoice URL received from the bot. * @param {Function} [callback] - Optional. A callback function that will be called when the invoice is closed. It receives a status string: 'paid', 'cancelled', 'failed', 'pending'. */ openInvoice(url, callback) { this.tg.openInvoice(url, (status) => { if (callback) { callback(status); } }); } }