Skip to content

Commit

Permalink
Merge pull request #139 from weni-ai/fix/whatsapp-flows-empty-flow-data
Browse files Browse the repository at this point in the history
fix: handle empty flow data to not be sent as empty object in request
  • Loading branch information
rasoro authored Oct 23, 2024
2 parents 938a868 + 36882ca commit 304c9a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
17 changes: 11 additions & 6 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -2219,9 +2224,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,
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 304c9a9

Please sign in to comment.