feat(telegram): tombol menu monitoring (Sistem/Tunggakan/Tiket/OLT/Trafik)

This commit is contained in:
oxpoe committed 2026-10-09 10:14:24 +07:00
1 parent 690864a847
commit 9037b78835
1 file changed
+68 -32
+68 -32
View File
@@ -297,6 +297,8 @@ function initTelegram() {
[{ text: '📊 Statistik', callback_data: 'menu_stats' }, { text: '👥 Pelanggan', callback_data: 'menu_cust' }],
[{ text: '🎫 Voucher', callback_data: 'menu_vouch' }, { text: '💰 Tagihan', callback_data: 'menu_bill' }],
[{ text: '⚙️ MikroTik Status', callback_data: 'menu_mt' }],
[{ text: '🖥️ Sistem', callback_data: 'menu_sys' }, { text: '💰 Tunggakan', callback_data: 'menu_arrears' }],
[{ text: '🎫 Tiket', callback_data: 'menu_tiket' }, { text: '📡 OLT', callback_data: 'menu_olt' }, { text: '📶 Trafik', callback_data: 'menu_trafik' }],
[{ text: '🔄 Refresh', callback_data: 'menu_main' }]
]
}
@@ -401,6 +403,28 @@ function initTelegram() {
});
}
else if (data === 'menu_sys') {
try { bot.sendMessage(chatId, tgSistemText(), { parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '⬅️ Kembali', callback_data: 'menu_main' }]] } }); }
catch (e) { bot.sendMessage(chatId, 'Gagal: ' + e.message); }
}
else if (data === 'menu_arrears') {
try { bot.sendMessage(chatId, tgTunggakanText(), { parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '⬅️ Kembali', callback_data: 'menu_main' }]] } }); }
catch (e) { bot.sendMessage(chatId, 'Gagal: ' + e.message); }
}
else if (data === 'menu_tiket') {
try { bot.sendMessage(chatId, tgTiketText(), { parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '⬅️ Kembali', callback_data: 'menu_main' }]] } }); }
catch (e) { bot.sendMessage(chatId, 'Gagal: ' + e.message); }
}
else if (data === 'menu_olt') {
bot.sendMessage(chatId, '📡 Mengambil status OLT, mohon tunggu…');
try { bot.sendMessage(chatId, await tgOltText(), { parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '⬅️ Kembali', callback_data: 'menu_main' }]] } }); }
catch (e) { bot.sendMessage(chatId, 'Gagal: ' + e.message); }
}
else if (data === 'menu_trafik') {
try { bot.sendMessage(chatId, await tgTrafikText(), { parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '⬅️ Kembali', callback_data: 'menu_main' }]] } }); }
catch (e) { bot.sendMessage(chatId, 'Gagal: ' + e.message); }
}
else if (data === 'mt_resource') {
try {
const res = await mikrotikSvc.getSystemResource();
@@ -684,10 +708,8 @@ function initTelegram() {
});
// ── Monitoring (admin): /sistem /tunggakan /tiket /olt /trafik ──
bot.onText(/\/(sistem|monitor|health)/i, async (msg) => {
if (!isAdmin(msg)) return;
try {
// ── Monitoring (admin): helper + perintah /sistem /tunggakan /tiket /olt /trafik ──
function tgSistemText() {
const h = monitoringSvc.getHealthStatus();
const m = (h && h.metrics) ? h.metrics : monitoringSvc.getAllMetrics();
const sys = (m && m.system) || {};
@@ -706,51 +728,65 @@ function initTelegram() {
t += `🕒 Uptime: ${sys.uptime || '-'}\n`;
if (h && h.issues && h.issues.length) t += `\n🔴 *Masalah:*\n- ${h.issues.join('\n- ')}`;
if (h && h.warnings && h.warnings.length) t += `\n🟡 *Peringatan:*\n- ${h.warnings.join('\n- ')}`;
bot.sendMessage(msg.chat.id, t, { parse_mode: 'Markdown' });
} catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
return t;
}
bot.onText(/\/tunggakan/i, async (msg) => {
if (!isAdmin(msg)) return;
try {
function tgTunggakanText() {
const row = db.prepare("SELECT COUNT(*) c, COALESCE(SUM(amount),0) s FROM invoices WHERE status='unpaid'").get();
const top = db.prepare("SELECT c.name, COALESCE(c.customer_code,'-') code, SUM(i.amount) t FROM invoices i JOIN customers c ON c.id=i.customer_id WHERE i.status='unpaid' GROUP BY i.customer_id ORDER BY t DESC LIMIT 8").all();
let t = `💰 *TUNGGAKAN*\n\nTotal: *${row.c} tagihan*\nNominal: *Rp ${Number(row.s).toLocaleString('id-ID')}*\n`;
if (top.length) { t += `\n*Top ${top.length}:*\n`; top.forEach((r, i) => { t += `${i + 1}. ${r.name} (${r.code}) — Rp ${Number(r.t).toLocaleString('id-ID')}\n`; }); }
bot.sendMessage(msg.chat.id, t, { parse_mode: 'Markdown' });
} catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
return t;
}
bot.onText(/\/tiket/i, async (msg) => {
if (!isAdmin(msg)) return;
try {
function tgTiketText() {
const s = ticketSvc.getTicketStats();
const open = ticketSvc.getAllTickets('open') || [];
let t = `🎫 *TIKET*\n\nOpen: *${s.open}* · Proses: *${s.inProgress}* · Selesai: *${s.resolved}*\n`;
if (open.length) { t += `\n*Open terbaru:*\n`; open.slice(0, 8).forEach(x => { t += `#${x.id} ${x.subject} — ${x.customer_name || '-'}\n`; }); }
bot.sendMessage(msg.chat.id, t, { parse_mode: 'Markdown' });
} catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
return t;
}
bot.onText(/\/(olt|oltstatus|olt-status)/i, async (msg) => {
if (!isAdmin(msg)) return;
bot.sendMessage(msg.chat.id, '📡 Mengambil status OLT, mohon tunggu…');
try {
async function tgOltText() {
const o = await oltSvc.getAllOltsStats();
let t = `📡 *OLT STATUS*\n\nONU total: *${o.onus_total}*\n🟢 Online: *${o.onus_online}*\n🔴 Offline: *${o.onus_offline}*\n🟡 Lemah: *${o.onus_weak}*\n`;
if (o.error) t += `\n_Catatan: ${o.error}_`;
bot.sendMessage(msg.chat.id, t, { parse_mode: 'Markdown' });
} catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
return t;
}
bot.onText(/\/(trafik|online)/i, async (msg) => {
if (!isAdmin(msg)) return;
try {
async function tgTrafikText() {
let count = 0;
try { const map = await mikrotikSvc.getAllActiveSessionsMap(); count = (map && map.size != null) ? map.size : ((map && map.length) || 0); } catch (e) {}
const t = `📶 *SESI AKTIF*\n\nPPPoE/Hotspot online: *${count}* sesi\n`;
bot.sendMessage(msg.chat.id, t, { parse_mode: 'Markdown' });
} catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
return `📶 *SESI AKTIF*\n\nPPPoE/Hotspot online: *${count}* sesi\n`;
}
const backBtn = { inline_keyboard: [[{ text: '⬅️ Kembali', callback_data: 'menu_main' }]] };
bot.onText(/\/(sistem|monitor|health)/i, async (msg) => {
if (!isAdmin(msg)) return;
try { bot.sendMessage(msg.chat.id, tgSistemText(), { parse_mode: 'Markdown', reply_markup: backBtn }); }
catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
bot.onText(/\/tunggakan/i, async (msg) => {
if (!isAdmin(msg)) return;
try { bot.sendMessage(msg.chat.id, tgTunggakanText(), { parse_mode: 'Markdown', reply_markup: backBtn }); }
catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
bot.onText(/\/tiket/i, async (msg) => {
if (!isAdmin(msg)) return;
try { bot.sendMessage(msg.chat.id, tgTiketText(), { parse_mode: 'Markdown', reply_markup: backBtn }); }
catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
bot.onText(/\/(olt|oltstatus|olt-status)/i, async (msg) => {
if (!isAdmin(msg)) return;
bot.sendMessage(msg.chat.id, '📡 Mengambil status OLT, mohon tunggu…');
try { bot.sendMessage(msg.chat.id, await tgOltText(), { parse_mode: 'Markdown', reply_markup: backBtn }); }
catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
bot.onText(/\/(trafik|online)/i, async (msg) => {
if (!isAdmin(msg)) return;
try { bot.sendMessage(msg.chat.id, await tgTrafikText(), { parse_mode: 'Markdown', reply_markup: backBtn }); }
catch (e) { bot.sendMessage(msg.chat.id, 'Gagal: ' + e.message); }
});
bot.onText(/\/ringkasan/i, async (msg) => {