460 lines
14 KiB
JavaScript
460 lines
14 KiB
JavaScript
const { Client, LocalAuth } = require('whatsapp-web.js');
|
|
const qrcode = require('qrcode');
|
|
const qrcodeTerminal = require('qrcode-terminal');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const config = require('./config');
|
|
const { sendSMS } = require('./sms');
|
|
const { sendTelegramPhoto } = require('./telegram');
|
|
|
|
let messageQueue = [];
|
|
let flushTimer = null;
|
|
let flushTimerStart = null;
|
|
let client = null;
|
|
let restartDelay = 1000;
|
|
let starting = false;
|
|
let restarting = false;
|
|
let msgCounter = 0;
|
|
|
|
let startTime = Date.now();
|
|
let totalForwarded = 0;
|
|
let userStats = {};
|
|
let groupStats = {};
|
|
|
|
function ts() {
|
|
return new Date().toLocaleString('he-IL', { hour12: false });
|
|
}
|
|
|
|
function log(level, msg) {
|
|
console.log(`[${ts()}] [${level}] ${msg}`);
|
|
}
|
|
|
|
function flushTime() {
|
|
if (!flushTimerStart) return '--:--';
|
|
const t = new Date(Date.now() + Math.max(0, config.batch.intervalMs - (Date.now() - flushTimerStart)));
|
|
return t.toLocaleTimeString('he-IL', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
|
|
}
|
|
|
|
function formatBatch(queue) {
|
|
const groups = {};
|
|
for (const m of queue) {
|
|
if (!groups[m.group]) groups[m.group] = [];
|
|
groups[m.group].push(m);
|
|
}
|
|
|
|
const parts = [];
|
|
for (const [group, msgs] of Object.entries(groups)) {
|
|
parts.push(`[👥 ${group} 👥]`);
|
|
for (const m of msgs) {
|
|
parts.push(`${m.sender}: ${m.text}`);
|
|
}
|
|
}
|
|
return parts.join('\n');
|
|
}
|
|
|
|
function queueSize() {
|
|
return messageQueue.reduce(
|
|
(sum, m) => sum + m.group.length + m.sender.length + m.text.length + 6,
|
|
0
|
|
);
|
|
}
|
|
|
|
function scheduleFlush() {
|
|
if (flushTimer) clearTimeout(flushTimer);
|
|
if (!flushTimerStart) flushTimerStart = Date.now();
|
|
flushTimer = setTimeout(flushQueue, config.batch.intervalMs);
|
|
}
|
|
|
|
async function flushQueue() {
|
|
if (restarting) return;
|
|
if (messageQueue.length === 0) return;
|
|
|
|
flushTimer = null;
|
|
flushTimerStart = null;
|
|
|
|
const batch = messageQueue;
|
|
messageQueue = [];
|
|
msgCounter = 0;
|
|
|
|
const text = formatBatch(batch);
|
|
|
|
try {
|
|
await sendSMS(text);
|
|
log('INFO', `Flushed ${batch.length} messages`);
|
|
for (const m of batch) {
|
|
userStats[m.sender] = (userStats[m.sender] || 0) + 1;
|
|
groupStats[m.group] = (groupStats[m.group] || 0) + 1;
|
|
totalForwarded++;
|
|
}
|
|
} catch (err) {
|
|
log('ERROR', `Flush failed: ${err.message}`);
|
|
messageQueue = batch.concat(messageQueue);
|
|
msgCounter = messageQueue.length;
|
|
scheduleFlush();
|
|
}
|
|
}
|
|
|
|
function enqueue(group, sender, text) {
|
|
msgCounter++;
|
|
messageQueue.push({ group, sender, text });
|
|
|
|
scheduleFlush();
|
|
log('QUEUE', `Queue #${msgCounter} - Message from ${sender}, flushed at ${flushTime()}`);
|
|
|
|
if (queueSize() >= config.batch.maxChars) {
|
|
clearTimeout(flushTimer);
|
|
flushTimer = null;
|
|
flushTimerStart = null;
|
|
flushQueue();
|
|
}
|
|
}
|
|
|
|
async function killClient() {
|
|
if (!client) return;
|
|
|
|
try {
|
|
client.removeAllListeners();
|
|
} catch {}
|
|
|
|
try {
|
|
await client.destroy();
|
|
} catch {}
|
|
|
|
/**
|
|
* IMPORTANT:
|
|
* Give Chromium time to release Windows file locks
|
|
*/
|
|
await new Promise(r => setTimeout(r, 3000));
|
|
|
|
client = null;
|
|
}
|
|
|
|
let keepAliveTimer = null;
|
|
|
|
function startKeepAlive() {
|
|
if (keepAliveTimer) clearInterval(keepAliveTimer);
|
|
const ka = config.keepAlive;
|
|
if (!ka || !ka.url) return;
|
|
|
|
log('INIT', `Keep-alive ping every ${ka.intervalMs / 1000}s to ${ka.url}`);
|
|
const ping = () => {
|
|
fetch(ka.url).catch(() => {});
|
|
};
|
|
ping();
|
|
keepAliveTimer = setInterval(ping, ka.intervalMs);
|
|
}
|
|
|
|
async function startClient() {
|
|
if (starting) {
|
|
log('WARN', 'Already starting — skipping duplicate call');
|
|
return;
|
|
}
|
|
|
|
starting = true;
|
|
|
|
try {
|
|
restarting = true;
|
|
|
|
log('INIT', 'Starting OmegaBaSMS...');
|
|
await killClient();
|
|
|
|
log('INIT', `Groups to monitor: ${config.groupNames.join(', ')}`);
|
|
log('INIT', `Batch interval: ${config.batch.intervalMs / 1000}s / max ${config.batch.maxChars} chars`);
|
|
log('INIT', `Forwarding SMS to: ${config.smsGateway.recipientNumber}`);
|
|
|
|
if (config.telegram.botToken) {
|
|
log('INIT', 'Telegram notifications enabled');
|
|
}
|
|
|
|
// startKeepAlive();
|
|
|
|
log('INIT', 'Launching WhatsApp Web...');
|
|
|
|
client = new Client({
|
|
authStrategy: new LocalAuth(),
|
|
puppeteer: {
|
|
headless: true,
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
|
protocolTimeout: 120000 // 2 minutes instead of default 30s
|
|
}
|
|
});
|
|
|
|
client.on('qr', async (qr) => {
|
|
log('QR', 'New QR code received — scan with WhatsApp on your phone');
|
|
qrcodeTerminal.generate(qr, { small: true });
|
|
|
|
if (config.telegram.botToken) {
|
|
try {
|
|
const buf = await qrcode.toBuffer(qr, { width: 400 });
|
|
await sendTelegramPhoto(buf, 'WhatsApp re-auth needed - scan this QR\nhttps://wa.me/settings/linked_devices');
|
|
log('QR', 'QR photo sent to Telegram');
|
|
} catch (err) {
|
|
log('ERROR', `Failed to send QR photo: ${err.message}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
client.on('ready', () => {
|
|
restarting = false;
|
|
restartDelay = 1000;
|
|
log('READY', 'WhatsApp connected successfully');
|
|
log('READY', `Monitoring ${config.groupNames.length} group(s): ${config.groupNames.join(', ')}`);
|
|
log('READY', `Forwarding to ${config.smsGateway.recipientNumber}`);
|
|
});
|
|
|
|
client.on('auth_failure', (msg) => {
|
|
log('ERROR', `Auth failure: ${msg}`);
|
|
});
|
|
|
|
client.on('disconnected', async (reason) => {
|
|
if (starting) return;
|
|
|
|
log('WARN', `Disconnected: ${reason}. Restarting in ${restartDelay / 1000}s...`);
|
|
|
|
restarting = true;
|
|
|
|
if (reason === 'LOGOUT') {
|
|
const authDir = path.join(__dirname, '.wwebjs_auth');
|
|
if (fs.existsSync(authDir)) {
|
|
try {
|
|
fs.rmSync(authDir, {
|
|
recursive: true,
|
|
force: true,
|
|
maxRetries: 10,
|
|
retryDelay: 500
|
|
});
|
|
|
|
log('WARN', 'Cleared old session data');
|
|
} catch (err) {
|
|
log('ERROR', `Failed clearing auth data: ${err.message}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
await new Promise((r) => setTimeout(r, restartDelay));
|
|
restartDelay = Math.min(restartDelay * 2, 30000);
|
|
startClient();
|
|
});
|
|
|
|
client.on('message_create', async (message) => {
|
|
try {
|
|
if (restarting) return;
|
|
if (message.type !== 'chat') return;
|
|
|
|
const chat = await message.getChat();
|
|
if (!chat.isGroup) return;
|
|
if (!config.groupNames.includes(chat.name)) return;
|
|
if (message.fromMe && !config.includeOwnMessages) return;
|
|
|
|
const contact = await message.getContact();
|
|
const sender = message.fromMe
|
|
? config.ownName
|
|
: (contact.name || contact.pushname || contact.shortName || contact.number || 'Unknown');
|
|
|
|
const body = message.body || (message.hasMedia ? '[Media]' : '');
|
|
if (!body) return;
|
|
|
|
enqueue(chat.name, sender, body);
|
|
} catch (err) {
|
|
log('ERROR', `Message handler: ${err.message}`);
|
|
}
|
|
});
|
|
|
|
client.initialize();
|
|
|
|
} finally {
|
|
starting = false;
|
|
}
|
|
}
|
|
|
|
const http = require('http');
|
|
function renderDashboard(clientState) {
|
|
const uptime = Math.floor((Date.now() - startTime) / 1000);
|
|
const h = Math.floor(uptime / 3600);
|
|
const m = Math.floor((uptime % 3600) / 60);
|
|
const s = uptime % 60;
|
|
const uptimeStr = `${h}h ${m}m ${s}s`;
|
|
|
|
const userRows = Object.entries(userStats)
|
|
.sort((a, b) => b[1] - a[1])
|
|
.map(([name, count]) =>
|
|
`<tr><td>${name}</td><td>${count}</td></tr>`
|
|
).join('');
|
|
|
|
const groupRows = Object.entries(groupStats)
|
|
.sort((a, b) => b[1] - a[1])
|
|
.map(([name, count]) =>
|
|
`<tr><td>${name}</td><td>${count}</td></tr>`
|
|
).join('');
|
|
|
|
const connected = clientState === 'CONNECTED';
|
|
|
|
const api = JSON.stringify({
|
|
uptime, uptimeStr, connected,
|
|
totalForwarded, queued: messageQueue.length, flushTime: flushTime(),
|
|
userStats, groupStats,
|
|
});
|
|
|
|
return `<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>OmegaBaSMS</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background: #0f172a; color: #e2e8f0; min-height: 100vh; padding: 2rem;
|
|
}
|
|
h1 {
|
|
font-size: 1.75rem; font-weight: 700; margin-bottom: 0.25rem;
|
|
background: linear-gradient(135deg, #22d3ee, #3b82f6);
|
|
-webkit-background-clip: text; -webkit-text-fill-color: transparent;
|
|
}
|
|
.subtitle { color: #64748b; margin-bottom: 2rem; font-size: 0.9rem; }
|
|
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; margin-bottom: 2rem; }
|
|
.card {
|
|
background: #1e293b; border-radius: 12px; padding: 1.25rem; border: 1px solid #334155;
|
|
}
|
|
.card .label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: #64748b; margin-bottom: 0.5rem; }
|
|
.card .value { font-size: 1.75rem; font-weight: 700; }
|
|
.card .value.green { color: #22c55e; }
|
|
.card .value.blue { color: #3b82f6; }
|
|
.card .value.yellow { color: #eab308; }
|
|
.card .value.red { color: #ef4444; }
|
|
.status-dot {
|
|
display: inline-block; width: 10px; height: 10px; border-radius: 50%;
|
|
margin-right: 0.5rem;
|
|
}
|
|
.status-dot.on { background: #22c55e; box-shadow: 0 0 8px #22c55e88; }
|
|
.status-dot.off { background: #ef4444; box-shadow: 0 0 8px #ef444488; }
|
|
.tables { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
|
|
@media (max-width: 640px) { .tables { grid-template-columns: 1fr; } }
|
|
h2 { font-size: 1rem; font-weight: 600; margin-bottom: 0.75rem; color: #94a3b8; }
|
|
table { width: 100%; border-collapse: collapse; }
|
|
th { text-align: left; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.05em; color: #64748b; padding: 0.5rem 0.75rem; border-bottom: 1px solid #334155; }
|
|
td { padding: 0.5rem 0.75rem; border-bottom: 1px solid #1e293b; font-size: 0.9rem; }
|
|
tr:last-child td { border-bottom: none; }
|
|
td:last-child { text-align: right; font-weight: 600; }
|
|
.bar-bg { background: #334155; border-radius: 4px; height: 6px; overflow: hidden; margin-top: 4px; }
|
|
.bar-fill { height: 100%; border-radius: 4px; background: linear-gradient(90deg, #3b82f6, #22d3ee); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="app">
|
|
<h1>OmegaBaSMS</h1>
|
|
<p class="subtitle">WhatsApp group messages forwarded via SMS</p>
|
|
<div class="grid">
|
|
<div class="card"><div class="label">Status</div><div class="value" id="status">—</div></div>
|
|
<div class="card"><div class="label">Uptime</div><div class="value blue" id="uptime">—</div></div>
|
|
<div class="card"><div class="label">Forwarded</div><div class="value green" id="forwarded">—</div></div>
|
|
<div class="card"><div class="label">Queued</div><div class="value yellow" id="queued">—</div><div style="font-size:0.75rem;color:#64748b;margin-top:0.25rem" id="flushTime"></div></div>
|
|
</div>
|
|
<div class="tables">
|
|
<div><h2>By Sender</h2><table><thead><tr><th>Name</th><th>Messages</th></tr></thead><tbody id="users"></tbody></table></div>
|
|
<div><h2>By Group</h2><table><thead><tr><th>Group</th><th>Messages</th></tr></thead><tbody id="groups"></tbody></table></div>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
async function poll() {
|
|
try {
|
|
const r = await fetch('/api/stats');
|
|
const d = await r.json();
|
|
document.getElementById('status').innerHTML = '<span class="status-dot ' + (d.connected ? 'on' : 'off') + '"></span>' + (d.connected ? 'Connected' : 'Disconnected');
|
|
document.getElementById('uptime').textContent = d.uptimeStr;
|
|
document.getElementById('forwarded').textContent = d.totalForwarded;
|
|
document.getElementById('queued').textContent = d.queued;
|
|
document.getElementById('flushTime').textContent = d.queued > 0 ? 'flushed at ' + d.flushTime : '';
|
|
document.getElementById('users').innerHTML = Object.entries(d.userStats).sort((a,b) => b[1]-a[1]).map(([n,c]) => '<tr><td>' + n + '</td><td>' + c + '</td></tr>').join('') || '<tr><td colspan="2" style="color:#64748b;">No messages yet</td></tr>';
|
|
document.getElementById('groups').innerHTML = Object.entries(d.groupStats).sort((a,b) => b[1]-a[1]).map(([n,c]) => '<tr><td>' + n + '</td><td>' + c + '</td></tr>').join('') || '<tr><td colspan="2" style="color:#64748b;">No messages yet</td></tr>';
|
|
} catch(e) {}
|
|
}
|
|
setInterval(poll, 3000);
|
|
poll();
|
|
</script>
|
|
</body>
|
|
</html>`;
|
|
}
|
|
|
|
async function getClientState() {
|
|
if (!client || restarting) return 'DISCONNECTED';
|
|
try {
|
|
return await client.getState();
|
|
} catch {
|
|
return 'DISCONNECTED';
|
|
}
|
|
}
|
|
|
|
const server = http.createServer(async (req, res) => {
|
|
if (req.url === '/liveness') {
|
|
const state = await getClientState();
|
|
if (state === 'CONNECTED') {
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
res.end('OK');
|
|
} else {
|
|
res.writeHead(503, { 'Content-Type': 'text/plain' });
|
|
res.end('NOT_CONNECTED');
|
|
}
|
|
return;
|
|
}
|
|
if (req.url === '/api/stats') {
|
|
const clientState = await getClientState();
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({
|
|
uptime: Math.floor((Date.now() - startTime) / 1000),
|
|
uptimeStr: (() => {
|
|
const t = Math.floor((Date.now() - startTime) / 1000);
|
|
return Math.floor(t/3600)+'h '+Math.floor((t%3600)/60)+'m '+t%60+'s';
|
|
})(),
|
|
connected: clientState === 'CONNECTED',
|
|
clientState,
|
|
totalForwarded,
|
|
queued: messageQueue.length,
|
|
flushTime: flushTime(),
|
|
userStats,
|
|
groupStats,
|
|
}));
|
|
return;
|
|
}
|
|
const clientState = await getClientState();
|
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
res.end(renderDashboard(clientState));
|
|
});
|
|
const PORT = process.env.PORT || 3000;
|
|
server.listen(PORT, () => log('INIT', `Dashboard on http://0.0.0.0:${PORT}`));
|
|
|
|
/** Recoverable error detection */
|
|
function shouldRestart(err) {
|
|
const msg = (err && err.message) || String(err);
|
|
return msg.includes('detached') ||
|
|
msg.includes('Execution context was destroyed') ||
|
|
msg.includes('Target closed') ||
|
|
msg.includes('Session closed') ||
|
|
msg.includes('Navigation failed') ||
|
|
msg.includes('Protocol error');
|
|
}
|
|
|
|
process.on('uncaughtException', (err) => {
|
|
if (shouldRestart(err)) {
|
|
log('WARN', `Recoverable: ${err.message}. Restarting...`);
|
|
startClient();
|
|
return;
|
|
}
|
|
|
|
log('FATAL', err.message);
|
|
process.exit(1);
|
|
});
|
|
|
|
process.on('unhandledRejection', (err) => {
|
|
if (shouldRestart(err)) {
|
|
log('WARN', `Recoverable: ${err.message}. Restarting...`);
|
|
startClient();
|
|
return;
|
|
}
|
|
|
|
log('FATAL', `Unhandled rejection: ${err.message}`);
|
|
process.exit(1);
|
|
});
|
|
|
|
startClient(); |