Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(webhook): filter and gate on chat kind, so a channel can be excluded
Yudhi Armyndharis7948187fcf92
diff --git a/dashboard/src/components/FilterBuilder.tsx b/dashboard/src/components/FilterBuilder.tsxindex 9c982f1b7..2a7508574 100644--- a/dashboard/src/components/FilterBuilder.tsx+++ b/dashboard/src/components/FilterBuilder.tsx@@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next';import { Plus, X } from 'lucide-react';import {MESSAGE_TYPES,+ CHAT_KINDS,type Chat,type WebhookFilters,type WebhookFilterCondition,@@ -26,6 +27,7 @@ const MESSAGE_FIELDS: FieldDescriptor[] = [{ field: 'body', kind: 'text', operators: ['contains', 'equals'] },{ field: 'type', kind: 'enum', operators: ['is', 'isNot'], enumValues: MESSAGE_TYPES },{ field: 'isGroup', kind: 'boolean', operators: ['is'] },+ { field: 'kind', kind: 'enum', operators: ['is', 'isNot'], enumValues: CHAT_KINDS },{ field: 'fromMe', kind: 'boolean', operators: ['is'] },{ field: 'hasMedia', kind: 'boolean', operators: ['is'] },{ field: 'mentions', kind: 'idArray', operators: ['is', 'isNot'] },diff --git a/dashboard/src/i18n/locales/ar.json b/dashboard/src/i18n/locales/ar.jsonindex 37146d5c1..93bdecc7a 100644--- a/dashboard/src/i18n/locales/ar.json+++ b/dashboard/src/i18n/locales/ar.json@@ -448,6 +448,7 @@"body": "نص الرسالة","type": "نوع الرسالة","isGroup": "محادثة جماعية",+ "kind": "Chat kind","fromMe": "مُرسَلة مني","hasMedia": "تحتوي على وسائط","mentions": "الإشارات"diff --git a/dashboard/src/i18n/locales/de.json b/dashboard/src/i18n/locales/de.jsonindex e0080d7e0..4176d3eb5 100644--- a/dashboard/src/i18n/locales/de.json+++ b/dashboard/src/i18n/locales/de.json@@ -433,6 +433,7 @@"body": "Nachrichteninhalt","type": "Nachrichtentyp","isGroup": "Ist Gruppenchat",+ "kind": "Chat kind","fromMe": "Von mir gesendet","hasMedia": "Enthält Medien","mentions": "Erwähnungen"diff --git a/dashboard/src/i18n/locales/en.json b/dashboard/src/i18n/locales/en.jsonindex fbea95d27..8a7ef1deb 100644--- a/dashboard/src/i18n/locales/en.json+++ b/dashboard/src/i18n/locales/en.json@@ -433,6 +433,7 @@"body": "Message body","type": "Message type","isGroup": "Is group chat",+ "kind": "Chat kind","fromMe": "Sent by me","hasMedia": "Has media","mentions": "Mentions"diff --git a/dashboard/src/i18n/locales/es.json b/dashboard/src/i18n/locales/es.jsonindex 49b35d800..31ce111cb 100644--- a/dashboard/src/i18n/locales/es.json+++ b/dashboard/src/i18n/locales/es.json@@ -433,6 +433,7 @@"body": "Cuerpo del mensaje","type": "Tipo de mensaje","isGroup": "Es chat de grupo",+ "kind": "Chat kind","fromMe": "Enviado por mí","hasMedia": "Tiene multimedia","mentions": "Menciones"diff --git a/dashboard/src/i18n/locales/fr.json b/dashboard/src/i18n/locales/fr.jsonindex 5f141f923..5d0a47224 100644--- a/dashboard/src/i18n/locales/fr.json+++ b/dashboard/src/i18n/locales/fr.json@@ -433,6 +433,7 @@"body": "Corps du message","type": "Type de message","isGroup": "Est une discussion de groupe",+ "kind": "Chat kind","fromMe": "Envoyé par moi","hasMedia": "Contient un média","mentions": "Mentions"diff --git a/dashboard/src/i18n/locales/he.json b/dashboard/src/i18n/locales/he.jsonindex f2efa3e16..ab934fdce 100644--- a/dashboard/src/i18n/locales/he.json+++ b/dashboard/src/i18n/locales/he.json@@ -442,6 +442,7 @@"body": "גוף ההודעה","type": "סוג הודעה","isGroup": "צ'אט קבוצתי",+ "kind": "Chat kind","fromMe": "נשלח על ידי","hasMedia": "מכיל מדיה","mentions": "אזכורים"diff --git a/dashboard/src/i18n/locales/it.json b/dashboard/src/i18n/locales/it.jsonindex dde77cc69..4e8147142 100644--- a/dashboard/src/i18n/locales/it.json+++ b/dashboard/src/i18n/locales/it.json@@ -433,6 +433,7 @@"body": "Corpo del messaggio","type": "Tipo di messaggio","isGroup": "È una chat di gruppo",+ "kind": "Chat kind","fromMe": "Inviato da me","hasMedia": "Contiene media","mentions": "Menzioni"diff --git a/dashboard/src/i18n/locales/ko.json b/dashboard/src/i18n/locales/ko.jsonindex 97b8856cf..019c9b828 100644--- a/dashboard/src/i18n/locales/ko.json+++ b/dashboard/src/i18n/locales/ko.json@@ -433,6 +433,7 @@"body": "메시지 본문","type": "메시지 유형","isGroup": "그룹 채팅 여부",+ "kind": "Chat kind","fromMe": "내가 보낸 메시지","hasMedia": "미디어 포함","mentions": "멘션"diff --git a/dashboard/src/i18n/locales/pt-BR.json b/dashboard/src/i18n/locales/pt-BR.jsonindex 808345d65..5d57d0c24 100644--- a/dashboard/src/i18n/locales/pt-BR.json+++ b/dashboard/src/i18n/locales/pt-BR.json@@ -433,6 +433,7 @@"body": "Corpo da mensagem","type": "Tipo de mensagem","isGroup": "É chat de grupo",+ "kind": "Chat kind","fromMe": "Enviado por mim","hasMedia": "Tem mídia","mentions": "Menções"diff --git a/dashboard/src/i18n/locales/te.json b/dashboard/src/i18n/locales/te.jsonindex bd75eae3a..6cecb7d6d 100644--- a/dashboard/src/i18n/locales/te.json+++ b/dashboard/src/i18n/locales/te.json@@ -433,6 +433,7 @@"body": "సందేశ విషయం","type": "సందేశ రకం","isGroup": "గ్రూప్ చాట్",+ "kind": "Chat kind","fromMe": "నేను పంపినది","hasMedia": "మీడియా ఉంది","mentions": "ప్రస్తావనలు"diff --git a/dashboard/src/i18n/locales/tr.json b/dashboard/src/i18n/locales/tr.jsonindex bb46b1546..665d8adc1 100644--- a/dashboard/src/i18n/locales/tr.json+++ b/dashboard/src/i18n/locales/tr.json@@ -433,6 +433,7 @@"body": "Mesaj içeriği","type": "Mesaj türü","isGroup": "Grup sohbeti mi",+ "kind": "Chat kind","fromMe": "Benim tarafımdan gönderildi","hasMedia": "Medya içeriyor","mentions": "Bahsetmeler"diff --git a/dashboard/src/i18n/locales/zh-CN.json b/dashboard/src/i18n/locales/zh-CN.jsonindex 55bdc3d05..6be92b41f 100644--- a/dashboard/src/i18n/locales/zh-CN.json+++ b/dashboard/src/i18n/locales/zh-CN.json@@ -433,6 +433,7 @@"body": "消息正文","type": "消息类型","isGroup": "是群聊",+ "kind": "Chat kind","fromMe": "由我发送","hasMedia": "包含媒体","mentions": "提及"diff --git a/dashboard/src/i18n/locales/zh-HK.json b/dashboard/src/i18n/locales/zh-HK.jsonindex e4d8ceeda..3f5e34d8e 100644--- a/dashboard/src/i18n/locales/zh-HK.json+++ b/dashboard/src/i18n/locales/zh-HK.json@@ -433,6 +433,7 @@"body": "訊息內容","type": "訊息類型","isGroup": "是群組聊天",+ "kind": "Chat kind","fromMe": "由我傳送","hasMedia": "包含媒體","mentions": "提及"diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.tsindex 9b4c3efad..15602ce1c 100644--- a/dashboard/src/services/api.ts+++ b/dashboard/src/services/api.ts@@ -218,6 +218,8 @@ export interface MessageResponse {// Mirrors the backend engine ChatKind (dashboard cannot import wa-id.ts).export type ChatKind = 'individual' | 'group' | 'channel' | 'status' | 'broadcast' | 'unknown';+// Mirrors CHAT_KINDS in the backend webhook filter registry (src/modules/webhook/filters/filter-types.ts).+export const CHAT_KINDS: readonly ChatKind[] = ['individual', 'group', 'channel', 'status', 'broadcast', 'unknown'];// Chat summary returned by GET /sessions/:id/chats (mirrors the backend ChatSummary).export interface Chat {diff --git a/src/modules/webhook/filters/filter-evaluator.spec.ts b/src/modules/webhook/filters/filter-evaluator.spec.tsindex 28f3b627d..c86dc990f 100644--- a/src/modules/webhook/filters/filter-evaluator.spec.ts+++ b/src/modules/webhook/filters/filter-evaluator.spec.ts@@ -194,4 +194,27 @@ describe('evaluateFilters', () => {expect(evaluateFilters(f, 'message.received', msg({ mentionedIds: ['111@lid', '[email protected]'] }), resolve)).toBe(true);});});++ describe('kind (chat kind, so a channel can be singled out where isGroup cannot)', () => {+ // The reporter's case: a webhook subscribed to message.received also gets channel posts, and+ // isGroup=false cannot tell a channel from a 1:1 chat. `kind` rides the received payload.+ it('excludes channel traffic with isNot, and matches it with is', () => {+ const exclude = filters({ field: 'kind', operator: 'isNot', value: ['channel'] });+ expect(evaluateFilters(exclude, 'message.received', msg({ kind: 'channel' }))).toBe(false);+ expect(evaluateFilters(exclude, 'message.received', msg({ kind: 'individual' }))).toBe(true);++ const only = filters({ field: 'kind', operator: 'is', value: ['channel'] });+ expect(evaluateFilters(only, 'message.received', msg({ kind: 'channel' }))).toBe(true);+ expect(evaluateFilters(only, 'message.received', msg({ kind: 'group' }))).toBe(false);+ });++ // The edited/reaction/revoked events in the family carry chatId but no kind, so it is derived.+ it('derives the kind from chatId when the payload omits it (edited/reaction/revoked)', () => {+ const f = filters({ field: 'kind', operator: 'is', value: ['channel'] });+ const revoked = { chatId: '120363000000000000@newsletter' } as Record<string, unknown>;+ expect(evaluateFilters(f, 'message.revoked', revoked)).toBe(true);+ const dm = { chatId: '[email protected]' } as Record<string, unknown>;+ expect(evaluateFilters(f, 'message.revoked', dm)).toBe(false);+ });+ });});diff --git a/src/modules/webhook/filters/filter-types.ts b/src/modules/webhook/filters/filter-types.tsindex 02c7f0edd..6ee1b36ab 100644--- a/src/modules/webhook/filters/filter-types.ts+++ b/src/modules/webhook/filters/filter-types.ts@@ -10,6 +10,7 @@*/import { MessageType } from '../../../engine/interfaces/whatsapp-engine.interface';+import { chatKind, type ChatKind } from '../../../engine/identity/wa-id';export type FilterOperator = 'is' | 'isNot' | 'contains' | 'equals';@@ -62,6 +63,9 @@ const MESSAGE_TYPE_FLAGS: Record<MessageType, true> = {export const MESSAGE_TYPES: readonly MessageType[] = Object.keys(MESSAGE_TYPE_FLAGS) as MessageType[];+/** Chat kinds a message-family filter can match on. `channel` is a newsletter; see chatKind(). */+export const CHAT_KINDS: readonly ChatKind[] = ['individual', 'group', 'channel', 'status', 'broadcast', 'unknown'];+// Guard rails. These bound both stored config size and per-event evaluation cost.export const MAX_CONDITIONS = 20;export const MAX_VALUES_PER_CONDITION = 100;@@ -112,6 +116,22 @@ export const FILTER_FIELDS: Record<string, FieldDefinition[]> = {operators: BOOLEAN_OPERATORS,resolve: data => data.isGroup === true,},+ {+ // The chat kind, so a filter can single out or exclude a channel (newsletter) where the+ // boolean isGroup cannot: individual, group, channel, status, broadcast and unknown all+ // collapse to isGroup=false. `kind` rides the received payload directly; the edited, reaction+ // and revoked events in this family carry only chatId, so derive it there.+ field: 'kind',+ kind: 'enum',+ operators: ENUM_OPERATORS,+ enumValues: CHAT_KINDS,+ resolve: data => {+ const k = str(data.kind);+ if (k) return k;+ const chatId = str(data.chatId);+ return chatId ? chatKind(chatId) : undefined;+ },+ },{field: 'fromMe',kind: 'boolean',