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): forward media album header counts and album_id on album items (#870)
Alejandro96753c4d6791
diff --git a/docs/webhook-payload.md b/docs/webhook-payload.mdindex 2fe5bd8e..7bed1772 100644--- a/docs/webhook-payload.md+++ b/docs/webhook-payload.md@@ -1584,6 +1584,49 @@ When a message is edited, the webhook includes the original message ID to track}```+### Media Album++When several photos or videos are sent together, WhatsApp first sends an album header that only announces how many items+follow, then each item as a regular image or video message. The header is forwarded with an `album` object (and no body):++```json+{+ "event": "message",+ "device_id": "[email protected]",+ "payload": {+ "id": "3EB0A1B2C3D4E5F6A7B8",+ "chat_id": "[email protected]",+ "from": "[email protected]",+ "from_name": "John Doe",+ "timestamp": "2023-10-15T11:42:00Z",+ "album": {+ "expected_image_count": 3,+ "expected_video_count": 1+ }+ }+}+```++Each item carries `album_id`, the `id` of its header, so the items can be grouped:++```json+{+ "event": "message",+ "device_id": "[email protected]",+ "payload": {+ "id": "3EB0F1E2D3C4B5A6F7E8",+ "chat_id": "[email protected]",+ "from": "[email protected]",+ "from_name": "John Doe",+ "timestamp": "2023-10-15T11:42:01Z",+ "image": "statics/media/1752405121-image.jpeg",+ "album_id": "3EB0A1B2C3D4E5F6A7B8"+ }+}+```++The header is not forwarded to Chatwoot, since it has no content of its own.+### Forwarded Message```jsondiff --git a/src/infrastructure/whatsapp/event_message.go b/src/infrastructure/whatsapp/event_message.goindex 91b28b5b..895bdd91 100644--- a/src/infrastructure/whatsapp/event_message.go+++ b/src/infrastructure/whatsapp/event_message.go@@ -211,6 +211,7 @@ func payloadHasNoRenderableContent(payload map[string]any) bool {"image", "audio", "video", "video_note", "document", "sticker","contact", "contacts_array", "list", "live_location", "location", "order","template", "buttons", "product", "selection", "interactive",+ "album",}for _, key := range renderableKeys {if _, ok := payload[key]; ok {@@ -243,6 +244,7 @@ func hasRecognizedMessageType(msg *waE2E.Message) bool {msg.GetTemplateMessage() != nil,msg.GetButtonsMessage() != nil,msg.GetProductMessage() != nil,+ msg.GetAlbumMessage() != nil,msg.GetListResponseMessage() != nil,msg.GetButtonsResponseMessage() != nil,msg.GetTemplateButtonReplyMessage() != nil,@@ -368,10 +370,38 @@ func buildOptionalFields(ctx context.Context, client *whatsmeow.Client, evt *eve}buildOtherMessageTypes(msg, payload)+ buildAlbumFields(evt, msg, payload)return nil}+// buildAlbumFields exposes WhatsApp's media album grouping. When several photos or+// videos are sent together, WhatsApp first sends an AlbumMessage carrying only the+// expected counts, then each item as a regular image/video message linked to it+// through messageContextInfo.messageAssociation (type MEDIA_ALBUM). Without this the+// album header reaches consumers as an empty, unsupported message and the items+// cannot be grouped.+func buildAlbumFields(evt *events.Message, msg *waE2E.Message, payload map[string]any) {+ if album := msg.GetAlbumMessage(); album != nil {+ payload["album"] = map[string]any{+ "expected_image_count": album.GetExpectedImageCount(),+ "expected_video_count": album.GetExpectedVideoCount(),+ }+ return+ }++ association := evt.Message.GetMessageContextInfo().GetMessageAssociation()+ if association == nil {+ association = msg.GetMessageContextInfo().GetMessageAssociation()+ }+ if association.GetAssociationType() != waE2E.MessageAssociation_MEDIA_ALBUM {+ return+ }+ if parentID := association.GetParentMessageKey().GetID(); parentID != "" {+ payload["album_id"] = parentID+ }+}+func buildMediaFields(ctx context.Context, client *whatsmeow.Client, msg *waE2E.Message, payload map[string]any) error {if audioMedia := msg.GetAudioMessage(); audioMedia != nil {if config.WhatsappAutoDownloadMedia {diff --git a/src/infrastructure/whatsapp/event_message_test.go b/src/infrastructure/whatsapp/event_message_test.goindex 8381c465..6c9dde49 100644--- a/src/infrastructure/whatsapp/event_message_test.go+++ b/src/infrastructure/whatsapp/event_message_test.go@@ -641,3 +641,97 @@ func TestBuildEventPayloadIncludesSenderDisplayName(t *testing.T) {})}}++func albumEventForTest(id string, message *waE2E.Message) *events.Message {+ return &events.Message{+ Info: types.MessageInfo{+ MessageSource: types.MessageSource{+ Chat: types.NewJID("123", types.DefaultUserServer),+ Sender: types.NewJID("456", types.DefaultUserServer),+ },+ ID: types.MessageID(id),+ Timestamp: time.Date(2026, time.October, 7, 10, 0, 0, 0, time.UTC),+ },+ Message: message,+ }+}++func TestBuildEventPayloadAlbumHeaderIncludesExpectedCounts(t *testing.T) {+ images, videos := uint32(3), uint32(1)+ evt := albumEventForTest("ALBUM1", &waE2E.Message{+ AlbumMessage: &waE2E.AlbumMessage{ExpectedImageCount: &images, ExpectedVideoCount: &videos},+ })++ eventType, payload, err := buildEventPayload(context.Background(), nil, evt)+ if err != nil {+ t.Fatalf("expected no error, got %v", err)+ }+ assert.Equal(t, EventTypeMessage, eventType)+ assert.Equal(t, map[string]any{"expected_image_count": images, "expected_video_count": videos}, payload["album"])+ assert.False(t, payloadHasNoRenderableContent(payload))+ assert.True(t, hasRecognizedMessageType(evt.Message))+ _, hasAlbumID := payload["album_id"]+ assert.False(t, hasAlbumID)+}++func TestBuildEventPayloadAlbumItemIncludesAlbumID(t *testing.T) {+ previous := config.WhatsappAutoDownloadMedia+ config.WhatsappAutoDownloadMedia = false+ t.Cleanup(func() { config.WhatsappAutoDownloadMedia = previous })++ albumType := waE2E.MessageAssociation_MEDIA_ALBUM+ evt := albumEventForTest("ITEM1", &waE2E.Message{+ ImageMessage: &waE2E.ImageMessage{},+ MessageContextInfo: &waE2E.MessageContextInfo{+ MessageAssociation: &waE2E.MessageAssociation{+ AssociationType: &albumType,+ ParentMessageKey: &waCommon.MessageKey{ID: protoString("ALBUM1")},+ },+ },+ })++ eventType, payload, err := buildEventPayload(context.Background(), nil, evt)+ if err != nil {+ t.Fatalf("expected no error, got %v", err)+ }+ assert.Equal(t, EventTypeMessage, eventType)+ assert.Equal(t, "ALBUM1", payload["album_id"])+ assert.Contains(t, payload, "image")+}++func TestBuildEventPayloadIgnoresNonAlbumAssociations(t *testing.T) {+ previous := config.WhatsappAutoDownloadMedia+ config.WhatsappAutoDownloadMedia = false+ t.Cleanup(func() { config.WhatsappAutoDownloadMedia = previous })++ otherType := waE2E.MessageAssociation_MOTION_PHOTO+ evt := albumEventForTest("ITEM2", &waE2E.Message{+ ImageMessage: &waE2E.ImageMessage{},+ MessageContextInfo: &waE2E.MessageContextInfo{+ MessageAssociation: &waE2E.MessageAssociation{+ AssociationType: &otherType,+ ParentMessageKey: &waCommon.MessageKey{ID: protoString("PARENT")},+ },+ },+ })++ _, payload, err := buildEventPayload(context.Background(), nil, evt)+ if err != nil {+ t.Fatalf("expected no error, got %v", err)+ }+ _, hasAlbumID := payload["album_id"]+ assert.False(t, hasAlbumID)+}++func TestAlbumHeaderIsNotForwardedToChatwootAsUnsupported(t *testing.T) {+ images := uint32(2)+ evt := albumEventForTest("ALBUM2", &waE2E.Message{AlbumMessage: &waE2E.AlbumMessage{ExpectedImageCount: &images}})++ eventType, payload, err := buildEventPayload(context.Background(), nil, evt)+ if err != nil {+ t.Fatalf("expected no error, got %v", err)+ }+ assert.True(t, isAlbumHeaderPayload(eventType, payload))+ assert.False(t, isAlbumHeaderPayload(EventTypeMessage, map[string]any{"album_id": "ALBUM2", "image": "x.jpg"}))+ assert.False(t, isAlbumHeaderPayload(EventTypeMessageReaction, payload))+}diff --git a/src/infrastructure/whatsapp/webhook_forward.go b/src/infrastructure/whatsapp/webhook_forward.goindex 53bd6839..615f83df 100644--- a/src/infrastructure/whatsapp/webhook_forward.go+++ b/src/infrastructure/whatsapp/webhook_forward.go@@ -1606,6 +1606,13 @@ func syncPayloadToChatwoot(ctx context.Context, payload map[string]any, eventNamreturn nil}+ if isAlbumHeaderPayload(eventName, data) {+ // The album header only announces how many items follow; each photo or+ // video arrives as its own message, so there is nothing to post.+ logrus.Debugf("Chatwoot: Skipping album header %v", data["id"])+ return nil+ }+switch eventName {case "message.ack":syncReadReceiptsToChatwoot(cw, deviceID, linkRepo, resolved.ConfigID, data)@@ -1703,6 +1710,16 @@ func syncPayloadToChatwoot(ctx context.Context, payload map[string]any, eventNamreturn nil}+// isAlbumHeaderPayload reports whether a message event is the AlbumMessage that+// precedes the items of a media album (see buildAlbumFields).+func isAlbumHeaderPayload(eventName string, data map[string]any) bool {+ if eventName != EventTypeMessage {+ return false+ }+ _, ok := data["album"]+ return ok+}+func forwardToChatwoot(ctx context.Context, payload map[string]any, eventName string) {logrus.Infof("Chatwoot: Attempting to forward %s...", eventName)deviceID, linkRepo := chatwootLinkStorageFromContext(ctx)