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.

feat: expose referral metadata in chat messages (#829)

Kizuno18dffe2e2bc2b2
diff --git a/docs/openapi.yaml b/docs/openapi.yaml
index 66066077..401ba959 100644
--- a/docs/openapi.yaml
+++ b/docs/openapi.yaml
@@ -5854,6 +5854,10 @@ components:
type: string
example: '{"call_id":"ABC","auto_rejected":false}'
description: JSON string with call details when media_type is `call`. Omitted when not applicable (matches Go json omitempty).
+ referral_metadata:
+ type: string
+ example: '{"ctwa_clid":"clid_123","source_app":"whatsapp"}'
+ description: JSON string with Meta Ads referral/attribution details when the message originated from a Click-to-WhatsApp ad. Omitted when not applicable (matches Go json omitempty).
filename:
type: string
example: 'photo.jpg'
diff --git a/src/domains/chat/chat.go b/src/domains/chat/chat.go
index 9d2fad9b..c738400e 100644
--- a/src/domains/chat/chat.go
+++ b/src/domains/chat/chat.go
@@ -66,12 +66,13 @@ type MessageInfo struct {
MediaType string `json:"media_type"`
Reactions []ReactionInfo `json:"reactions,omitempty"`
// CallMetadata is JSON when media_type is "call" (incoming call log).
- CallMetadata string `json:"call_metadata,omitempty"`
- Filename string `json:"filename"`
- URL string `json:"url"`
- FileLength uint64 `json:"file_length"`
- CreatedAt string `json:"created_at"`
- UpdatedAt string `json:"updated_at"`
+ CallMetadata string `json:"call_metadata,omitempty"`
+ ReferralMetadata string `json:"referral_metadata,omitempty"`
+ Filename string `json:"filename"`
+ URL string `json:"url"`
+ FileLength uint64 `json:"file_length"`
+ CreatedAt string `json:"created_at"`
+ UpdatedAt string `json:"updated_at"`
}
type PaginationResponse struct {
diff --git a/src/usecase/chat.go b/src/usecase/chat.go
index c5dec65e..d08de127 100644
--- a/src/usecase/chat.go
+++ b/src/usecase/chat.go
@@ -205,6 +205,7 @@ func (service serviceChat) GetChatMessages(ctx context.Context, request domainCh
IsFromMe: message.IsFromMe,
MediaType: message.MediaType,
CallMetadata: message.CallMetadata,
+ ReferralMetadata: message.ReferralMetadata,
Filename: message.Filename,
URL: message.URL,
FileLength: message.FileLength,
diff --git a/src/usecase/chat_test.go b/src/usecase/chat_test.go
index 2634840c..d433f668 100644
--- a/src/usecase/chat_test.go
+++ b/src/usecase/chat_test.go
@@ -370,6 +370,82 @@ func TestChatSenderDisplayNameJSONContract(t *testing.T) {
}
}
+func TestGetChatMessagesExposesReferralMetadata(t *testing.T) {
+ accountJID := types.NewJID("628999999999", types.DefaultUserServer)
+ deviceID := accountJID.String()
+ chatJID := "[email protected]"
+ now := time.Date(2026, time.May, 16, 8, 0, 0, 0, time.UTC)
+ referralMetadata := `{"ctwa_clid":"clid_123","source_app":"whatsapp"}`
+ repo := &chatUsecaseRepoStub{
+ chat: &domainChatStorage.Chat{
+ DeviceID: deviceID,
+ JID: chatJID,
+ Name: "Alice",
+ LastMessageTime: now,
+ CreatedAt: now,
+ UpdatedAt: now,
+ },
+ messages: []*domainChatStorage.Message{
+ {
+ ID: "msg-ad",
+ ChatJID: chatJID,
+ DeviceID: deviceID,
+ Sender: chatJID,
+ Content: "hello from the ad",
+ Timestamp: now,
+ CreatedAt: now,
+ UpdatedAt: now,
+ ReferralMetadata: referralMetadata,
+ },
+ {
+ ID: "msg-plain",
+ ChatJID: chatJID,
+ DeviceID: deviceID,
+ Sender: chatJID,
+ Content: "hello again",
+ Timestamp: now.Add(time.Minute),
+ CreatedAt: now.Add(time.Minute),
+ UpdatedAt: now.Add(time.Minute),
+ },
+ },
+ }
+ service := NewChatService(repo)
+ client := &whatsmeow.Client{Store: &store.Device{ID: &accountJID, PushName: "Primary Account"}}
+ ctx := whatsapp.ContextWithDevice(context.Background(), whatsapp.NewDeviceInstance(deviceID, client, nil))
+
+ response, err := service.GetChatMessages(ctx, domainChat.GetChatMessagesRequest{
+ ChatJID: chatJID,
+ Limit: 50,
+ })
+ if err != nil {
+ t.Fatalf("get chat messages: %v", err)
+ }
+ if len(response.Data) != 2 {
+ t.Fatalf("expected two messages, got %d", len(response.Data))
+ }
+ if response.Data[0].ReferralMetadata != referralMetadata {
+ t.Fatalf("referral metadata = %q, want %q", response.Data[0].ReferralMetadata, referralMetadata)
+ }
+ if response.Data[1].ReferralMetadata != "" {
+ t.Fatalf("expected empty referral metadata, got %q", response.Data[1].ReferralMetadata)
+ }
+
+ payload, err := json.Marshal(response.Data)
+ if err != nil {
+ t.Fatalf("marshal messages payload: %v", err)
+ }
+ var messages []map[string]any
+ if err := json.Unmarshal(payload, &messages); err != nil {
+ t.Fatalf("unmarshal messages payload: %v", err)
+ }
+ if messages[0]["referral_metadata"] != referralMetadata {
+ t.Fatalf("message referral_metadata = %#v", messages[0]["referral_metadata"])
+ }
+ if _, ok := messages[1]["referral_metadata"]; ok {
+ t.Fatalf("referral_metadata must be omitted when empty, got %#v", messages[1])
+ }
+}
+
type chatUsecaseRepoStub struct {
domainChatStorage.IChatStorageRepository
chat *domainChatStorage.Chat