From 66ab869f29cf3c5be5500e2f36083982f634d5f7 Mon Sep 17 00:00:00 2001 From: elishadavidi <62501723+elishadavidi@users.noreply.github.com> Date: Thu, 4 Jun 2026 16:20:57 +0300 Subject: [PATCH] Add debug logs --- index.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 594441a..a7b2f83 100644 --- a/index.js +++ b/index.js @@ -78,6 +78,8 @@ async function flushQueue() { if (restarting) return; if (messageQueue.length === 0) return; + log('FLUSH', `Flushing ${messageQueue.length} queued messages`); + flushTimer = null; flushTimerStart = null; @@ -88,27 +90,30 @@ async function flushQueue() { const prodBatch = 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; const text = formatBatch(subset); + log('FLUSH', `Sending ${label} batch (${subset.length} msgs) to ${recipient}`); try { 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) { userStats[m.sender] = (userStats[m.sender] || 0) + 1; groupStats[m.group] = (groupStats[m.group] || 0) + 1; totalForwarded++; } } catch (err) { - log('ERROR', `Flush failed for ${recipient}: ${err.message}`); + log('ERROR', `${label} flush failed for ${recipient}: ${err.message}`); messageQueue = subset.concat(messageQueue); msgCounter = messageQueue.length; } } - await doFlush(prodBatch, config.smsGateway.recipientNumber); + await doFlush(prodBatch, config.smsGateway.recipientNumber, 'prod'); const testRecipient = config.test.smsRecipient || config.smsGateway.recipientNumber; - await doFlush(testBatch, testRecipient); + await doFlush(testBatch, testRecipient, 'test'); 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 }); scheduleFlush(); - log('QUEUE', `Queue #${msgCounter} - Message from ${sender}, flushed at ${flushTime()}`); - - console.log(formatBatch(messageQueue)) + const tag = isTest ? '[TEST]' : '[PROD]'; + log('QUEUE', `Queue #${msgCounter} ${tag} ${sender} → ${group}: ${text.length > 80 ? text.slice(0, 80) + '...' : text}`); if (queueSize() >= config.batch.maxChars) { clearTimeout(flushTimer); @@ -271,6 +275,7 @@ async function startClient() { const isTest = config.test.groupNames.length && config.test.groupNames.includes(chat.name); if (!config.groupNames.includes(chat.name) && !isTest) 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 sender = message.fromMe @@ -545,9 +550,11 @@ const server = http.createServer(async (req, res) => { } 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 matchedGroup = candidates.find((g) => g.toLowerCase() === groupName.toLowerCase()); if (!matchedGroup) { + log('SMS', `Group "${groupName}" not found in ${candidates.join(', ')}`); res.writeHead(404); res.end(`Group "${groupName}" not found`); return; @@ -561,9 +568,10 @@ const server = http.createServer(async (req, res) => { res.end(`Chat "${matchedGroup}" not found on WhatsApp`); 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++; - log('SMS', `Reply sent to "${matchedGroup}" from ${from}: ${replyText}`); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ success: true, group: matchedGroup })); } catch (err) {