GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
feat(webhook): add WHATSAPP_WEBHOOK_IGNORE_JIDS to skip chats/JIDs (#736)
Hub Operacional9ee16292a403
diff --git a/docs/webhook-payload.md b/docs/webhook-payload.mdindex 33721faa..01b4b9c9 100644--- a/docs/webhook-payload.md+++ b/docs/webhook-payload.md@@ -79,6 +79,45 @@ WHATSAPP_WEBHOOK_EVENTS=group.participants,group.joined,newsletter.joined,newsle- If configured, only the specified events are forwarded to webhooks- Event names are case-insensitive+### Ignoring chats/JIDs++In addition to filtering by **event type** above, you can skip events by **conversation or sender JID** —+for example, to mute all group traffic from the webhook. This is independent of `WHATSAPP_WEBHOOK_EVENTS`+(the two filters compose: an event is forwarded only if its type is allowed **and** its JID is not ignored).++**Environment Variable:**++```bash+# Drop all group messages/receipts (any chat_id ending in @g.us)++# Drop all groups plus one specific 1:1 chat+```++**CLI Flag:**++```bash+./whatsapp rest --webhook="https://yourapp.com/webhook" --webhook-ignore-jids="@g.us"+```++**Behavior:**++- Matches the event's `chat_id`, `from`, `chat_lid` or `from_lid` against the list (so an `@lid`+ pattern matches LID-migrated events, whose `@lid` JID lives in the `*_lid` fields).+- An `@`-prefixed entry is an address-space **wildcard** (`@g.us`, `@s.whatsapp.net`, `@lid`); any other+ entry is an **exact** JID match.+- Empty/unset (default) forwards everything.+- Independent from the Chatwoot integration, which keeps its own `CHATWOOT_IGNORE_JIDS`.+- `@g.us` is the recommended way to mute groups (it matches the group `chat_id`). The+ `@s.whatsapp.net` wildcard matches the **sender** too, so it also suppresses group messages (whose+ `from` is the participant's `@s.whatsapp.net` JID) — use exact JIDs if you only want to mute specific+ 1:1 chats.+- Note: a few events carry the group JID elsewhere or omit `chat_id` — `call.offer` puts the group in+ `group_jid` (not `chat_id`), `message.deleted` only includes `chat_id` when the original message is+ found locally, and `group.participants`/`group.joined` have no `from`. The `@g.us` wildcard still+ covers ordinary group messages and receipts (which is the common case for muting groups).+## Security### HMAC Signature Verificationdiff --git a/readme.md b/readme.mdindex ebdaea4e..1c95c40a 100644--- a/readme.md+++ b/readme.md@@ -155,6 +155,13 @@ Download:| `call.offer` | Incoming call received |If not configured (empty), all events will be forwarded.+- **Webhook JID Filtering**++ You can skip events for specific chats or senders (e.g. mute all groups) before they are forwarded:+ - `--webhook-ignore-jids="@g.us,[email protected]"` (comma-separated list)+ - Or environment variable: `[email protected]`+ - Supports the `@g.us` / `@s.whatsapp.net` / `@lid` wildcards (match a whole address space) and exact JIDs.+ - This filters by conversation/sender and is independent of `--webhook-events` (which filters by event type). The Chatwoot integration keeps its own `CHATWOOT_IGNORE_JIDS`.- **Webhook TLS Configuration**If you encounter TLS certificate verification errors when using webhooks (e.g., with Cloudflare tunnels or self-signed@@ -222,6 +229,7 @@ To use environment variables:| `WHATSAPP_WEBHOOK_SECRET` | Webhook secret for validation | `secret` | `WHATSAPP_WEBHOOK_SECRET=super-secret-key` || `WHATSAPP_WEBHOOK_INSECURE_SKIP_VERIFY` | Skip TLS verification for webhooks (insecure) | `false` | `WHATSAPP_WEBHOOK_INSECURE_SKIP_VERIFY=true` || `WHATSAPP_WEBHOOK_EVENTS` | Whitelist of events to forward (comma-separated, empty = all) | - | `WHATSAPP_WEBHOOK_EVENTS=message,message.ack` |+| `WHATSAPP_WEBHOOK_IGNORE_JIDS` | JIDs/wildcards to skip when forwarding (comma-separated) | - | `[email protected]` || `WHATSAPP_ACCOUNT_VALIDATION` | Enable account validation | `true` | `WHATSAPP_ACCOUNT_VALIDATION=false` || `WHATSAPP_PRESENCE_ON_CONNECT` | Presence on connect: `available`, `unavailable`, or `none` | `unavailable` | `WHATSAPP_PRESENCE_ON_CONNECT=unavailable` || `WHATSAPP_PRESENCE_PULSE_ENABLED` | Enable daily available/unavailable presence pulse | `true` | `WHATSAPP_PRESENCE_PULSE_ENABLED=false` |diff --git a/src/.env.example b/src/.env.exampleindex fce5d80e..b5208de0 100644--- a/src/.env.example+++ b/src/.env.example@@ -23,6 +23,9 @@ WHATSAPP_WEBHOOK=https://webhook.site/07b69616-5943-4c7f-a8be-db4819df699e,httpsWHATSAPP_WEBHOOK_SECRET=super-secret-keyWHATSAPP_WEBHOOK_INSECURE_SKIP_VERIFY=falseWHATSAPP_WEBHOOK_EVENTS=message,message.reaction,message.revoked,message.edited,message.ack,message.deleted,group.participants+# Comma-separated WhatsApp JIDs (or "@g.us"/"@s.whatsapp.net"/"@lid" wildcards) to skip when forwarding to the webhook.+# Example: "@g.us" drops all group traffic; add exact JIDs to mute specific chats/senders.+WHATSAPP_WEBHOOK_IGNORE_JIDS=WHATSAPP_WEBHOOK_INCLUDE_OUTGOING=falseWHATSAPP_ACCOUNT_VALIDATION=trueWHATSAPP_PRESENCE_ON_CONNECT=unavailablediff --git a/src/cmd/root.go b/src/cmd/root.goindex dc993336..91f132c0 100644--- a/src/cmd/root.go+++ b/src/cmd/root.go@@ -144,6 +144,16 @@ func initEnvConfig() {events := strings.Split(envWebhookEvents, ",")config.WhatsappWebhookEvents = events}+ if envWebhookIgnoreJids := viper.GetString("whatsapp_webhook_ignore_jids"); envWebhookIgnoreJids != "" {+ parts := strings.Split(envWebhookIgnoreJids, ",")+ jids := make([]string, 0, len(parts))+ for _, p := range parts {+ if trimmed := strings.TrimSpace(p); trimmed != "" {+ jids = append(jids, trimmed)+ }+ }+ config.WhatsappWebhookIgnoreJids = jids+ }if viper.IsSet("whatsapp_account_validation") {config.WhatsappAccountValidation = viper.GetBool("whatsapp_account_validation")}@@ -358,6 +368,12 @@ func initFlags() {config.WhatsappWebhookEvents,`whitelist of events to forward to webhook (empty = all events) --webhook-events <string> | example: --webhook-events="message,message.ack,group.participants"`,)+ rootCmd.PersistentFlags().StringSliceVarP(+ &config.WhatsappWebhookIgnoreJids,+ "webhook-ignore-jids", "",+ config.WhatsappWebhookIgnoreJids,+ `comma-separated WhatsApp JIDs (or "@g.us"/"@s.whatsapp.net"/"@lid" wildcards) to skip when forwarding to webhooks --webhook-ignore-jids <list> | example: --webhook-ignore-jids="@g.us,[email protected]"`,+ )rootCmd.PersistentFlags().BoolVarP(&config.WhatsappAccountValidation,"account-validation", "",diff --git a/src/config/settings.go b/src/config/settings.goindex a9aa7ac4..a464d3b2 100644--- a/src/config/settings.go+++ b/src/config/settings.go@@ -35,6 +35,7 @@ var (WhatsappWebhookSecret = "secret"WhatsappWebhookInsecureSkipVerify = false // Skip TLS certificate verification for webhooks (insecure)WhatsappWebhookEvents []string // Whitelist of events to forward to webhook (empty = all events)+ WhatsappWebhookIgnoreJids []string // JIDs (or "@g.us"/"@s.whatsapp.net"/"@lid" wildcards) to skip when forwarding to webhooksWhatsappAutoRejectCall = false // Auto-reject incoming callsWhatsappLogLevel = "ERROR"WhatsappSettingMaxImageSize int64 = 20000000 // 20MBdiff --git a/src/infrastructure/whatsapp/webhook_forward.go b/src/infrastructure/whatsapp/webhook_forward.goindex ead66c31..6682c99a 100644--- a/src/infrastructure/whatsapp/webhook_forward.go+++ b/src/infrastructure/whatsapp/webhook_forward.go@@ -94,7 +94,8 @@ func forwardPayloadToConfiguredWebhooks(ctx context.Context, payload map[string]webhookConfig = nil}- webhookAllowed := isEventWhitelistedForDevice(eventName, webhookConfig)+ webhookAllowed := isEventWhitelistedForDevice(eventName, webhookConfig) &&+ !shouldIgnoreWebhookJID(payload)chatwootAllowed := config.ChatwootEnabled && shouldForwardEventToChatwoot(eventName) && isEventWhitelistedForChatwoot(eventName)if !webhookAllowed && !chatwootAllowed {@@ -192,6 +193,33 @@ func isEventWhitelistedForDevice(eventName string, deviceConfig *domainChatStorareturn len(config.WhatsappWebhookEvents) == 0 || isEventWhitelisted(eventName)}+// shouldIgnoreWebhookJID reports whether an event should be skipped for WHATSAPP_WEBHOOK+// forwarding because its chat or sender JID matches WHATSAPP_WEBHOOK_IGNORE_JIDS (e.g. the+// "@g.us" wildcard to drop all group traffic). The JID fields live in the nested inner+// payload, so it descends one level. Both the resolved phone JIDs (chat_id/from) and the+// LID forms (chat_lid/from_lid) are matched: a LID-migrated event keeps the @lid JID in the+// *_lid fields while chat_id/from hold the resolved phone JID, so an "@lid" pattern (or an+// exact ...@lid) only matches via the *_lid fields. It is a no-op when the ignore list is+// empty, the inner payload is absent, or no JID matches — so events without a JID and the+// default (no list configured) keep forwarding unchanged. This only gates the generic+// webhook; the Chatwoot path keeps its own CHATWOOT_IGNORE_JIDS filter.+func shouldIgnoreWebhookJID(payload map[string]any) bool {+ ignore := config.WhatsappWebhookIgnoreJids+ if len(ignore) == 0 {+ return false+ }+ data, ok := payload["payload"].(map[string]any)+ if !ok {+ return false+ }+ for _, key := range []string{"chat_id", "from", "chat_lid", "from_lid"} {+ if jid, _ := data[key].(string); utils.MatchesIgnoredJID(jid, ignore) {+ return true+ }+ }+ return false+}+// addWebhookSessionID injects the operator-facing session id into a webhook// payload, derived from its device_id (the WhatsApp JID). It is a no-op when the// JID can't be mapped to a session (single-session deployments before login, ordiff --git a/src/infrastructure/whatsapp/webhook_forward_ignore_jids_test.go b/src/infrastructure/whatsapp/webhook_forward_ignore_jids_test.gonew file mode 100644index 00000000..2c983bf5--- /dev/null+++ b/src/infrastructure/whatsapp/webhook_forward_ignore_jids_test.go@@ -0,0 +1,122 @@+package whatsapp++import (+ "context"+ "testing"++ "github.com/aldinokemal/go-whatsapp-web-multidevice/config"+ domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"+)++// ignoreJidPayload builds a webhook envelope with the nested inner payload where chat_id/from+// live (matching how event_message.go etc. shape the body passed to the forwarder).+func ignoreJidPayload(chatID, from string) map[string]any {+ return map[string]any{+ "event": "message",+ "device_id": "org_1",+ "payload": map[string]any{+ "chat_id": chatID,+ "from": from,+ },+ }+}++// runIgnoreJidForward runs the forwarder with a single webhook configured and the given+// WHATSAPP_WEBHOOK_IGNORE_JIDS list, reporting whether the event was forwarded (submit called).+func runIgnoreJidForward(t *testing.T, ignoreJids []string, eventName string, payload map[string]any) bool {+ t.Helper()++ originalWebhooks := config.WhatsappWebhook+ originalEvents := config.WhatsappWebhookEvents+ originalIgnore := config.WhatsappWebhookIgnoreJids+ config.WhatsappWebhook = []string{"https://test.com"}+ config.WhatsappWebhookEvents = nil+ config.WhatsappWebhookIgnoreJids = ignoreJids+ defer func() {+ config.WhatsappWebhook = originalWebhooks+ config.WhatsappWebhookEvents = originalEvents+ config.WhatsappWebhookIgnoreJids = originalIgnore+ }()++ called := false+ originalSubmit := submitWebhookFn+ submitWebhookFn = func(context.Context, map[string]any, string, *domainChatStorage.DeviceWebhookConfig) error {+ called = true+ return nil+ }+ defer func() { submitWebhookFn = originalSubmit }()++ if err := forwardPayloadToConfiguredWebhooks(context.Background(), payload, eventName); err != nil {+ t.Fatalf("expected no error, got %v", err)+ }+ return called+}++func TestWebhookIgnoreJID_GroupWildcardDropsGroupMessage(t *testing.T) {+ payload := ignoreJidPayload("[email protected]", "[email protected]")+ if runIgnoreJidForward(t, []string{"@g.us"}, "message", payload) {+ t.Fatal("group message should be dropped when @g.us is in WHATSAPP_WEBHOOK_IGNORE_JIDS")+ }+}++func TestWebhookIgnoreJID_GroupWildcardKeepsDirectMessage(t *testing.T) {+ payload := ignoreJidPayload("[email protected]", "[email protected]")+ if !runIgnoreJidForward(t, []string{"@g.us"}, "message", payload) {+ t.Fatal("1:1 message should still be forwarded when only @g.us is ignored")+ }+}++func TestWebhookIgnoreJID_ExactJIDMatchesSender(t *testing.T) {+ payload := ignoreJidPayload("[email protected]", "[email protected]")+ if runIgnoreJidForward(t, []string{"[email protected]"}, "message", payload) {+ t.Fatal("message should be dropped when its sender JID (from) is in the ignore list")+ }+}++func TestWebhookIgnoreJID_EmptyListForwardsAll(t *testing.T) {+ payload := ignoreJidPayload("[email protected]", "[email protected]")+ if !runIgnoreJidForward(t, nil, "message", payload) {+ t.Fatal("group message should be forwarded when the ignore list is empty (default)")+ }+}++func TestWebhookIgnoreJID_EventWithoutInnerPayloadForwards(t *testing.T) {+ // Envelope with no nested "payload" map (no JID to match) must keep forwarding —+ // the filter must never drop events that carry no chat/sender JID.+ payload := map[string]any{"event": "qr", "device_id": "org_1", "code": "abc"}+ if !runIgnoreJidForward(t, []string{"@g.us"}, "qr", payload) {+ t.Fatal("event without an inner JID must be forwarded (defensive default)")+ }+}++func TestWebhookIgnoreJID_LidWildcardMatchesLidFields(t *testing.T) {+ // LID-migrated event: chat_id/from hold the resolved phone JID, while the @lid+ // JID lives in chat_lid/from_lid. An "@lid" pattern must still drop it.+ payload := map[string]any{+ "event": "message",+ "device_id": "org_1",+ "payload": map[string]any{+ "chat_id": "[email protected]",+ "from": "[email protected]",+ "chat_lid": "111222333@lid",+ "from_lid": "111222333@lid",+ },+ }+ if runIgnoreJidForward(t, []string{"@lid"}, "message", payload) {+ t.Fatal("event whose LID fields end in @lid should be dropped when @lid is ignored")+ }+}++func TestWebhookIgnoreJID_ExactLidMatchesChatLid(t *testing.T) {+ payload := map[string]any{+ "event": "message",+ "device_id": "org_1",+ "payload": map[string]any{+ "chat_id": "[email protected]",+ "chat_lid": "120363999@lid",+ },+ }+ if runIgnoreJidForward(t, []string{"120363999@lid"}, "message", payload) {+ t.Fatal("event should be dropped when its exact chat_lid JID is in the ignore list")+ }+}