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
fix(chatwoot): use saved WhatsApp contact name instead of phone number (#688) (#714)
Aldino Kemal92661658fea0
diff --git a/src/infrastructure/whatsapp/chatwoot_content_test.go b/src/infrastructure/whatsapp/chatwoot_content_test.goindex 071095c9..10540dfe 100644--- a/src/infrastructure/whatsapp/chatwoot_content_test.go+++ b/src/infrastructure/whatsapp/chatwoot_content_test.go@@ -243,6 +243,102 @@ func TestExtractChatwootContactInfo(t *testing.T) {})}+// TestExtractChatwootContactInfoPrefersSavedAddressBookName pins the issue #688+// fix: a 1:1 contact's Chatwoot name must prefer the operator's saved+// address-book name (resolved from the WhatsApp contact store) over the event+// pushname and the bare phone number, while still falling back cleanly when no+// saved name exists. The contact-store lookup is stubbed via contactDisplayNameFn.+func TestExtractChatwootContactInfoPrefersSavedAddressBookName(t *testing.T) {+ ctx := context.Background()+ orig := contactDisplayNameFn+ defer func() { contactDisplayNameFn = orig }()++ t.Run("incoming 1:1 prefers saved name over pushname", func(t *testing.T) {+ contactDisplayNameFn = func(_ context.Context, jid string) string {+ if jid == "[email protected]" {+ return "Saved Name"+ }+ return ""+ }+ info, err := extractChatwootContactInfo(ctx, map[string]any{+ "from": "[email protected]",+ "chat_id": "[email protected]",+ "from_name": "Pushy",+ })+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if info.Name != "Saved Name" {+ t.Fatalf("expected saved address-book name, got %q", info.Name)+ }+ })++ t.Run("incoming 1:1 falls back to pushname when no saved name", func(t *testing.T) {+ contactDisplayNameFn = func(context.Context, string) string { return "" }+ info, err := extractChatwootContactInfo(ctx, map[string]any{+ "from": "[email protected]",+ "chat_id": "[email protected]",+ "from_name": "Pushy",+ })+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if info.Name != "Pushy" {+ t.Fatalf("expected fallback to pushname, got %q", info.Name)+ }+ })++ t.Run("incoming 1:1 falls back to phone when no saved name and no pushname", func(t *testing.T) {+ contactDisplayNameFn = func(context.Context, string) string { return "" }+ info, err := extractChatwootContactInfo(ctx, map[string]any{+ "from": "[email protected]",+ "chat_id": "[email protected]",+ })+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if info.Name != "628123456789" {+ t.Fatalf("expected fallback to phone identifier, got %q", info.Name)+ }+ })++ t.Run("outgoing 1:1 uses recipient saved name", func(t *testing.T) {+ contactDisplayNameFn = func(_ context.Context, jid string) string {+ if jid == "[email protected]" {+ return "Recipient Saved"+ }+ return ""+ }+ info, err := extractChatwootContactInfo(ctx, map[string]any{+ "from": "[email protected]",+ "chat_id": "[email protected]",+ "is_from_me": true,+ "from_name": "Me",+ })+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if info.Name != "Recipient Saved" {+ t.Fatalf("expected recipient saved name for outgoing, got %q", info.Name)+ }+ })++ t.Run("outgoing 1:1 falls back to identifier when no saved name", func(t *testing.T) {+ contactDisplayNameFn = func(context.Context, string) string { return "" }+ info, err := extractChatwootContactInfo(ctx, map[string]any{+ "from": "[email protected]",+ "chat_id": "[email protected]",+ "is_from_me": true,+ })+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if info.Name != info.Identifier {+ t.Fatalf("expected Name==Identifier for outgoing fallback, got Name=%q Identifier=%q", info.Name, info.Identifier)+ }+ })+}+// TestBuildChatwootMessageContent pins how a message payload is rendered into the// (content, attachments) pair Chatwoot receives. The media handling and group// sender-prefixing are the regression-prone parts: a dropped attachment or adiff --git a/src/infrastructure/whatsapp/webhook_forward.go b/src/infrastructure/whatsapp/webhook_forward.goindex 62176c65..fdbb4bb4 100644--- a/src/infrastructure/whatsapp/webhook_forward.go+++ b/src/infrastructure/whatsapp/webhook_forward.go@@ -20,6 +20,10 @@ import (var (submitWebhookFn = submitWebhookgetChatwootClientFn = chatwoot.GetDefaultClient+ // contactDisplayNameFn resolves the operator-saved address-book name for a+ // 1:1 JID from the WhatsApp contact store. It is a seam so tests can stub+ // the lookup without a real client/store.+ contactDisplayNameFn = lookupContactDisplayName)// mutexShardCount is the number of mutex shards for contact synchronization.@@ -199,10 +203,23 @@ func extractChatwootContactInfo(ctx context.Context, data map[string]any) (*chatlogrus.Infof("Chatwoot: Detected group message, using group contact: %s", info.Name)} else if isFromMe {info.Identifier = chatwootIdentifierForJID(chatID)- info.Name = info.Identifier+ // The contact is the recipient (chatID). Prefer the operator's saved+ // address-book name so an operator-initiated chat isn't created with a+ // bare phone number; there is no useful pushname for our own messages.+ info.Name = contactDisplayNameFn(ctx, chatID)+ if info.Name == "" {+ info.Name = info.Identifier+ }} else {info.Identifier = chatwootIdentifierForJID(from)- info.Name = fromName+ // Prefer the operator's saved address-book name (FullName), then the+ // sender's pushname from the event, then the bare phone/identifier.+ // Using only the pushname meant a number the operator had saved under a+ // real name still surfaced in Chatwoot as its phone number (issue #688).+ info.Name = contactDisplayNameFn(ctx, from)+ if info.Name == "" {+ info.Name = fromName+ }if info.Name == "" {info.Name = info.Identifier}@@ -229,6 +246,41 @@ func chatwootIdentifierForJID(jid string) string {return utils.ExtractPhoneFromJID(jid)}+// lookupContactDisplayName returns the operator-saved address-book name for a+// 1:1 WhatsApp JID from the local contact store, preferring the saved FullName+// over the contact's self-set pushname and business name (the same priority and+// source used by the /user/my/contacts endpoint). It returns "" when no client+// is available or the contact is unknown, letting callers fall back to the live+// event pushname / phone number.+func lookupContactDisplayName(ctx context.Context, jid string) string {+ client := ClientFromContext(ctx)+ if client == nil || client.Store == nil || client.Store.Contacts == nil {+ return ""+ }+ parsed, err := types.ParseJID(jid)+ if err != nil || parsed.IsEmpty() {+ return ""+ }+ // The incoming context may already be canceled (this runs from a goroutine,+ // mirroring getGroupName); use a fresh short deadline for the local read.+ freshCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancel()+ contact, err := client.Store.Contacts.GetContact(freshCtx, parsed)+ if err != nil || !contact.Found {+ return ""+ }+ switch {+ case contact.FullName != "":+ return contact.FullName+ case contact.PushName != "":+ return contact.PushName+ case contact.BusinessName != "":+ return contact.BusinessName+ default:+ return ""+ }+}+// extractMediaPath returns the on-disk path for a media field in the// webhook payload, handling both shapes that buildAutoDownloadPayload can// emit: a bare string (no caption) or a map with a "path" key (when a