UNPKG

skailan-conversations

Version:

Servicio de conversaciones y mensajería para Skailan

137 lines 4.71 kB
import axios from "axios"; export class TelegramService { botToken; baseUrl; constructor(botToken) { this.botToken = botToken; this.baseUrl = `https://api.telegram.org/bot${botToken}`; } async sendMessage(chatId, text, options = {}) { try { const messageData = { chat_id: chatId, text: text, parse_mode: "HTML", ...options, }; const response = await axios.post(`${this.baseUrl}/sendMessage`, messageData); const data = response.data; if (data.ok) { console.log(`✅ Mensaje de Telegram enviado: "${text}" a chat ${chatId}`); return data.result; } else { console.log(`❌ Error al enviar mensaje de Telegram: ${data.description}`); throw new Error(data.description); } } catch (error) { console.error("❌ Error enviando mensaje de Telegram:", error); throw error; } } async sendInteractiveMessage(chatId, text, buttons) { try { const keyboard = { inline_keyboard: buttons.map((row) => row.map((button) => ({ text: button.text, callback_data: button.callback_data, }))), }; const messageData = { chat_id: chatId, text: text, parse_mode: "HTML", reply_markup: keyboard, }; const response = await axios.post(`${this.baseUrl}/sendMessage`, messageData); const data = response.data; if (data.ok) { console.log(`✅ Mensaje interactivo de Telegram enviado a chat ${chatId}`); return data.result; } else { console.log(`❌ Error al enviar mensaje interactivo: ${data.description}`); throw new Error(data.description); } } catch (error) { console.error("❌ Error enviando mensaje interactivo de Telegram:", error); throw error; } } async answerCallbackQuery(callbackQueryId, text) { try { const data = { callback_query_id: callbackQueryId, }; if (text) { data.text = text; } const response = await axios.post(`${this.baseUrl}/answerCallbackQuery`, data); if (response.data.ok) { console.log(`✅ Callback query respondido: ${callbackQueryId}`); return response.data.result; } else { console.log(`❌ Error al responder callback query: ${response.data.description}`); throw new Error(response.data.description); } } catch (error) { console.error("❌ Error respondiendo callback query:", error); throw error; } } async getBotInfo() { try { const response = await axios.get(`${this.baseUrl}/getMe`); if (response.data.ok) { return response.data.result; } else { throw new Error(response.data.description); } } catch (error) { console.error("❌ Error obteniendo información del bot:", error); throw error; } } async setWebhook(url) { try { const response = await axios.post(`${this.baseUrl}/setWebhook`, { url: url, allowed_updates: ["message", "callback_query"], drop_pending_updates: true, }); if (response.data.ok) { console.log(`✅ Webhook configurado: ${url}`); return response.data.result; } else { throw new Error(response.data.description); } } catch (error) { console.error("❌ Error configurando webhook:", error); throw error; } } async getWebhookInfo() { try { const response = await axios.get(`${this.baseUrl}/getWebhookInfo`); if (response.data.ok) { return response.data.result; } else { throw new Error(response.data.description); } } catch (error) { console.error("❌ Error obteniendo información del webhook:", error); throw error; } } } //# sourceMappingURL=TelegramService.js.map