Aggiunto collegamento al server
This commit is contained in:
152
plugin/telegram/callbacks/dashboard.js
Normal file
152
plugin/telegram/callbacks/dashboard.js
Normal file
@@ -0,0 +1,152 @@
|
||||
// Mappa globale per salvare gli interval id anche dopo un "hot-reload"
|
||||
if (!global.__meb_live_dashboards) {
|
||||
global.__meb_live_dashboards = new Map();
|
||||
}
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
id: 'dashboard-refresh',
|
||||
execute: async ({ bot, chatId, msg }) => {
|
||||
const dash = require('../commands/dashboard.js');
|
||||
const newText = dash.formatSensorData();
|
||||
|
||||
try {
|
||||
await bot.editMessageText(newText, {
|
||||
chat_id: chatId,
|
||||
message_id: msg.message_id,
|
||||
parse_mode: 'Markdown',
|
||||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[
|
||||
{ text: "⚡️ Avvia Live Tracker (2s)", callback_data: 'dashboard-live-start' }
|
||||
],
|
||||
[
|
||||
{ text: "🔄 Ricarica Dati", callback_data: 'dashboard-refresh' }
|
||||
]
|
||||
]
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
if (!e.message.includes('message is not modified')) {
|
||||
console.error("Errore nel refresh dashboard:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'dashboard-live-start',
|
||||
execute: async ({ bot, chatId, msg }) => {
|
||||
const dash = require('../commands/dashboard.js');
|
||||
|
||||
const messageId = msg.message_id;
|
||||
const liveKey = `${chatId}_${messageId}`;
|
||||
|
||||
// Se è già attivo un live per questo messaggio, non fare nulla
|
||||
if (global.__meb_live_dashboards.has(liveKey)) return;
|
||||
|
||||
// Avvisa che sta partendo
|
||||
const startMarkup = {
|
||||
inline_keyboard: [
|
||||
[
|
||||
{ text: "🛑 Ferma Live Tracker", callback_data: 'dashboard-live-stop' }
|
||||
]
|
||||
]
|
||||
};
|
||||
|
||||
await bot.editMessageReplyMarkup(startMarkup, { chat_id: chatId, message_id: messageId });
|
||||
|
||||
// Inizializza l'interval a 2 secondi. Autodistruzione dopo 30s
|
||||
let count = 15; // 15 tick da 2 secondi = 30 secondi
|
||||
const intervalTimer = setInterval(async () => {
|
||||
count--;
|
||||
const baseText = dash.formatSensorData();
|
||||
|
||||
// Se il tempo scade, disattiva il live e ripristina i tasti normali
|
||||
if (count <= 0) {
|
||||
if (global.__meb_live_dashboards.has(liveKey)) {
|
||||
clearInterval(global.__meb_live_dashboards.get(liveKey));
|
||||
global.__meb_live_dashboards.delete(liveKey);
|
||||
}
|
||||
try {
|
||||
await bot.editMessageText(baseText + `\n🛑 _Live tracker terminato automaticamente (30s) per risparmiare risorse._`, {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
parse_mode: 'Markdown',
|
||||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[{ text: "⚡️ Avvia Live Tracker (2s)", callback_data: 'dashboard-live-start' }],
|
||||
[{ text: "🔄 Ricarica Dati", callback_data: 'dashboard-refresh' }]
|
||||
]
|
||||
}
|
||||
});
|
||||
} catch (e) { }
|
||||
return;
|
||||
}
|
||||
|
||||
// Altrimenti prosegui con l'aggiornamento e la stringa del countdown
|
||||
const newText = baseText + `\n⏳ _Live attivo: arresto automatico tra *${count * 2}s*_`;
|
||||
|
||||
try {
|
||||
await bot.editMessageText(newText, {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
parse_mode: 'Markdown',
|
||||
reply_markup: startMarkup
|
||||
});
|
||||
} catch (e) {
|
||||
// API limits o the message was not modified
|
||||
if (e.response && e.response.statusCode === 400 && e.message.includes("message is not modified")) {
|
||||
// ignore
|
||||
} else if (e.response && e.response.statusCode === 429) {
|
||||
// Troppe richieste Telegram
|
||||
console.warn("[Telegram Dashboard] Rate Limit raggionto. Riprovo più tardi...");
|
||||
} else if (e.response && e.response.statusCode === 400 && e.message.includes("message to edit not found")) {
|
||||
// Il messaggio è stato cancellato dall'utente
|
||||
clearInterval(intervalTimer);
|
||||
global.__meb_live_dashboards.delete(liveKey);
|
||||
} else {
|
||||
console.error("[Telegram Dashboard] Errore update live:", e);
|
||||
}
|
||||
}
|
||||
}, 2000);
|
||||
|
||||
global.__meb_live_dashboards.set(liveKey, intervalTimer);
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'dashboard-live-stop',
|
||||
execute: async ({ bot, chatId, msg }) => {
|
||||
const dash = require('../commands/dashboard.js');
|
||||
|
||||
const messageId = msg.message_id;
|
||||
const liveKey = `${chatId}_${messageId}`;
|
||||
|
||||
// Pulisci l'interval se esiste
|
||||
if (global.__meb_live_dashboards.has(liveKey)) {
|
||||
clearInterval(global.__meb_live_dashboards.get(liveKey));
|
||||
global.__meb_live_dashboards.delete(liveKey);
|
||||
}
|
||||
|
||||
// Ripristina la formattazione iniziale
|
||||
const newText = dash.formatSensorData();
|
||||
|
||||
try {
|
||||
await bot.editMessageText(newText, {
|
||||
chat_id: chatId,
|
||||
message_id: messageId,
|
||||
parse_mode: 'Markdown',
|
||||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[
|
||||
{ text: "⚡️ Avvia Live Tracker (2s)", callback_data: 'dashboard-live-start' }
|
||||
],
|
||||
[
|
||||
{ text: "🔄 Ricarica Dati", callback_data: 'dashboard-refresh' }
|
||||
]
|
||||
]
|
||||
}
|
||||
});
|
||||
} catch (e) { }
|
||||
}
|
||||
}
|
||||
];
|
||||
Reference in New Issue
Block a user