feat: notifikasi admin/monitoring via Telegram (WA fallback); alert staff dialihkan ke Telegram

This commit is contained in:
oxpoe committed 2026-10-09 09:23:06 +07:00
1 parent 52f6be26d1
commit e838645531
7 files changed
+35 -9

No files matched your search

+1 -1
View File
@@ -824,7 +824,7 @@ async function sendAutoLunasRecap(pMonth, pYear) {
const list = rows.map(function (r) { return '• ' + r.name + ' (Rp ' + Number(r.amount || 0).toLocaleString('id-ID') + ')'; }).join('\n');
const total = rows.reduce(function (s, r) { return s + Number(r.amount || 0); }, 0);
const msg = '📋 *REKAP AUTO-LUNAS — Periode ' + m + '/' + y + '*\n\nJumlah : *' + rows.length + ' pelanggan*\nNominal : *Rp ' + total.toLocaleString('id-ID') + '*\n\nDaftar:\n' + list + '\n\nWaktu : ' + now.toLocaleString('id-ID');
const to = await waNotify.sendToStaff(msg);
const to = await waNotify.notifyAdmin(msg);
logger.info('[CRON] Rekap auto-lunas ' + m + '/' + y + ' terkirim ke admin (' + rows.length + ' pelanggan).');
return { sent: true, count: rows.length, period: m + '/' + y, to: to };
} catch (e) { logger.error('[CRON] rekap auto-lunas error: ' + (e && e.message)); return { sent: false, error: e && e.message }; }
+1 -1
View File
@@ -943,7 +943,7 @@ async function autoDeactivateOverdueCustomers(months = 3, actor = null) {
const parts = [];
if (processed) parts.push(`${processed} pelanggan NONAKTIF (menunggak ${m} bln)`);
if (removed.length) parts.push(`${removed.length} pelanggan DIHAPUS (menunggak > ${m} bln)`);
await waNotify.sendToStaff(`📋 *Auto-Tunggakan*
await waNotify.notifyAdmin(`📋 *Auto-Tunggakan*
${parts.join('; ')}.
Nonaktif: ${names.join(', ') || '-'}
+15 -1
View File
@@ -998,4 +998,18 @@ function initTelegram() {
}
// Export for manual re-init from settings
module.exports = { initTelegram };
async function notifyAdmin(text) {
try {
const adminId = String(getSetting('telegram_admin_id', '') || '').trim();
if (!bot || !adminId || !text) return false;
const ids = adminId.split(/[,s]+/).filter(Boolean);
let ok = false;
for (const id of ids) {
try { await bot.sendMessage(id, text, { parse_mode: 'Markdown' }); ok = true; }
catch (e) { try { await bot.sendMessage(id, String(text)); ok = true; } catch (e2) {} }
}
return ok;
} catch (e) { return false; }
}
module.exports = { initTelegram, notifyAdmin };
+13 -1
View File
@@ -154,4 +154,16 @@ async function isConnected() {
return val;
}
module.exports = { sendText, sendImage, sendDocument, sendToStaff, isConnected, gateway };
// Notifikasi untuk ADMIN/monitoring → Telegram (utama). WA (sendToStaff) sebagai fallback.
async function notifyAdmin(text) {
try {
const tg = require('./telegramBot');
if (tg && typeof tg.notifyAdmin === 'function') {
const ok = await tg.notifyAdmin(text);
if (ok) return true;
}
} catch (e) {}
return sendToStaff(text);
}
module.exports = { sendText, sendImage, sendDocument, sendToStaff, notifyAdmin, isConnected, gateway };