fix(whatsapp): respond to linked-account group messages

This commit is contained in:
xmanrui 2026-08-25 01:12:55 +08:00
parent 9657107a21
commit 2cc3a42d45
11 changed files with 150 additions and 34 deletions

View file

@ -1,4 +1,4 @@
import { createHash } from 'node:crypto';
import { createHash, randomBytes } from 'node:crypto';
import {
areJidsSameUser,
@ -217,9 +217,9 @@ export function normalizeWhatsappMessage(message, accountJid, {
const fromMe = message.key.fromMe === true;
const selfChat = fromMe && !group
&& [remoteJid, alternateRemoteJid].some((jid) => jid && areJidsSameUser(jid, accountJid));
if (fromMe && !selfChat) return null;
const senderJid = selfChat ? accountJid : group ? message.key.participant : remoteJid;
const senderAlternateJid = group ? message.key.participantAlt : alternateRemoteJid;
if (fromMe && !selfChat && !group) return null;
const senderJid = fromMe ? accountJid : group ? message.key.participant : remoteJid;
const senderAlternateJid = group && !fromMe ? message.key.participantAlt : alternateRemoteJid;
if (typeof senderJid !== 'string' || !senderJid) return null;
const viewOnce = hasViewOnceWrapper(message.message);
const content = normalizeMessageContent(message.message);
@ -241,7 +241,7 @@ export function normalizeWhatsappMessage(message, accountJid, {
content: messageText(content),
images: image ? [image] : [],
files: file ? [file] : [],
addressed: !group || mentioned || replyToSelf,
addressed: !group || fromMe || mentioned || replyToSelf,
selfChat,
replyTarget: { jid: remoteJid, quoted: message, selfChat },
};
@ -272,6 +272,14 @@ class RecentWhatsappOutboundIds {
}
remember(id) {
this.#store(id);
}
reserve(id) {
this.#store(id);
}
#store(id) {
if (typeof id !== 'string' || !id) return;
this.#purge();
this.#ids.set(id, Date.now() + 5 * 60_000);
@ -360,10 +368,23 @@ export class WhatsappBotClient {
await this.#stopTyping(target.jid);
const providerMessageIds = [];
for (const [index, chunk] of splitMessageText(text, 4_000).entries()) {
const messageId = randomBytes(10).toString('hex').toUpperCase();
const options = {
...(index === 0 && target.quoted ? { quoted: target.quoted } : {}),
messageId,
};
// Linked-account group messages are valid inbound prompts in open mode.
// Reserve our own id before dispatch so an early local echo cannot loop
// back through the bridge as another owner-authored group prompt.
if (typeof this.#outboundIds.reserve === 'function') {
this.#outboundIds.reserve(messageId);
} else {
this.#outboundIds.remember(messageId);
}
const result = await this.#socket.sendMessage(
target.jid,
{ text: chunk },
index === 0 && target.quoted ? { quoted: target.quoted } : undefined,
options,
);
this.#outboundIds.remember(result?.key?.id);
if (typeof result?.key?.id === 'string' && result.key.id) {