Skip to content

Commit f889c85

Browse files
committed
fix: carousel card template message
Signed-off-by: sarthakjdev <jsarthak448@gmail.com>
1 parent 497a11d commit f889c85

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.vscode/
3-
dist/*
3+
dist/*
4+
.omx

manager/template_manager.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type WhatsAppBusinessTemplatesFetchResponseEdge struct {
7575

7676
// TemplateMessageComponentCard represents a card component in a message template.
7777
type TemplateMessageComponentCard struct {
78-
// Add card-specific fields if needed.
78+
CardIndex int `json:"card_index,omitempty"`
7979
Components []WhatsAppBusinessHSMWhatsAppHSMComponent `json:"components,omitempty"`
8080
}
8181

@@ -141,6 +141,7 @@ const (
141141
MessageTemplateComponentFormatText MessageTemplateComponentFormat = "TEXT"
142142
MessageTemplateComponentFormatImage MessageTemplateComponentFormat = "IMAGE"
143143
MessageTemplateComponentFormatDocument MessageTemplateComponentFormat = "DOCUMENT"
144+
MessageTemplateComponentFormatProduct MessageTemplateComponentFormat = "PRODUCT"
144145
MessageTemplateComponentFormatVideo MessageTemplateComponentFormat = "VIDEO"
145146
MessageTemplateComponentFormatLocation MessageTemplateComponentFormat = "LOCATION"
146147
)
@@ -183,6 +184,7 @@ type WhatsappMessageTemplateButtonCreateRequestBodyAlias = WhatsappMessageTempla
183184
// WhatsappMessageTemplateComponentCreateOrUpdateRequestBody represents the request body for creating/updating a component.
184185
type WhatsappMessageTemplateComponentCreateOrUpdateRequestBody struct {
185186
Type MessageTemplateComponentType `json:"type,omitempty"`
187+
Cards []TemplateMessageComponentCard `json:"cards,omitempty"`
186188
Format MessageTemplateComponentFormat `json:"format,omitempty"`
187189
Text string `json:"text,omitempty"`
188190
Buttons []WhatsappMessageTemplateButtonCreateRequestBody `json:"buttons,omitempty"`
@@ -296,8 +298,8 @@ type TemplateMigrationResponse struct {
296298
// MigrateFromOtherBusinessAccount migrates templates from another business account.
297299
func (manager *TemplateManager) MigrateFromOtherBusinessAccount(sourcePageNumber int, sourceWabaId int) (*TemplateMigrationResponse, error) {
298300
apiRequest := manager.requester.NewApiRequest(strings.Join([]string{manager.businessAccountId, "migrate_message_templates"}, "/"), http.MethodGet)
299-
apiRequest.AddQueryParam("page_number", string(sourcePageNumber))
300-
apiRequest.AddQueryParam("source_waba_id", string(sourceWabaId))
301+
apiRequest.AddQueryParam("page_number", strconv.Itoa(sourcePageNumber))
302+
apiRequest.AddQueryParam("source_waba_id", strconv.Itoa(sourceWabaId))
301303
response, err := apiRequest.Execute()
302304
if err != nil {
303305
return nil, err

pkg/components/template_message.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,19 @@ func (t TemplateMessageComponentBodyType) GetComponentType() string {
8484
return string(t.Type)
8585
}
8686

87-
type TemplateMessageCaraouselCard struct {
87+
type TemplateMessageCarouselCard struct {
8888
CardIndex int `json:"card_index" validate:"required"`
8989
Components []TemplateMessageComponent `json:"components" validate:"required"` // only header, buttons and body
9090
}
9191

9292
type TemplateMessageComponentCarouselType struct {
93-
Type TemplateMessageComponentType `json:"type" validate:"required"` // "carousel"
94-
Cards []TemplateMessageCaraouselCard `json:"cards" validate:"required"`
93+
Type TemplateMessageComponentType `json:"type" validate:"required"` // "carousel"
94+
Cards []TemplateMessageCarouselCard `json:"cards" validate:"required"`
95+
}
96+
97+
// GetComponentType returns the component type.
98+
func (t TemplateMessageComponentCarouselType) GetComponentType() string {
99+
return string(t.Type)
95100
}
96101

97102
// TemplateMessageParameterType represents the type of a parameter.
@@ -284,6 +289,33 @@ func (tm *TemplateMessage) AddBody(params TemplateMessageComponentBodyType) {
284289
}
285290
}
286291

292+
// AddCarousel adds (or overrides) a carousel component in the template message.
293+
// Only one carousel is allowed, and a carousel can contain up to 10 cards.
294+
func (tm *TemplateMessage) AddCarousel(params TemplateMessageComponentCarouselType) error {
295+
if len(params.Cards) > 10 {
296+
return fmt.Errorf("maximum number of carousel cards reached")
297+
}
298+
299+
var existingCarouselIndex int
300+
var found bool
301+
302+
for i, component := range tm.Components {
303+
if TemplateMessageComponentType(component.GetComponentType()) == TemplateMessageComponentTypeCarousel {
304+
existingCarouselIndex = i
305+
found = true
306+
break
307+
}
308+
}
309+
310+
if found {
311+
tm.Components[existingCarouselIndex] = params
312+
} else {
313+
tm.Components = append(tm.Components, params)
314+
}
315+
316+
return nil
317+
}
318+
287319
// AddButton adds a button component to the template message.
288320
// A maximum of 10 buttons is allowed.
289321
func (tm *TemplateMessage) AddButton(params TemplateMessageComponentButtonType) error {

0 commit comments

Comments
 (0)