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.

36 lines (33 loc) 1.11 kB
/** * @class MessagingModule * @description Manages sending data and messages from the Mini App. */ export default class MessagingModule { /** * @param {object} tg - The Telegram WebApp object. */ constructor(tg) { this.tg = tg; } /** * Sends data to the bot. * The bot will receive this data as a `web_app_data` message. * The data should be a string, typically a JSON string. * @param {string|object} data - The data to send. If an object, it will be stringified. */ sendData(data) { let dataString = typeof data === 'string' ? data : JSON.stringify(data); this.tg.sendData(dataString); } /** * Prompts the user for permission to send messages to them via the bot. * @returns {Promise<boolean>} A promise that resolves with true if access was granted, false otherwise. */ requestWriteAccess() { return new Promise((resolve) => { this.tg.requestWriteAccess((granted) => { resolve(granted); }); }); } }