Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions scripts/whatsapp-bridge/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ function getArg(name, defaultVal) {
return idx !== -1 && args[idx + 1] ? args[idx + 1] : defaultVal;
}

const WHATSAPP_DEBUG =
typeof process !== 'undefined' &&
process.env &&
typeof process.env.WHATSAPP_DEBUG === 'string' &&
['1', 'true', 'yes', 'on'].includes(process.env.WHATSAPP_DEBUG.toLowerCase());
function parseEnvBool(name, defaultVal = false) {
const raw = process.env[name];
if (raw === undefined || raw === '') return defaultVal;
return ['1', 'true', 'yes', 'on'].includes(raw.toLowerCase());
}

const WHATSAPP_DEBUG = parseEnvBool('WHATSAPP_DEBUG');
const WHATSAPP_MARK_READ = parseEnvBool('WHATSAPP_MARK_READ');

const PORT = parseInt(getArg('port', '3000'), 10);
const SESSION_DIR = getArg('session', path.join(process.env.HOME || '~', '.hermes', 'whatsapp', 'session'));
Expand Down Expand Up @@ -246,10 +249,22 @@ async function startSocket() {
normalizeWhatsAppId(sock.user?.lid),
].filter(Boolean)));

// Collect message keys for batch read-receipt
const messagesToRead = [];

for (const msg of messages) {
if (!msg.message) continue;
// Validate message key before any API calls
if (!msg.key) continue;
// Skip our own messages — no need to mark those as read
if (msg.key.fromMe) continue;

const chatId = msg.key.remoteJid;
if (!chatId || !msg.key.id) continue;
// Queue for batch read-receipt (skip broadcast and groups)
if (!chatId.endsWith('@broadcast') && !chatId.endsWith('@g.us')) {
messagesToRead.push(msg.key);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This queues the receipt before the handler's self-chat and allowlist gates. A foreign DM rejected later by the current bridge policy would still receive a blue tick; collect this key only after the message has passed those admission checks.

}
if (WHATSAPP_DEBUG) {
try {
console.log(JSON.stringify({
Expand Down Expand Up @@ -445,6 +460,21 @@ async function startSocket() {
messageQueue.shift();
}
}

// Batch mark messages as read (blue ticks) — single API call instead of per-message
if (WHATSAPP_MARK_READ && sock && messagesToRead.length > 0) {
try {
await sock.sendPresenceUpdate('available');
} catch (err) {
console.error('[blue-tick] sendPresenceUpdate failed:', err?.message || err);
}
try {
await sock.readMessages(messagesToRead);
console.log(`[blue-tick] Marked ${messagesToRead.length} message(s) as read`);
} catch (err) {
console.error('[blue-tick] batch readMessages failed:', err?.message || err);
}
}
});
}

Expand Down