Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(webhook): dispatch message.reaction events (WebSocket/webhook parity) (#380)
Yudhi Armyndharisc7759e808ff0
diff --git a/dashboard/src/pages/Webhooks.tsx b/dashboard/src/pages/Webhooks.tsxindex b5e5016b4..593103aa9 100644--- a/dashboard/src/pages/Webhooks.tsx+++ b/dashboard/src/pages/Webhooks.tsx@@ -34,6 +34,7 @@ const availableEventNames = ['message.ack','message.failed','message.revoked',+ 'message.reaction','session.status','session.qr','session.authenticated',diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 4a29f3432..d2f3aa87b 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -1324,6 +1324,7 @@ Examples:- message.sent: msg_{sessionId}_{messageId}- message.ack: ack_{sessionId}_{messageId}_{status}- message.failed: failed_{sessionId}_{messageId}_{status}+- message.reaction: react_{sessionId}_{messageId}_{senderId}- session.status: sess_{sessionId}_{status}- group.join: grp_{groupId}_{participantId}_join```@@ -1663,6 +1664,7 @@ ws.send(JSON.stringify({| `message.sent` | Message sent successfully || `message.ack` | Message acknowledgment (delivered, read) || `message.revoked` | Message was deleted |+| `message.reaction` | A reaction was added, changed, or removed on a message || `group.join` | Group join event || `group.leave` | Group leave event || `group.update` | Group info updated |diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 20e6a089f..371e2d2aa 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -810,6 +810,32 @@ describe('SessionService', () => {expect(chains.size).toBe(0);});+ it('dispatches message.reaction to the webhook with the post-apply reactions snapshot', async () => {+ const callbacks = await startAndCaptureCallbacks();+ type Row = { metadata?: Record<string, unknown> };+ const clone = (r: Row): Row => JSON.parse(JSON.stringify(r)) as Row;+ let stored: Row = { metadata: {} };+ (messageRepository.findOne as jest.Mock).mockImplementation(() => Promise.resolve(clone(stored)));+ (messageRepository.save as jest.Mock).mockImplementation((m: Row) => {+ stored = clone(m);+ return Promise.resolve(m);+ });++ callbacks.onMessageReaction!({ messageId: 'wa-1', chatId: 'c', senderId: 'alice', reaction: 'π' });+ for (let i = 0; i < 3; i++) await flush();++ const dispatched = dispatchedEvents('message.reaction');+ expect(dispatched).toHaveLength(1);+ // Webhook payload mirrors the WS payload: the event plus the post-apply reactions snapshot.+ expect(dispatched[0][2]).toMatchObject({+ messageId: 'wa-1',+ chatId: 'c',+ senderId: 'alice',+ reaction: 'π',+ reactions: { alice: 'π' },+ });+ });+it('dispatches message.received (not message.sent) on an incoming message event', async () => {const callbacks = await startAndCaptureCallbacks();diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.tsindex c06c795b5..c803874f4 100644--- a/src/modules/session/session.service.ts+++ b/src/modules/session/session.service.ts@@ -812,6 +812,9 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicatawait this.messageRepository.save(msg);this.eventsGateway.emitMessageReaction(id, { ...event, reactions });+ // Webhook parity with the WebSocket broadcast: same payload (event + post-apply snapshot), so a+ // webhook-only consumer observes reactions too. Idempotency for this event is salted per dispatch.+ void this.webhookService.dispatch(id, 'message.reaction', { ...event, reactions });} catch (err) {this.logger.error(`Failed to update message reaction: ${event.messageId}`, String(err));}diff --git a/src/modules/webhook/dto/webhook.dto.ts b/src/modules/webhook/dto/webhook.dto.tsindex 94d7229e4..2f8c60991 100644--- a/src/modules/webhook/dto/webhook.dto.ts+++ b/src/modules/webhook/dto/webhook.dto.ts@@ -22,6 +22,7 @@ export const WEBHOOK_EVENTS = ['message.ack','message.failed','message.revoked',+ 'message.reaction','session.status','session.qr','session.authenticated',diff --git a/src/modules/webhook/utils/idempotency.util.spec.ts b/src/modules/webhook/utils/idempotency.util.spec.tsindex 062b9f419..8cd07bcfe 100644--- a/src/modules/webhook/utils/idempotency.util.spec.ts+++ b/src/modules/webhook/utils/idempotency.util.spec.ts@@ -111,6 +111,37 @@ describe('Idempotency Utils', () => {expect(a).toBe('ack_A_X_read');});+ it('salts message.reaction keys so a re-reaction (same sender/emoji, later time) is a distinct event', () => {+ // A reaction has no unique id and is a read-modify-write: the same sender can go π β remove β π.+ // Keying on (sender, message, emoji) alone would collapse the re-reaction onto the earlier one.+ const a = generateIdempotencyKey(+ 'message.reaction',+ { sessionId: 'A', messageId: 'MSG1', senderId: '[email protected]', reaction: 'π' },+ '2026-06-20T00:00:00.000Z',+ );+ const b = generateIdempotencyKey(+ 'message.reaction',+ { sessionId: 'A', messageId: 'MSG1', senderId: '[email protected]', reaction: 'π' },+ '2026-06-20T00:05:00.000Z',+ );+ expect(a).not.toBe(b);+ });++ it('is retry-stable for message.reaction: the same occurrence regenerates the same key', () => {+ const at = '2026-06-20T00:00:00.000Z';+ const data = { sessionId: 'A', messageId: 'MSG1', senderId: '[email protected]', reaction: 'π' };+ expect(generateIdempotencyKey('message.reaction', data, at)).toBe(+ generateIdempotencyKey('message.reaction', data, at),+ );+ });++ it('gives two senders reacting to the same message DISTINCT message.reaction keys', () => {+ const at = '2026-06-20T00:00:00.000Z';+ const a = generateIdempotencyKey('message.reaction', { sessionId: 'A', messageId: 'M', senderId: 'S1' }, at);+ const b = generateIdempotencyKey('message.reaction', { sessionId: 'A', messageId: 'M', senderId: 'S2' }, at);+ expect(a).not.toBe(b);+ });+it('should generate key for group.join', () => {const key = generateIdempotencyKey('group.join', {groupId: 'grp_1',diff --git a/src/modules/webhook/utils/idempotency.util.ts b/src/modules/webhook/utils/idempotency.util.tsindex 991404807..07ca10aaa 100644--- a/src/modules/webhook/utils/idempotency.util.ts+++ b/src/modules/webhook/utils/idempotency.util.ts@@ -56,6 +56,14 @@ export function generateIdempotencyKey(event: string, data: Record<string, unknocase 'message.revoked':return `rev_${toStr(data.sessionId)}_${toStr(data.id ?? data.messageId)}`;+ case 'message.reaction':+ // A reaction carries no unique id and is a read-modify-write of the message's reactions map; the+ // same sender can re-apply the same emoji over time (π β remove β π). Keying on+ // (sender, target message) alone would collapse a genuine re-reaction onto the earlier one, so salt+ // with occurredAt (captured once per dispatch, reused across retries): distinct occurrences get+ // distinct keys while retries of the same delivery stay stable.+ return `react_${toStr(data.sessionId)}_${toStr(data.messageId)}_${toStr(data.senderId)}${occurrence}`;+case 'session.status':// Salted so repeated transitions to the same status (e.g. across disconnect/reconnect cycles)// stay distinct instead of collapsing onto one key.