Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

fix(chatwoot): skip @newsletter JIDs before contact creation

Aldino Kemale3aac4c6bf3b
diff --git a/src/infrastructure/chatwoot/sync.go b/src/infrastructure/chatwoot/sync.go
index ff442e19..4ce6ae5f 100644
--- a/src/infrastructure/chatwoot/sync.go
+++ b/src/infrastructure/chatwoot/sync.go
@@ -203,13 +203,13 @@ func (s *SyncService) SyncHistory(ctx context.Context, deviceID string, waClient
return progress, err // Context cancelled
}
- // status@broadcast and [email protected] carry no actionable
- // conversation for an agent, and operators can ignore additional JIDs
- // (or whole address spaces) via CHATWOOT_IGNORE_JIDS. Skipping at the
- // chat level (rather than per-message) keeps the totals honest in the
- // progress tracker — the chat is excluded entirely instead of counted
- // as "synced 0".
- if utils.IsSystemBroadcastJID(chat.JID) || utils.MatchesIgnoredJID(chat.JID, config.ChatwootIgnoreJids) {
+ // status@broadcast, [email protected] and @newsletter channels carry no
+ // actionable conversation for an agent, and operators can ignore
+ // additional JIDs (or whole address spaces) via CHATWOOT_IGNORE_JIDS.
+ // Skipping at the chat level (rather than per-message) keeps the
+ // totals honest in the progress tracker — the chat is excluded
+ // entirely instead of counted as "synced 0".
+ if utils.IsSystemBroadcastJID(chat.JID) || utils.IsNewsletterJID(chat.JID) || utils.MatchesIgnoredJID(chat.JID, config.ChatwootIgnoreJids) {
logrus.Debugf("Chatwoot Sync: Skipping ignored chat %s", chat.JID)
continue
}
diff --git a/src/infrastructure/whatsapp/chatwoot_content_test.go b/src/infrastructure/whatsapp/chatwoot_content_test.go
index 10540dfe..5296c2f7 100644
--- a/src/infrastructure/whatsapp/chatwoot_content_test.go
+++ b/src/infrastructure/whatsapp/chatwoot_content_test.go
@@ -105,6 +105,20 @@ func TestExtractChatwootContactInfo(t *testing.T) {
}
})
+ t.Run("newsletter chat_id is skipped", func(t *testing.T) {
+ // Channel (newsletter) feeds are broadcast-only — no conversation for
+ // an agent, and the channel id is not a phone number, so relaying one
+ // would 422 at Chatwoot contact creation ("Phone number should be in
+ // e164 format").
+ _, err := extractChatwootContactInfo(ctx, map[string]any{
+ "from": "120363144038483540@newsletter",
+ "chat_id": "120363144038483540@newsletter",
+ })
+ if err == nil {
+ t.Fatal("expected @newsletter chat to be skipped")
+ }
+ })
+
t.Run("system service account from is skipped", func(t *testing.T) {
// [email protected] is WhatsApp's official service account; guarding on
// 'from' (not just chat_id) blocks its TOS/notice messages.
diff --git a/src/infrastructure/whatsapp/webhook_forward.go b/src/infrastructure/whatsapp/webhook_forward.go
index 5849bf07..ead66c31 100644
--- a/src/infrastructure/whatsapp/webhook_forward.go
+++ b/src/infrastructure/whatsapp/webhook_forward.go
@@ -307,6 +307,13 @@ func extractChatwootContactInfo(ctx context.Context, data map[string]any) (*chat
return nil, fmt.Errorf("skipping system/broadcast JID chat=%s from=%s", chatID, from)
}
+ // Channel (newsletter) feeds are broadcast-only: no conversation for an
+ // agent, and the channel id is not a phone number — relaying one would
+ // fail Chatwoot contact creation with a 422 e164 error.
+ if utils.IsNewsletterJID(chatID) || utils.IsNewsletterJID(from) {
+ return nil, fmt.Errorf("skipping newsletter JID chat=%s from=%s", chatID, from)
+ }
+
// Operator-configured ignore list (CHATWOOT_IGNORE_JIDS) on top of the
// always-ignored system JIDs — supports exact JIDs and the "@g.us" /
// "@s.whatsapp.net" / "@lid" address-space wildcards.
diff --git a/src/pkg/utils/chatwoot.go b/src/pkg/utils/chatwoot.go
index 55dfdcc9..c85cb18c 100644
--- a/src/pkg/utils/chatwoot.go
+++ b/src/pkg/utils/chatwoot.go
@@ -41,6 +41,17 @@ func IsSystemBroadcastJID(jid string) bool {
return jid == "status@broadcast" || jid == "[email protected]"
}
+// IsNewsletterJID reports whether jid belongs to a WhatsApp channel
+// (newsletter). Channels are broadcast feeds with no conversation for an
+// agent to handle, and their local part is an 18-digit channel id — not a
+// phone number — so Chatwoot's contact creation rejects it with a 422
+// ("Phone number should be in e164 format"; E.164 caps at 15 digits).
+// Both the live-forward path (webhook_forward.go) and the history importer
+// (chatwoot/sync.go) share this single definition.
+func IsNewsletterJID(jid string) bool {
+ return strings.HasSuffix(jid, "@newsletter")
+}
+
// Markdown translation between WhatsApp and Chatwoot.
//
// WhatsApp renders *bold*, _italic_, ~strikethrough~. Chatwoot renders the
diff --git a/src/pkg/utils/chatwoot_test.go b/src/pkg/utils/chatwoot_test.go
index ae737c10..8639746f 100644
--- a/src/pkg/utils/chatwoot_test.go
+++ b/src/pkg/utils/chatwoot_test.go
@@ -141,6 +141,32 @@ func TestMatchesIgnoredJID(t *testing.T) {
}
}
+func TestIsNewsletterJID(t *testing.T) {
+ // Newsletter (channel) JIDs like 120363144038483540@newsletter are
+ // broadcast feeds, not conversations. Their local part is an 18-digit
+ // channel id — not a phone number — so letting one reach the Chatwoot
+ // contact-creation phone path always fails with a 422 "Phone number
+ // should be in e164 format" (E.164 caps at 15 digits).
+ tests := []struct {
+ name string
+ jid string
+ want bool
+ }{
+ {name: "newsletter JID", jid: "120363144038483540@newsletter", want: true},
+ {name: "ordinary user JID", jid: "[email protected]", want: false},
+ {name: "group JID", jid: "[email protected]", want: false},
+ {name: "lid JID", jid: "abc123@lid", want: false},
+ {name: "empty string", jid: "", want: false},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := IsNewsletterJID(tt.jid); got != tt.want {
+ t.Fatalf("IsNewsletterJID(%q) = %v, want %v", tt.jid, got, tt.want)
+ }
+ })
+ }
+}
+
func TestIsSystemBroadcastJID(t *testing.T) {
// Pinning the exact strings prevents a future "match by suffix" rewrite
// from letting real-status-bearing JIDs (e.g. "[email protected]")