Add debug logs
Some checks failed
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
elishadavidi 2026-06-04 16:20:57 +03:00
parent 6d440225ad
commit 66ab869f29

View File

@ -78,6 +78,8 @@ async function flushQueue() {
if (restarting) return; if (restarting) return;
if (messageQueue.length === 0) return; if (messageQueue.length === 0) return;
log('FLUSH', `Flushing ${messageQueue.length} queued messages`);
flushTimer = null; flushTimer = null;
flushTimerStart = null; flushTimerStart = null;
@ -88,27 +90,30 @@ async function flushQueue() {
const prodBatch = batch.filter((m) => !m.isTest); const prodBatch = batch.filter((m) => !m.isTest);
const testBatch = batch.filter((m) => m.isTest); const testBatch = batch.filter((m) => m.isTest);
async function doFlush(subset, recipient) { if (testBatch.length) log('FLUSH', `Split: ${prodBatch.length} prod → ${config.smsGateway.recipientNumber}, ${testBatch.length} test → ${config.test.smsRecipient || config.smsGateway.recipientNumber}`);
async function doFlush(subset, recipient, label) {
if (subset.length === 0) return; if (subset.length === 0) return;
const text = formatBatch(subset); const text = formatBatch(subset);
log('FLUSH', `Sending ${label} batch (${subset.length} msgs) to ${recipient}`);
try { try {
await sendSMS(text, recipient); await sendSMS(text, recipient);
log('INFO', `Flushed ${subset.length} messages to ${recipient}`); log('INFO', `Flushed ${subset.length} ${label} messages to ${recipient}`);
for (const m of subset) { for (const m of subset) {
userStats[m.sender] = (userStats[m.sender] || 0) + 1; userStats[m.sender] = (userStats[m.sender] || 0) + 1;
groupStats[m.group] = (groupStats[m.group] || 0) + 1; groupStats[m.group] = (groupStats[m.group] || 0) + 1;
totalForwarded++; totalForwarded++;
} }
} catch (err) { } catch (err) {
log('ERROR', `Flush failed for ${recipient}: ${err.message}`); log('ERROR', `${label} flush failed for ${recipient}: ${err.message}`);
messageQueue = subset.concat(messageQueue); messageQueue = subset.concat(messageQueue);
msgCounter = messageQueue.length; msgCounter = messageQueue.length;
} }
} }
await doFlush(prodBatch, config.smsGateway.recipientNumber); await doFlush(prodBatch, config.smsGateway.recipientNumber, 'prod');
const testRecipient = config.test.smsRecipient || config.smsGateway.recipientNumber; const testRecipient = config.test.smsRecipient || config.smsGateway.recipientNumber;
await doFlush(testBatch, testRecipient); await doFlush(testBatch, testRecipient, 'test');
if (messageQueue.length > 0) scheduleFlush(); if (messageQueue.length > 0) scheduleFlush();
} }
@ -118,9 +123,8 @@ function enqueue(group, sender, text, showSender = true, isTest = false) {
messageQueue.push({ group, sender, text, showSender, isTest }); messageQueue.push({ group, sender, text, showSender, isTest });
scheduleFlush(); scheduleFlush();
log('QUEUE', `Queue #${msgCounter} - Message from ${sender}, flushed at ${flushTime()}`); const tag = isTest ? '[TEST]' : '[PROD]';
log('QUEUE', `Queue #${msgCounter} ${tag} ${sender}${group}: ${text.length > 80 ? text.slice(0, 80) + '...' : text}`);
console.log(formatBatch(messageQueue))
if (queueSize() >= config.batch.maxChars) { if (queueSize() >= config.batch.maxChars) {
clearTimeout(flushTimer); clearTimeout(flushTimer);
@ -271,6 +275,7 @@ async function startClient() {
const isTest = config.test.groupNames.length && config.test.groupNames.includes(chat.name); const isTest = config.test.groupNames.length && config.test.groupNames.includes(chat.name);
if (!config.groupNames.includes(chat.name) && !isTest) return; if (!config.groupNames.includes(chat.name) && !isTest) return;
if (message.fromMe && !config.includeOwnMessages) return; if (message.fromMe && !config.includeOwnMessages) return;
if (isTest) log('TEST', `Message from test group "${chat.name}" by ${message.fromMe ? config.ownName : '?'}`);
const contact = await message.getContact(); const contact = await message.getContact();
const sender = message.fromMe const sender = message.fromMe
@ -545,9 +550,11 @@ const server = http.createServer(async (req, res) => {
} }
const isTest = config.test.smsFrom && from === config.test.smsFrom; const isTest = config.test.smsFrom && from === config.test.smsFrom;
log('SMS', `Incoming webhook from ${from}${isTest ? ' (test mode)' : ''}: "${msg}"`);
const candidates = isTest && config.test.groupNames.length ? config.test.groupNames : config.groupNames; const candidates = isTest && config.test.groupNames.length ? config.test.groupNames : config.groupNames;
const matchedGroup = candidates.find((g) => g.toLowerCase() === groupName.toLowerCase()); const matchedGroup = candidates.find((g) => g.toLowerCase() === groupName.toLowerCase());
if (!matchedGroup) { if (!matchedGroup) {
log('SMS', `Group "${groupName}" not found in ${candidates.join(', ')}`);
res.writeHead(404); res.writeHead(404);
res.end(`Group "${groupName}" not found`); res.end(`Group "${groupName}" not found`);
return; return;
@ -561,9 +568,10 @@ const server = http.createServer(async (req, res) => {
res.end(`Chat "${matchedGroup}" not found on WhatsApp`); res.end(`Chat "${matchedGroup}" not found on WhatsApp`);
return; return;
} }
await chat.sendMessage(`[${from}]\n ${replyText}`); const smsBody = `[${from}]\n ${replyText}`;
log('SMS', `Sending${isTest ? ' [TEST]' : ''} reply to WhatsApp group "${matchedGroup}": ${smsBody}`);
await chat.sendMessage(smsBody);
smsReplies++; smsReplies++;
log('SMS', `Reply sent to "${matchedGroup}" from ${from}: ${replyText}`);
res.writeHead(200, { 'Content-Type': 'application/json' }); res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, group: matchedGroup })); res.end(JSON.stringify({ success: true, group: matchedGroup }));
} catch (err) { } catch (err) {