Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): forward the original deleted message id as revokedId on message.revoked (#567)
Jibay Mcs114b98f2c880
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex def771191..21f94ee48 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed- **Incoming WhatsApp Business interactive messages no longer arrive with an empty body on the Baileys engine.** Messages sent as interactive/button/template shapes — which businesses use for one-time codes and verification prompts — were saved with `type: "unknown"` and a blank body, dropping the text (e.g. an OTP) entirely. The engine now extracts the display text from `interactiveMessage`, `buttonsMessage`, `templateMessage`, and `interactiveResponseMessage` into the message body and classifies them as `text`, so the content is retrievable over the standard messages API and rendered in the dashboard. (#562)+- **Deleting a message "for everyone" now reliably flags it as revoked, and `message.revoked` carries the original message id.** On the whatsapp-web.js engine the revoke event's `id` is the _revocation notification_ — a distinct message whose id never matched the stored row — so the stored message was silently never marked revoked, and webhook/WebSocket consumers had no id to reconcile against. The `message.revoked` payload now includes an optional `revokedId` (the original deleted message's id) that both engines populate; OpenWA flags the stored message on `revokedId` (falling back to `id`), and consumers should match the same way. Purely additive and backward-compatible — on Baileys `id` and `revokedId` coincide. (#567) Thanks @JibayMcs.## [0.7.17] - 2026-07-01diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 297557b06..5971087aa 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -4519,7 +4519,7 @@ These are the events OpenWA actually emits. A webhook is registered with an `eve| `message.sent` | An outbound message is created/sent from this session | Same message object shape as `message.received` || `message.ack` | A delivery/read receipt updates an outbound message | `{ id, messageId, status, ack }` — `status` is the canonical state (`pending`/`sent`/`delivered`/`read`/`failed`); `ack` is the deprecated legacy integer derived from it || `message.failed` | A receipt resolves to `failed` (dispatched in addition to `message.ack`) | `{ id, messageId, status: "failed", ack: -1 }` |-| `message.revoked` | A message is deleted/recalled | The engine's revoked-message object (e.g. `{ id, … }`) |+| `message.revoked` | A message is deleted/recalled | `{ id, revokedId?, chatId, from, to, type: "revoked", body: "", timestamp }` — **reconcile on `revokedId`** (the original deleted message's id), falling back to `id`. On whatsapp-web.js `id` is the *revocation notification* (a distinct message that won't match a stored id) and `revokedId` may be absent when the original isn't cached locally; on Baileys the two coincide || `message.reaction` | A reaction is added, changed, or removed | `{ messageId, chatId, reaction, senderId, reactions }` — `reactions` is the post-apply `{ senderId: emoji }` snapshot; `reaction` is empty when removed || `session.qr` | A new pairing QR is generated | `{ sessionId, qr }` (raw QR string) || `session.authenticated` | The session pairs and becomes ready | `{ sessionId, phone, pushName }` |diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 2b026eb65..f277e4593 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -1238,11 +1238,14 @@ describe('BaileysAdapter inbound fan-out', () => {// eslint-disable-next-line @typescript-eslint/no-unsafe-member-accessconst revoked = onMessageRevoked.mock.calls[0][0] as {id: string;+ revokedId?: string;chatId: string;type: string;body: string;};expect(revoked.id).toBe('ORIGINAL_ID');+ // The REVOKE protocolMessage key IS the original, so revokedId mirrors id here.+ expect(revoked.revokedId).toBe('ORIGINAL_ID');expect(revoked.chatId).toBe('[email protected]'); // canonicalized to the neutral dialectexpect(revoked.type).toBe('revoked');expect(revoked.body).toBe('');diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.tsindex 11d1ec418..a7ac775f0 100644--- a/src/engine/adapters/baileys.adapter.ts+++ b/src/engine/adapters/baileys.adapter.ts@@ -947,6 +947,10 @@ export class BaileysAdapter implements IWhatsAppEngine {const to = msg.key.fromMe === true ? remoteJid : this.normalizedSelfJid();const revoked: RevokedMessage = {id: pm.key?.id ?? '',+ // The REVOKE protocolMessage's key points at the ORIGINAL deleted message,+ // so `id` already IS the original here. Mirror it into `revokedId` so that+ // field is the reliable cross-engine handle (wwebjs sets it separately).+ revokedId: pm.key?.id ?? undefined,chatId: this.sessionStore.toNeutralJid(remoteJid),from: this.sessionStore.toNeutralJid(from),to: this.sessionStore.toNeutralJid(to),diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex da7f9eb06..b7ecab679 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -1122,6 +1122,67 @@ describe('WhatsAppWebJsAdapter inbound media (MEDIA_DOWNLOAD_ENABLED=false)', ()});});+describe('WhatsAppWebJsAdapter message_revoke_everyone (forwards the original deleted id as revokedId)', () => {+ const wireRevokeHandler = (): { onMessageRevoked: jest.Mock; client: EventEmitter } => {+ const adapter = new WhatsAppWebJsAdapter({+ sessionId: 'sess-revoke-test',+ sessionDataPath: './data/sessions',+ puppeteer: {},+ });+ const client = Object.assign(new EventEmitter(), {+ info: { wid: { _serialized: '[email protected]', user: '628123' }, pushname: 'Tester' },+ getState: jest.fn().mockResolvedValue(WAState.CONNECTED),+ pupPage: { evaluate: jest.fn().mockResolvedValue(true) },+ });+ (adapter as unknown as { client: unknown }).client = client;+ const onMessageRevoked = jest.fn();+ (adapter as unknown as { callbacks: unknown }).callbacks = { onMessageRevoked };+ (adapter as unknown as { setupEventHandlers: () => void }).setupEventHandlers();+ return { onMessageRevoked, client };+ };++ it('emits revokedId from `before` (the original) distinct from `id` (the revocation notification)', () => {+ const { onMessageRevoked, client } = wireRevokeHandler();++ client.emit(+ 'message_revoke_everyone',+ { id: { _serialized: 'REVOKE_NOTIF' }, from: '[email protected]', to: '[email protected]', timestamp: 1700000070 },+ { id: { _serialized: 'ORIGINAL_MSG' } },+ );++ expect(onMessageRevoked).toHaveBeenCalledTimes(1);+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access+ const revoked = onMessageRevoked.mock.calls[0][0] as {+ id: string;+ revokedId?: string;+ chatId: string;+ type: string;+ body: string;+ };+ expect(revoked.id).toBe('REVOKE_NOTIF');+ expect(revoked.revokedId).toBe('ORIGINAL_MSG');+ expect(revoked.chatId).toBe('[email protected]'); // incoming: chatId is the peer, not self+ expect(revoked.type).toBe('revoked');+ expect(revoked.body).toBe('');+ });++ it('leaves revokedId undefined when whatsapp-web.js has no `before` (original not in local store)', () => {+ const { onMessageRevoked, client } = wireRevokeHandler();++ client.emit(+ 'message_revoke_everyone',+ { id: { _serialized: 'REVOKE_NOTIF_2' }, from: '[email protected]', to: '[email protected]', timestamp: 1700000071 },+ undefined,+ );++ expect(onMessageRevoked).toHaveBeenCalledTimes(1);+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access+ const revoked = onMessageRevoked.mock.calls[0][0] as { id: string; revokedId?: string };+ expect(revoked.id).toBe('REVOKE_NOTIF_2');+ expect(revoked.revokedId).toBeUndefined();+ });+});+describe('outbound mentions (#530)', () => {const ready = (client: unknown): WhatsAppWebJsAdapter => {const adapter = new WhatsAppWebJsAdapter({ sessionId: 's', sessionDataPath: './data/sessions', puppeteer: {} });diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.tsindex 0bf80ec49..b3e1cda4e 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts@@ -493,13 +493,19 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEnginthis.callbacks.onMessageAck?.(msg.id._serialized, wwebjsAckToDeliveryStatus(ack));});- this.client.on('message_revoke_everyone', after => {+ this.client.on('message_revoke_everyone', (after, before) => {try {const selfWid = this.client?.info?.wid?._serialized;// Emit structured data only; the engine layer never produces a localized// display string. The dashboard renders the localized "message deleted" text.+ //+ // `after` is the revocation notification (its own id); `before` is the+ // ORIGINAL deleted message (when whatsapp-web.js has it in the local store).+ // We forward `before.id` as `revokedId` so consumers can reconcile the+ // deleted message in their own storage.const payload: RevokedMessage = {id: after.id._serialized,+ revokedId: before?.id?._serialized,chatId: after.from === selfWid ? after.to : after.from,from: after.from,to: after.to,diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.tsindex 1e39b8eaa..1d19c438b 100644--- a/src/engine/interfaces/whatsapp-engine.interface.ts+++ b/src/engine/interfaces/whatsapp-engine.interface.ts@@ -346,6 +346,20 @@ export type DeliveryStatus = 'pending' | 'sent' | 'delivered' | 'read' | 'failed*/export interface RevokedMessage {id: string;+ /**+ * Serialized id of the ORIGINAL message that was deleted (when available).+ *+ * This is the reliable cross-engine field for reconciling the deleted message in+ * your own storage — both adapters populate it with the original message id:+ * - whatsapp-web.js: `id` is the revocation NOTIFICATION (a distinct message), so+ * `id !== revokedId`. `revokedId` may be undefined when the original is not in+ * the local store.+ * - Baileys: the revoke arrives as a protocolMessage whose key already points at+ * the original, so `id === revokedId`.+ *+ * Consumers should match on `revokedId` (falling back to `id`) rather than `id`.+ */+ revokedId?: string;chatId: string;from: string;to: string;diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex c48de3283..ac13bffdc 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -1390,6 +1390,59 @@ describe('SessionService', () => {expect(eventsGateway.emitMessageRevoked as jest.Mock).toHaveBeenCalledWith('sess-uuid-1', expect.anything());});+ it('flags the DB row by revokedId (the original), not the revocation notification id', async () => {+ const callbacks = await startAndCaptureCallbacks();++ // wwebjs shape: `id` is the revocation notification, `revokedId` the original message.+ callbacks.onMessageRevoked!({+ id: 'REVOKE_NOTIF',+ revokedId: 'ORIGINAL_MSG',+ chatId: '[email protected]',+ from: '[email protected]',+ to: '[email protected]',+ type: 'revoked',+ body: '',+ timestamp: 1706868000,+ });+ await flush();++ expect(messageRepository.update as jest.Mock).toHaveBeenCalledWith(+ { sessionId: 'sess-uuid-1', waMessageId: 'ORIGINAL_MSG' },+ { body: '', type: 'revoked' },+ );++ // The DB flag is an internal side effect; the delivered payload is the public contract+ // this fix exists for. Webhook and WS consumers must receive `revokedId` (the original),+ // not just the revocation-notification `id`, so they can reconcile the deleted message.+ expect(dispatchedEvents('message.revoked')[0][2]).toEqual(+ expect.objectContaining({ id: 'REVOKE_NOTIF', revokedId: 'ORIGINAL_MSG' }),+ );+ expect(eventsGateway.emitMessageRevoked as jest.Mock).toHaveBeenCalledWith(+ 'sess-uuid-1',+ expect.objectContaining({ id: 'REVOKE_NOTIF', revokedId: 'ORIGINAL_MSG' }),+ );+ });++ it('falls back to `id` for the DB flag when revokedId is absent (Baileys shape)', async () => {+ const callbacks = await startAndCaptureCallbacks();++ callbacks.onMessageRevoked!({+ id: 'ORIGINAL_MSG',+ chatId: '[email protected]',+ from: '[email protected]',+ to: '[email protected]',+ type: 'revoked',+ body: '',+ timestamp: 1706868000,+ });+ await flush();++ expect(messageRepository.update as jest.Mock).toHaveBeenCalledWith(+ { sessionId: 'sess-uuid-1', waMessageId: 'ORIGINAL_MSG' },+ { body: '', type: 'revoked' },+ );+ });+// ── session lifecycle events ──────────────────────────────────────it('dispatches session.qr with the QR payload when the engine emits a QR code', async () => {diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex f93aef55a..000de75e0 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -894,10 +894,15 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat// Flag the stored message as revoked (best-effort; the message may not be in the// DB). The dashboard renders the localized "message deleted" text, so no display// string is persisted here.+ //+ // Match on `revokedId` (the ORIGINAL deleted message's id) when present: on wwebjs+ // `message.id` is the revocation notification, which never matches a stored row.+ // `revokedId` falls back to `id` (Baileys, where the two are the same).+ const revokedWaMessageId = message.revokedId ?? message.id;void this.messageRepository- .update({ sessionId: id, waMessageId: message.id }, { body: '', type: 'revoked' })+ .update({ sessionId: id, waMessageId: revokedWaMessageId }, { body: '', type: 'revoked' }).catch(err => {- this.logger.error(`Failed to update revoked message: ${message.id}`, String(err));+ this.logger.error(`Failed to update revoked message: ${revokedWaMessageId}`, String(err));});// Notify consumers regardless of whether the row existed: webhook (message.revoked