Skip to content

Commit 591e810

Browse files
authored
feat: Add ecommerce element in rich_message to api v3.6 ME-2195 (#232)
* add ecommerce element to rich_message in v3.6 * customer-api * missing json tags * add ecommerce to message postback * allow sending ecommerce in postbacks * add ecommerce to RichMessageElement --------- Co-authored-by: kacperf531 <kacperf531@users.noreply.github.com>
1 parent feb6b2a commit 591e810

File tree

7 files changed

+118
-32
lines changed

7 files changed

+118
-32
lines changed

agent/api.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,17 @@ func (a *API) SendEvent(chatID string, event interface{}, attachToLastThread boo
232232
}
233233

234234
// SendRichMessagePostback sends postback for given rich message event.
235-
func (a *API) SendRichMessagePostback(chatID, eventID, threadID, postbackID string, toggled bool) error {
235+
func (a *API) SendRichMessagePostback(chatID, eventID, threadID, postbackID string, toggled bool, buttonType, buttonValue string, ecommerce *PostbackEcommerce) error {
236236
return a.Call("send_rich_message_postback", &sendRichMessagePostbackRequest{
237237
ChatID: chatID,
238238
EventID: eventID,
239239
ThreadID: threadID,
240240
Postback: postback{
241-
ID: postbackID,
242-
Toggled: toggled,
241+
ID: postbackID,
242+
Toggled: toggled,
243+
ButtonType: buttonType,
244+
ButtonValue: buttonValue,
245+
Ecommerce: ecommerce,
243246
},
244247
}, &emptyResponse{})
245248
}

agent/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ func TestSendRichMessagePostbackShouldReturnDataReceivedFromAgentAPI(t *testing.
902902
t.Error("API creation failed")
903903
}
904904

905-
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false)
905+
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false, "", "", nil)
906906
if rErr != nil {
907907
t.Errorf("SendRichMessagePostback failed: %v", rErr)
908908
}
@@ -1424,7 +1424,7 @@ func TestSendRichMessagePostbackShouldNotCrashOnErrorResponse(t *testing.T) {
14241424
t.Error("API creation failed")
14251425
}
14261426

1427-
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false)
1427+
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false, "", "", nil)
14281428
verifyErrorResponse("SendRichMessagePostback", rErr, t)
14291429
}
14301430

agent/structures.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import (
1212
type Properties map[string]map[string]interface{}
1313

1414
type postback struct {
15-
ID string `json:"id"`
16-
Toggled bool `json:"toggled"`
15+
ID string `json:"id"`
16+
Toggled bool `json:"toggled"`
17+
ButtonType string `json:"button_type,omitempty"`
18+
ButtonValue string `json:"button_value,omitempty"`
19+
Ecommerce *PostbackEcommerce `json:"ecommerce,omitempty"`
1720
}
1821

1922
type ban struct {
@@ -52,6 +55,12 @@ type TransferChatOptions struct {
5255
IgnoreAgentsAvailability bool
5356
}
5457

58+
type PostbackEcommerce struct {
59+
ProductID string `json:"product_id,omitempty"`
60+
OptionID string `json:"option_id,omitempty"`
61+
Quantity int `json:"quantity,omitempty"`
62+
}
63+
5564
// User represents base of both Customer and Agent
5665
//
5766
// To get specific user type's structure, call Agent() or Customer() (based on Type value).
@@ -704,11 +713,12 @@ func (f *FilledFormField) GroupChooser() *FilledFormFieldGroupChooser {
704713

705714
// Postback represents postback data in LiveChat message event.
706715
type Postback struct {
707-
ID string `json:"id"`
708-
ThreadID string `json:"thread_id"`
709-
EventID string `json:"event_id"`
710-
Type string `json:"type,omitempty"`
711-
Value string `json:"value,omitempty"`
716+
ID string `json:"id"`
717+
ThreadID string `json:"thread_id"`
718+
EventID string `json:"event_id"`
719+
Type string `json:"type,omitempty"`
720+
Value string `json:"value,omitempty"`
721+
Ecommerce *PostbackEcommerce `json:"ecommerce,omitempty"`
712722
}
713723

714724
// Message represents LiveChat message event.
@@ -828,10 +838,11 @@ type RichMessage struct {
828838

829839
// RichMessageElement represents element of LiveChat rich message
830840
type RichMessageElement struct {
831-
Buttons []RichMessageButton `json:"buttons"`
832-
Title string `json:"title"`
833-
Subtitle string `json:"subtitle"`
834-
Image *RichMessageImage `json:"image,omitempty"`
841+
Buttons []RichMessageButton `json:"buttons"`
842+
Title string `json:"title"`
843+
Subtitle string `json:"subtitle"`
844+
Image *RichMessageImage `json:"image,omitempty"`
845+
Ecommerce *RichMessageEcommerce `json:"ecommerce,omitempty"`
835846
}
836847

837848
// RichMessageButton represents button in LiveChat rich message
@@ -858,6 +869,35 @@ type RichMessageImage struct {
858869
AlternativeText string `json:"alternative_text,omitempty"`
859870
}
860871

872+
// RichMessageImage represents ecommerce element in LiveChat rich message
873+
type RichMessageEcommerce struct {
874+
ProductID string `json:"product_id"`
875+
Label string `json:"label"`
876+
ViewType string `json:"view_type"`
877+
Options []RichMessageEcommerceOption `json:"options,omitempty"`
878+
Addons []RichMessageEcommerceAddon `json:"addons,omitempty"`
879+
}
880+
881+
type RichMessageEcommerceOption struct {
882+
OptionID string `json:"option_id"`
883+
Label string `json:"label"`
884+
Price string `json:"price,omitempty"`
885+
RegularPrice string `json:"regular_price,omitempty"`
886+
Currency string `json:"currency,omitempty"`
887+
Color string `json:"color,omitempty"`
888+
ImageURL string `json:"image_url,omitempty"`
889+
ImageThumbnailURL string `json:"image_thumbnail_url,omitempty"`
890+
Available *bool `json:"available,omitempty"`
891+
Selected *bool `json:"selected,omitempty"`
892+
}
893+
894+
type RichMessageEcommerceAddon struct {
895+
AddonType string `json:"addon_type"`
896+
RangeFrom string `json:"range_from,omitempty"`
897+
RangeTo string `json:"range_to,omitempty"`
898+
Currency string `json:"currency,omitempty"`
899+
}
900+
861901
// RichMessage function converts Event object to RichMessage object if Event's Type is "rich_message".
862902
// If Type is different or Event is malformed, then it returns nil.
863903
func (e *Event) RichMessage() *RichMessage {

customer/api.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,17 @@ func (a *API) DeactivateChat(chatID string) error {
181181
}
182182

183183
// SendRichMessagePostback sends postback for given rich message event.
184-
func (a *API) SendRichMessagePostback(chatID, threadID, eventID, postbackID string, toggled bool) error {
184+
func (a *API) SendRichMessagePostback(chatID, threadID, eventID, postbackID string, toggled bool, buttonType, buttonValue string, ecommerce *PostbackEcommerce) error {
185185
return a.Call("send_rich_message_postback", &sendRichMessagePostbackRequest{
186186
ChatID: chatID,
187187
ThreadID: threadID,
188188
EventID: eventID,
189189
Postback: postback{
190-
ID: postbackID,
191-
Toggled: toggled,
190+
ID: postbackID,
191+
Toggled: toggled,
192+
ButtonType: buttonType,
193+
ButtonValue: buttonValue,
194+
Ecommerce: ecommerce,
192195
},
193196
}, &emptyResponse{})
194197
}

customer/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ func TestSendRichMessagePostbackShouldReturnDataReceivedFromCustomerAPI(t *testi
636636
t.Error("API creation failed")
637637
}
638638

639-
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false)
639+
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false, "", "", nil)
640640
if rErr != nil {
641641
t.Errorf("SendRichMessagePostback failed: %v", rErr)
642642
}
@@ -1073,7 +1073,7 @@ func TestSendRichMessagePostbackShouldNotCrashOnErrorResponse(t *testing.T) {
10731073
t.Error("API creation failed")
10741074
}
10751075

1076-
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false)
1076+
rErr := api.SendRichMessagePostback("stubChatID", "stubThreadID", "stubEventID", "stubPostbackID", false, "", "", nil)
10771077
verifyErrorResponse("SendRichMessagePostback", rErr, t)
10781078
}
10791079

customer/internal.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ type sendRichMessagePostbackRequest struct {
7272
}
7373

7474
type postback struct {
75-
ID string `json:"id"`
76-
Toggled bool `json:"toggled"`
75+
ID string `json:"id"`
76+
Toggled bool `json:"toggled"`
77+
ButtonType string `json:"button_type"`
78+
ButtonValue string `json:"button_value"`
79+
Ecommerce *PostbackEcommerce `json:"ecommerce,omitempty"`
7780
}
7881

7982
type sendSneakPeekRequest struct {

customer/structures.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,18 @@ func (f *FilledFormField) GroupChooser() *FilledFormFieldGroupChooser {
495495

496496
// Postback represents postback data in LiveChat message event.
497497
type Postback struct {
498-
ID string `json:"id"`
499-
ThreadID string `json:"thread_id"`
500-
EventID string `json:"event_id"`
501-
Type string `json:"type,omitempty"`
502-
Value string `json:"value,omitempty"`
498+
ID string `json:"id"`
499+
ThreadID string `json:"thread_id"`
500+
EventID string `json:"event_id"`
501+
Type string `json:"type,omitempty"`
502+
Value string `json:"value,omitempty"`
503+
Ecommerce *PostbackEcommerce `json:"ecommerce,omitempty"`
504+
}
505+
506+
type PostbackEcommerce struct {
507+
ProductID string `json:"product_id,omitempty"`
508+
OptionID string `json:"option_id,omitempty"`
509+
Quantity int `json:"quantity,omitempty"`
503510
}
504511

505512
// Message represents LiveChat message event.
@@ -619,10 +626,11 @@ type RichMessage struct {
619626

620627
// RichMessageElement represents element of LiveChat rich message
621628
type RichMessageElement struct {
622-
Buttons []RichMessageButton `json:"buttons"`
623-
Title string `json:"title"`
624-
Subtitle string `json:"subtitle"`
625-
Image *RichMessageImage `json:"image,omitempty"`
629+
Buttons []RichMessageButton `json:"buttons"`
630+
Title string `json:"title"`
631+
Subtitle string `json:"subtitle"`
632+
Image *RichMessageImage `json:"image,omitempty"`
633+
Ecommerce *RichMessageEcommerce `json:"ecommerce,omitempty"`
626634
}
627635

628636
// RichMessageButton represents button in LiveChat rich message
@@ -649,6 +657,35 @@ type RichMessageImage struct {
649657
AlternativeText string `json:"alternative_text,omitempty"`
650658
}
651659

660+
// RichMessageEcommerce represents ecommerce element in LiveChat rich message
661+
type RichMessageEcommerce struct {
662+
ProductID string `json:"product_id"`
663+
Label string `json:"label"`
664+
ViewType string `json:"view_type"`
665+
Options []RichMessageEcommerceOption `json:"options,omitempty"`
666+
Addons []RichMessageEcommerceAddon `json:"addons,omitempty"`
667+
}
668+
669+
type RichMessageEcommerceOption struct {
670+
OptionID string `json:"option_id"`
671+
Label string `json:"label"`
672+
Price string `json:"price,omitempty"`
673+
RegularPrice string `json:"regular_price,omitempty"`
674+
Currency string `json:"currency,omitempty"`
675+
Color string `json:"color,omitempty"`
676+
ImageURL string `json:"image_url,omitempty"`
677+
ImageThumbnailURL string `json:"image_thumbnail_url,omitempty"`
678+
Available *bool `json:"available,omitempty"`
679+
Selected *bool `json:"selected,omitempty"`
680+
}
681+
682+
type RichMessageEcommerceAddon struct {
683+
AddonType string `json:"addon_type"`
684+
RangeFrom string `json:"range_from,omitempty"`
685+
RangeTo string `json:"range_to,omitempty"`
686+
Currency string `json:"currency,omitempty"`
687+
}
688+
652689
// RichMessage function converts Event object to RichMessage object if Event's Type is "rich_message".
653690
// If Type is different or Event is malformed, then it returns nil.
654691
func (e *Event) RichMessage() *RichMessage {

0 commit comments

Comments
 (0)