UNPKG

tgram.js

Version:

Lightweight modern library for telegram bot. use Node.js

38 lines (36 loc) 1.26 kB
/** * Send a message with modern inline buttons * @param {string} token - Telegram Bot Token * @param {number|string} chatId - The target chat ID * @param {string} text - Message text * @param {Array} buttons - Buttons in 1D or 2D array format * @param {Object} options - Additional Telegram sendMessage options */ export async function sendButton(token, chatId, text, buttons = [], options = {}) { const inline_keyboard = buttons.map(row => Array.isArray(row) ? row.map(btn => { if (Array.isArray(btn) && btn.length === 2) { return { text: btn[0], callback_data: btn[1] } } else if (typeof btn === "string") { return { text: btn, callback_data: btn } } else { return btn // fallback } }) : [typeof row === "string" ? { text: row, callback_data: row } : Array.isArray(row) && row.length === 2 ? { text: row[0], callback_data: row[1] } : row ] ) const payload = { chat_id: chatId, text, parse_mode: options.parse_mode || "Markdown", reply_markup: { inline_keyboard } } const res = await telegramFetch(`https://api.telegram.org/bot${token}/sendMessage`, payload) return res }