Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): preserve API reaction echoes (#1777)
Yudhi Armyndharis2cc3c71ec3e9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8399cacc8..2dad77ccd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Emit Baileys API reaction updates, including reaction removal.- Restore legacy webhook failures and normalize terminal duplicates before SQLite schema synchronization.- Exclude retained webhook replay payloads from database backup reads.- Advance webhook recovery scans past live queued deliveries.diff --git a/package-lock.json b/package-lock.jsonindex 0cfc6029d..56eabbda3 100644--- a/package-lock.json+++ b/package-lock.json@@ -12703,9 +12703,9 @@}},"node_modules/shell-quote": {- "version": "1.10.0",- "resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.10.0.tgz",- "integrity": "sha512-w1aiOKwKuRgtwAReIIj89puqg+I7GvX4IbLrvmhXbzQsj1+Zwi4VO3+fa6ZF91TWSjIxoEkKnMeHcLEODK5ZXA==",+ "version": "1.12.0",+ "resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.12.0.tgz",+ "integrity": "sha512-PcByqNyT/38F2kDNi006HAMRJaULuBzq/FOsw3qdZvX/GA9W/jamDaRskgHjubHiftXK5sIFxLNkvrXUwcof6Q==","dev": true,"license": "MIT","engines": {diff --git a/package.json b/package.jsonindex d08b65f60..7f58d7e48 100644--- a/package.json+++ b/package.json@@ -508,7 +508,7 @@},"overrides": {"@puppeteer/browsers": "3.2.1",- "shell-quote": "^1.10.0",+ "shell-quote": "^1.11.0","minimatch@3": {"brace-expansion": "^1.1.21"},diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex f13c2e4c6..5492f8818 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -830,8 +830,8 @@ export class BaileysMessaging {}/**- * Every message this delegate sends goes through here so its id is recorded before the library- * echoes it back. Baileys re-emits each own send through `messages.upsert` tagged `append`, the+ * Every send goes through here. Content message ids are recorded before the library echoes+ * them back. Baileys re-emits each own send through `messages.upsert` tagged `append`, the* same tag WhatsApp uses to replay what the account typed on its phone while the gateway was* down, and the id is the only thing that tells the two apart (see handleMessagesUpsert). The* record is synchronous on the send's own continuation, ahead of the library's buffered echo.@@ -866,7 +866,8 @@ export class BaileysMessaging {if (this.host.getSocketOrNull() !== sock) throw new EngineNotReadyError();throw error;}- this.host.rememberOwnSend(sent?.key?.id);+ // Reactions are announced by their library echo; no local message.sent callback replaces it.+ if (!('react' in content)) this.host.rememberOwnSend(sent?.key?.id);return sent;}diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 03977b605..e5e4aeb17 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -2607,6 +2607,42 @@ describe('BaileysAdapter inbound fan-out', () => {expect(onMessageCreate).toHaveBeenCalledTimes(1); // the library echo added nothing});+ it.each(['\u{1f44d}', ''])('delivers an API reaction echo with text %p', async reaction => {+ jest+ .requireMock<Record<string, jest.Mock>>('@whiskeysockets/baileys')+ .getContentType.mockImplementation(realGetContentType);+ fakeSock.user = { id: '[email protected]' };+ const target = {+ key: { remoteJid: '[email protected]', fromMe: false, id: 'TARGET' },+ message: { conversation: 'hello' },+ };+ const echo = {+ key: { remoteJid: '[email protected]', fromMe: true, id: 'REACTION_ECHO' },+ message: { reactionMessage: { key: target.key, text: reaction } },+ messageTimestamp: Math.floor(Date.now() / 1000),+ };+ fakeStore.getMessage.mockResolvedValue(target);+ fakeSock.sendMessage.mockResolvedValue(echo);+ const onMessageReaction = jest.fn();+ const onMessageCreate = jest.fn();+ const adapter = newAdapter();+ await adapter.initialize({ onMessageReaction, onMessageCreate });+ fakeSock.fire('connection.update', { connection: 'open' });+ await adapter.reactToMessage('[email protected]', 'TARGET', reaction);+ fakeSock.fire('messages.upsert', { type: 'append', messages: [echo] });+ await new Promise(resolve => setImmediate(resolve));+ expect(onMessageReaction).toHaveBeenCalledTimes(1);+ expect(onMessageReaction).toHaveBeenCalledWith(+ expect.objectContaining({+ messageId: 'TARGET',+ chatId: '[email protected]',+ senderId: '[email protected]',+ reaction,+ }),+ );+ expect(onMessageCreate).not.toHaveBeenCalled();+ });+// The account's own phone kept working while the gateway was down. WhatsApp replays what it sent// in that window through the same 'append' tag as the API echo above, and only the id says it is// not ours. It is an outgoing message the session never saw, so it goes out as onMessageCreate.