Files
signalk-plugin/plugin/telegram/core.js
Giuseppe Raffa bb8d267cd4 Aggiunta stili CSS per Kiosk, struttura HTML per la Mappa e Riferimenti ai Sensori
• Creato un nuovo file CSS per gli stili del chiosco (kiosk) con variabili, stili per le schede (card) e animazioni.
• Aggiunto un file HTML per l'interfaccia della mappa utilizzando Mapbox, inclusi gli stili e il JavaScript per le funzionalità della mappa.
• Introdotto un file JSON per i riferimenti ai sensori, definendo percorsi ed elementi per i dati di temperatura, vento, onde, posizione, batteria, motore e sistema.

Co-authored-by: Copilot <copilot@github.com>
2026-04-23 16:19:11 +02:00

122 lines
3.4 KiB
JavaScript

const TelegramBot = require('node-telegram-bot-api');
const fs = require('fs');
const path = require('path');
const configManager = require('../config/configManager.js');
let bot = null;
/**
* Inizializza il bot Telegram con il token dalle configurazioni del plugin.
* Carica automaticamente i comandi dalla cartella commands/ e i callback dalla cartella callbacks/.
* @returns {TelegramBot|null} L'istanza del bot o null se il token non è disponibile.
*/
function init() {
const token = configManager.getTelegramToken();
if (!token) {
console.error('[TELEGRAM] TELEGRAM_BOT_TOKEN non trovato nelle configurazioni del plugin');
return null;
}
bot = new TelegramBot(token, { polling: true });
loadCommands();
loadCallbacks();
bot.on('polling_error', (error) => {
console.error('[TELEGRAM] Errore polling:', error.message);
});
return bot;
}
/**
* Carica tutti i file dalla cartella commands/ e li registra come handler per i comandi.
* Ogni file deve esportare un oggetto con { command: string, handler: function(bot, msg, match) }
*/
function loadCommands() {
const commandsDir = path.join(__dirname, 'commands');
if (!fs.existsSync(commandsDir)) return;
const files = fs.readdirSync(commandsDir).filter(f => f.endsWith('.js'));
for (const file of files) {
try {
const cmd = require(path.join(commandsDir, file));
if (cmd.command && cmd.handler) {
bot.onText(new RegExp(`^/${cmd.command}`), (msg, match) => {
cmd.handler(bot, msg, match);
});
}
} catch (error) {
console.error(`[TELEGRAM] Errore caricamento comando ${file}:`, error.message);
}
}
}
/**
* Carica tutti i file dalla cartella callbacks/ e li registra come handler per le callback query.
* Ogni file deve esportare un oggetto con { prefix: string, handler: function(bot, query) }
*/
function loadCallbacks() {
const callbacksDir = path.join(__dirname, 'callbacks');
if (!fs.existsSync(callbacksDir)) return;
const files = fs.readdirSync(callbacksDir).filter(f => f.endsWith('.js'));
const handlers = [];
for (const file of files) {
try {
const cb = require(path.join(callbacksDir, file));
if (cb.prefix && cb.handler) {
handlers.push(cb);
}
} catch (error) {
console.error(`[TELEGRAM] Errore caricamento callback ${file}:`, error.message);
}
}
if (handlers.length > 0) {
bot.on('callback_query', (query) => {
const matched = handlers.find(h => query.data.startsWith(h.prefix));
if (matched) {
matched.handler(bot, query);
}
});
}
}
/**
* Restituisce l'istanza del bot (se inizializzato)
* @returns {TelegramBot|null}
*/
function getBot() {
return bot;
}
/**
* Invia un messaggio ad un chatId specifico
* @param {Number|String} chatId
* @param {String} text
* @param {Object} options - opzioni aggiuntive (parse_mode, reply_markup, ecc.)
*/
async function send(chatId, text, options = {}) {
if (!bot) {
console.error('[TELEGRAM] Bot non inizializzato');
return;
}
try {
await bot.sendMessage(chatId, text, options);
} catch (error) {
console.error(`[TELEGRAM] Errore invio messaggio: ${error.message}`);
}
}
module.exports = {
init,
getBot,
send
};