From 36882cad3e1f2cd29164f5eccc1ec373d7181b21 Mon Sep 17 00:00:00 2001 From: Paulo Bernardo Date: Tue, 22 Oct 2024 17:41:23 -0300 Subject: [PATCH] fix: handle empty flow data to not be sent as empty object in request --- handlers/facebookapp/facebookapp.go | 17 +++++++++++------ handlers/facebookapp/facebookapp_test.go | 7 +++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/handlers/facebookapp/facebookapp.go b/handlers/facebookapp/facebookapp.go index 19dad01a6..6fdcd7fbe 100644 --- a/handlers/facebookapp/facebookapp.go +++ b/handlers/facebookapp/facebookapp.go @@ -1452,6 +1452,11 @@ type wacAmountWithOffset struct { DiscountProgramName string `json:"discount_program_name,omitempty"` } +type wacFlowActionPayload struct { + Data map[string]interface{} `json:"data,omitempty"` + Screen string `json:"screen"` +} + func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) { // can't do anything without an access token accessToken := h.Server().Config().WhatsappAdminSystemUserToken @@ -1775,9 +1780,9 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) "flow_id": flowMessage.FlowID, "flow_cta": flowMessage.FlowCTA, "flow_action": "navigate", - "flow_action_payload": map[string]interface{}{ - "screen": flowMessage.FlowScreen, - "data": flowMessage.FlowData, + "flow_action_payload": wacFlowActionPayload{ + Screen: flowMessage.FlowScreen, + Data: flowMessage.FlowData, }, }, }, @@ -2220,9 +2225,9 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) "flow_id": flowMessage.FlowID, "flow_cta": flowMessage.FlowCTA, "flow_action": "navigate", - "flow_action_payload": map[string]interface{}{ - "screen": flowMessage.FlowScreen, - "data": flowMessage.FlowData, + "flow_action_payload": wacFlowActionPayload{ + Screen: flowMessage.FlowScreen, + Data: flowMessage.FlowData, }, }, }, diff --git a/handlers/facebookapp/facebookapp_test.go b/handlers/facebookapp/facebookapp_test.go index 2fa599942..f6c3bf1e9 100644 --- a/handlers/facebookapp/facebookapp_test.go +++ b/handlers/facebookapp/facebookapp_test.go @@ -860,6 +860,13 @@ var SendTestCasesWAC = []ChannelSendTestCase{ ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"flow","header":{"type":"text","text":"header text"},"body":{"text":"msg text"},"footer":{"text":"footer text"},"action":{"name":"flow","parameters":{"flow_action":"navigate","flow_action_payload":{"data":{"list":[1,2],"name":"John Doe"},"screen":"WELCOME_SCREEN"},"flow_cta":"Start Flow","flow_id":"29287124123","flow_message_version":"3","flow_token":"547deaf7-7620-4434-95b3-58675999c4b7","mode":"published"}}}}`, SendPrep: setSendURL}, + {Label: "Send Flow Message without flow data", + Metadata: json.RawMessage(`{"flow_message":{"flow_id": "29287124123", "flow_screen": "WELCOME_SCREEN", "flow_cta": "Start Flow", "flow_data": {}, "flow_mode":"published"},"footer":"footer text","header_text":"header text","header_type":"text","interaction_type":"flow_msg","text":"msgs text"}`), + Text: "msg text", URN: "whatsapp:250788123123", + Status: "W", ExternalID: "157b5e14568e8", + ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"flow","header":{"type":"text","text":"header text"},"body":{"text":"msg text"},"footer":{"text":"footer text"},"action":{"name":"flow","parameters":{"flow_action":"navigate","flow_action_payload":{"screen":"WELCOME_SCREEN"},"flow_cta":"Start Flow","flow_id":"29287124123","flow_message_version":"3","flow_token":"338ff339-5663-49ed-8ef6-384876655d1b","mode":"published"}}}}`, + SendPrep: setSendURL}, {Label: "Send Order Details Message", Metadata: json.RawMessage(`{"order_details_message":{"reference_id":"220788123125","total_amount":18200,"order":{"catalog_id":"14578923723","items":[{"retailer_id":"789236789","name":"item 1","amount":{"offset":100,"value":200},"quantity":2},{"retailer_id":"59016733","name":"item 2","amount":{"offset":100,"value":4000},"quantity":9,"sale_amount":{"offset":100,"value":2000}}],"subtotal":36400,"tax":{"value":500,"description":"tax description"},"shipping":{"value":900,"description":"shipping description"},"discount":{"value":1000,"description":"discount description","program_name":"discount program name"}},"payment_settings":{"type":"digital-goods","payment_link":"https://foo.bar","pix_config":{"key":"pix-key","key_type":"EMAIL","merchant_name":"merchant name","code":"pix-code"}}},"footer":"footer text","header_type":"media","interaction_type":"order_details","text":"msgs text"}`), Text: "msg text", URN: "whatsapp:250788123123",