diff --git a/handlers/facebookapp/facebookapp.go b/handlers/facebookapp/facebookapp.go index 0cf9e6815..ff8eafe3e 100644 --- a/handlers/facebookapp/facebookapp.go +++ b/handlers/facebookapp/facebookapp.go @@ -1626,13 +1626,13 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) } else { if i < (len(msgParts) + len(msg.Attachments()) - 1) { + payload.Type = "text" if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") { text := wacText{} text.PreviewURL = true text.Body = msgParts[i-len(msg.Attachments())] payload.Text = &text } else { - payload.Type = "text" payload.Text = &wacText{Body: msgParts[i-len(msg.Attachments())]} } } else { @@ -1973,7 +1973,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) payload.Document = &media } //end - } else { + } else { // have attachment if len(qrs) > 0 || len(msg.ListMessage().ListItems) > 0 { payload.Type = "interactive" // We can use buttons @@ -2140,7 +2140,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) } else { return nil, fmt.Errorf("too many quick replies WAC supports only up to 10 quick replies") } - } else if msg.InteractionType() == "location" { + } else if msg.InteractionType() == "location" { // Unreachable due to else if sending only the attachment interactive := wacInteractive[map[string]any]{Type: "location_request_message", Body: struct { Text string "json:\"text\"" }{Text: msgParts[i-len(msg.Attachments())]}, Action: &struct { @@ -2154,7 +2154,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) }{Name: "send_location"}} payload.Interactive = &interactive - } else if msg.InteractionType() == "cta_url" { + } else if msg.InteractionType() == "cta_url" { // Unreachable due to else if sending only the attachment if ctaMessage := msg.CTAMessage(); ctaMessage != nil { interactive := wacInteractive[map[string]any]{ Type: "cta_url", @@ -2195,7 +2195,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) } payload.Interactive = &interactive } - } else if msg.InteractionType() == "flow_msg" { + } else if msg.InteractionType() == "flow_msg" { // Unreachable due to else if sending only the attachment if flowMessage := msg.FlowMessage(); flowMessage != nil { interactive := wacInteractive[map[string]any]{ Type: "flow", diff --git a/handlers/facebookapp/facebookapp_test.go b/handlers/facebookapp/facebookapp_test.go index a3156f9a9..71d934842 100644 --- a/handlers/facebookapp/facebookapp_test.go +++ b/handlers/facebookapp/facebookapp_test.go @@ -716,10 +716,10 @@ var SendTestCasesWAC = []ChannelSendTestCase{ RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","body":{"text":"Interactive Button Msg"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`, SendPrep: setSendURL}, {Label: "Interactive Button Message Send with Slashes", - Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"\\\\BUTTON1", "/BUTTON2"}, + Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"\\\\BUTTON1", "/BUTTON2", "\\/BUTTON3"}, Status: "W", ExternalID: "157b5e14568e8", ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, - RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","body":{"text":"Interactive Button Msg"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"\\BUTTON1"}},{"type":"reply","reply":{"id":"1","title":"/BUTTON2"}}]}}}`, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","body":{"text":"Interactive Button Msg"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"\\BUTTON1"}},{"type":"reply","reply":{"id":"1","title":"/BUTTON2"}},{"type":"reply","reply":{"id":"2","title":"/BUTTON3"}}]}}}`, SendPrep: setSendURL}, {Label: "Interactive List Message Send", Text: "Interactive List Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"ROW1", "ROW2", "ROW3", "ROW4"}, @@ -839,6 +839,13 @@ var SendTestCasesWAC = []ChannelSendTestCase{ ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"product_list","body":{"text":"Catalog Body Msg"},"action":{"sections":[{"title":"product1","product_items":[{"product_retailer_id":"p1"}]},{"title":"long product name greate","product_items":[{"product_retailer_id":"p2"}]}],"catalog_id":"c4t4l0g-1D","name":"View Products"}}}`, SendPrep: setSendURL}, + {Label: "Catalog Message Send 2 products - With Header - With Footer", + Metadata: json.RawMessage(`{"header": "header text", "footer": "footer text", "body":"Catalog Body Msg", "products": [{"Product": "product1","ProductRetailerIDs":["p1"]},{"Product": "long product name greate than 24","ProductRetailerIDs":["p2"]}], "action": "View Products", "send_catalog":false}`), + Text: "Catalog Msg", 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":"product_list","header":{"type":"text","text":"header text"},"body":{"text":"Catalog Body Msg"},"footer":{"text":"footer text"},"action":{"sections":[{"title":"product1","product_items":[{"product_retailer_id":"p1"}]},{"title":"long product name greate","product_items":[{"product_retailer_id":"p2"}]}],"catalog_id":"c4t4l0g-1D","name":"View Products"}}}`, + SendPrep: setSendURL}, {Label: "Send Product Catalog", Metadata: json.RawMessage(`{"body":"Catalog Body Msg", "action": "View Products", "send_catalog":true}`), Text: "Catalog Msg", URN: "whatsapp:250788123123", @@ -867,7 +874,7 @@ 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":{"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", + {Label: "Send Order Details Message - With Attachment", 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", Status: "W", ExternalID: "157b5e14568e8", @@ -875,6 +882,13 @@ var SendTestCasesWAC = []ChannelSendTestCase{ ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"order_details","header":{"type":"image","image":{"link":"https://foo.bar/image.jpg"}},"body":{"text":"msg text"},"footer":{"text":"footer text"},"action":{"name":"review_and_pay","parameters":{"currency":"BRL","order":{"catalog_id":"c4t4l0g-1D","discount":{"description":"discount description","discount_program_name":"discount program name","offset":100,"value":1000},"items":[{"amount":{"offset":100,"value":200},"name":"item 1","quantity":2,"retailer_id":"789236789"},{"amount":{"offset":100,"value":4000},"name":"item 2","quantity":9,"retailer_id":"59016733","sale_amount":{"offset":100,"value":2000}}],"shipping":{"description":"shipping description","offset":100,"value":900},"status":"pending","subtotal":{"offset":100,"value":36400},"tax":{"description":"tax description","offset":100,"value":500}},"payment_settings":[{"payment_link":{"uri":"https://foo.bar"},"type":"payment_link"},{"pix_dynamic_code":{"code":"pix-code","key":"pix-key","key_type":"EMAIL","merchant_name":"merchant name"},"type":"pix_dynamic_code"}],"payment_type":"br","reference_id":"220788123125","total_amount":{"offset":100,"value":18200},"type":"digital-goods"}}}}`, 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","interaction_type":"order_details","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":"order_details","body":{"text":"msg text"},"footer":{"text":"footer text"},"action":{"name":"review_and_pay","parameters":{"currency":"BRL","order":{"catalog_id":"c4t4l0g-1D","discount":{"description":"discount description","discount_program_name":"discount program name","offset":100,"value":1000},"items":[{"amount":{"offset":100,"value":200},"name":"item 1","quantity":2,"retailer_id":"789236789"},{"amount":{"offset":100,"value":4000},"name":"item 2","quantity":9,"retailer_id":"59016733","sale_amount":{"offset":100,"value":2000}}],"shipping":{"description":"shipping description","offset":100,"value":900},"status":"pending","subtotal":{"offset":100,"value":36400},"tax":{"description":"tax description","offset":100,"value":500}},"payment_settings":[{"payment_link":{"uri":"https://foo.bar"},"type":"payment_link"},{"pix_dynamic_code":{"code":"pix-code","key":"pix-key","key_type":"EMAIL","merchant_name":"merchant name"},"type":"pix_dynamic_code"}],"payment_type":"br","reference_id":"220788123125","total_amount":{"offset":100,"value":18200},"type":"digital-goods"}}}}`, + SendPrep: setSendURL}, {Label: "Message Template - Order Details", Text: "templated message", URN: "whatsapp:250788123123", @@ -891,6 +905,194 @@ var SendTestCasesWAC = []ChannelSendTestCase{ ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 200, RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]},{"type":"button","sub_type":"url","index":0,"parameters":[{"type":"text","text":"first param"}]}]}}`, SendPrep: setSendURL}, + {Label: "Plain Send - Link preview on a long message", + Text: "https://link.com Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean m", + URN: "whatsapp:250788123123", Path: "/12345_ID/messages", + Status: "W", ExternalID: "157b5e14568e8", + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"text","text":{"body":"https://link.com Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula","preview_url":true}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"text","text":{"body":"eget dolor. Aenean m"}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, + {Label: "Plain Send - Long message", + Text: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque.", + URN: "whatsapp:250788123123", Path: "/12345_ID/messages", + Status: "W", ExternalID: "157b5e14568e8", + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"text","text":{"body":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean"}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"text","text":{"body":"massa. Cum sociis natoque."}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, + {Label: "Interactive Button Message Send - With Footer - With Header", + Metadata: json.RawMessage(`{"footer":"footer text","header_text":"header text","header_type":"text"}`), + Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"BUTTON1"}, + Status: "W", ExternalID: "157b5e14568e8", + ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","header":{"type":"text","text":"header text"},"body":{"text":"Interactive Button Msg"},"footer":{"text":"footer text"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`, + SendPrep: setSendURL}, + {Label: "Interactive Button Message Send - With Attachment - With Footer", + Metadata: json.RawMessage(`{"footer":"footer text"}`), + Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"BUTTON1"}, + Status: "W", ExternalID: "157b5e14568e8", + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","header":{"type":"image","image":{"link":"https://foo.bar/image.jpg"}},"body":{"text":"Interactive Button Msg"},"footer":{"text":"footer text"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`, + SendPrep: setSendURL}, + {Label: "Interactive Button Message Send - With Attachment Video - With Footer", + Metadata: json.RawMessage(`{"footer":"footer text"}`), + Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"BUTTON1"}, + Status: "W", ExternalID: "157b5e14568e8", + Attachments: []string{"video/mp4:https://foo.bar/video.mp4"}, + ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","header":{"type":"video","video":{"link":"https://foo.bar/video.mp4"}},"body":{"text":"Interactive Button Msg"},"footer":{"text":"footer text"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`, + SendPrep: setSendURL}, + {Label: "Interactive Button Message Send - With unknown attachment - With Footer", + Metadata: json.RawMessage(`{"footer":"footer text"}`), + Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"BUTTON1"}, + Status: "W", ExternalID: "157b5e14568e8", + Attachments: []string{"unknown/unknown:https://foo.bar/video.unknown"}, + ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","body":{"text":"Interactive Button Msg"},"footer":{"text":"footer text"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`, + SendPrep: setSendURL}, + {Label: "Interactive List Message Send - With list items - With Header - With Footer", + Metadata: json.RawMessage(`{"footer":"footer text","header_text":"header text","header_type":"text","interaction_type":"list","list_message":{"button_text":"button text","list_items":[{"uuid":"123","title":"title1","description":"description1"},{"uuid":"456","title":"title2"}]}}`), + Text: "Interactive List Msg", URN: "whatsapp:250788123123", + Status: "W", ExternalID: "157b5e14568e8", TextLanguage: "en-US", + ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"list","header":{"type":"text","text":"header text"},"body":{"text":"Interactive List Msg"},"footer":{"text":"footer text"},"action":{"button":"button text","sections":[{"rows":[{"id":"123","title":"title1","description":"description1"},{"id":"456","title":"title2"}]}]}}}`, + SendPrep: setSendURL}, + {Label: "Interactive List Message Send - With list items - With Attachments - With Footer", + Metadata: json.RawMessage(`{"footer":"footer text","interaction_type":"list","list_message":{"button_text":"button text","list_items":[{"uuid":"123","title":"title1","description":"description1"},{"uuid":"456","title":"title2"}]}}`), + Text: "Interactive List Msg", URN: "whatsapp:250788123123", + Status: "W", ExternalID: "157b5e14568e8", TextLanguage: "en-US", + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"image","image":{"link":"https://foo.bar/image.jpg"}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"list","body":{"text":"Interactive List Msg"},"footer":{"text":"footer text"},"action":{"button":"button text","sections":[{"rows":[{"id":"123","title":"title1","description":"description1"},{"id":"456","title":"title2"}]}]}}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, + {Label: "Interactive Location Request", + Metadata: json.RawMessage(`{"interaction_type":"location"}`), + Text: "Interactive Location Request", URN: "whatsapp:250788123123", + Status: "W", ExternalID: "157b5e14568e8", TextLanguage: "en-US", + ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201, + RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"location_request_message","body":{"text":"Interactive Location Request"},"action":{"name":"send_location"}}}`, + SendPrep: setSendURL}, + {Label: "[VERIFY] Interactive Location Request - With attachment - Sending only the attachment (is this ok? shouldn't the location request be a priority?)", // TODO: Verify, is Location + Attachment a valid combination? I believe that the Send WhatsApp Message card does not allow this + Metadata: json.RawMessage(`{"interaction_type":"location"}`), + Text: "Interactive Location Request With Attachment", URN: "whatsapp:250788123123", + Status: "W", ExternalID: "157b5e14568e8", TextLanguage: "en-US", + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"image","image":{"link":"https://foo.bar/image.jpg","caption":"Interactive Location Request With Attachment"}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, + {Label: "[VERIFY] Send CTA Url - With Attachment - Sending only the attachment (is this ok too?)", // TODO: Verify, is CTA + Attachment a valid combination? I believe that the Send WhatsApp Message card does not allow this + Metadata: json.RawMessage(`{"cta_message":{"display_text":"link button","url":"https://foo.bar"},"footer":"footer text","header_text":"header text","header_type":"text","interaction_type":"cta_url","text":"msgs text"}`), + Text: "msg text", URN: "whatsapp:250788123123", + Status: "W", ExternalID: "157b5e14568e8", + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"image","image":{"link":"https://foo.bar/image.jpg","caption":"msg text"}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, + {Label: "Send Flow Message - With Attachment - Sending only the attachment (is this ok too?)", // TODO: Verify, is Flow + Attachment a valid combination? I believe that the Send WhatsApp Message card does not allow this + Metadata: json.RawMessage(`{"flow_message":{"flow_id": "29287124123", "flow_screen": "WELCOME_SCREEN", "flow_cta": "Start Flow", "flow_data": {"name": "John Doe", "list": [1, 2]},"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", + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"image","image":{"link":"https://foo.bar/image.jpg","caption":"msg text"}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, + {Label: "Media Message Template Send - Unknown attachment type", + Text: "Media Message Msg", URN: "whatsapp:250788123123", + Metadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "language": "eng", "country": "US", "variables": ["Chef", "tomorrow"]}}`), + Attachments: []string{"unknown/unknown:https://foo.bar/unknown.unknown"}, + Error: "unknown attachment mime type: unknown", + SendPrep: setSendURL}, + {Label: "Interactive Button Message Send with too many replies", + Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"}, + Error: "too many quick replies WAC supports only up to 10 quick replies", + SendPrep: setSendURL}, + {Label: "Interactive Button Message Send with too many replies and attachments", // TODO: attachment is sent, but the list message fails, is this correct? + Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"}, + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + Error: "too many quick replies WAC supports only up to 10 quick replies", + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"image","image":{"link":"https://foo.bar/image.jpg"}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, {Label: "Mesage without text and with quick replies and attachments should not be sent", Text: "", URN: "whatsapp:250788123123", QuickReplies: []string{"Yes", "No"}, @@ -905,43 +1107,102 @@ var CachedSendTestCasesWAC = []ChannelSendTestCase{ Status: "W", ExternalID: "157b5e14568e8", Attachments: []string{`application/pdf:https://foo.bar/document.pdf`}, Responses: map[MockedRequest]MockedResponse{ - MockedRequest{ + { Method: "POST", Path: "/12345_ID/media", BodyContains: "media bytes", - }: MockedResponse{ + }: { Status: 201, Body: `{"id":"157b5e14568e8"}`, }, - MockedRequest{ + { Method: "POST", Path: "/12345_ID/messages", Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","header":{"type":"document","document":{"id":"157b5e14568e8","filename":"document.pdf"}},"body":{"text":"Interactive Button Msg"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`, - }: MockedResponse{ + }: { Status: 201, Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, }, }, SendPrep: setSendURL}, + {Label: "Media Message Template Send - Image with cached attachment", + Text: "Media Message Msg", URN: "whatsapp:250788123123", + Status: "W", + Metadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "language": "eng", "country": "US", "variables": ["Chef", "tomorrow"]}}`), + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/media", + BodyContains: "media bytes", + }: { + Status: 201, + Body: `{"id":"157b5e14568e8"}`, + }, + { + Method: "POST", + Path: "/12345_ID/messages", + BodyContains: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en_US"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]},{"type":"header","parameters":[{"type":"image","image":{"id":"157b5e14568e8"}}]}]}}`, + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, + {Label: "Media Message Template Send - Image with cached attachment failing to upload", + Text: "Media Message Msg", URN: "whatsapp:250788123123", + Status: "E", + Metadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "language": "eng", "country": "US", "variables": ["Chef", "tomorrow"]}}`), + Attachments: []string{"image/jpeg:https://foo.bar/image2.jpg"}, + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/media", + BodyContains: "media bytes", + }: { + Status: 400, + Body: `error`, + }, + }, + SendPrep: setSendURL}, {Label: "Media Message Template Send - Document (Cached)", Text: "Media Message Msg", URN: "whatsapp:250788123123", Status: "W", ExternalID: "157b5e14568e8", Attachments: []string{"application/pdf:https://foo.bar/document2.pdf"}, Metadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "language": "eng", "country": "US", "variables": ["Chef", "tomorrow"]}}`), Responses: map[MockedRequest]MockedResponse{ - MockedRequest{ + { Method: "POST", Path: "/12345_ID/media", BodyContains: "media bytes", - }: MockedResponse{ + }: { Status: 201, Body: `{"id":"268c6f24679f9"}`, }, - MockedRequest{ + { Method: "POST", Path: "/12345_ID/messages", Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en_US"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]},{"type":"header","parameters":[{"type":"document","document":{"id":"268c6f24679f9","filename":"document2.pdf"}}]}]}}`, - }: MockedResponse{ + }: { + Status: 201, + Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, + }, + }, + SendPrep: setSendURL}, +} + +var FailingCachedSendTestCasesWAC = []ChannelSendTestCase{ + {Label: "Media Message Template Send - Image with failing cached attachment should send the default attachment URL", + Text: "Media Message Msg", URN: "whatsapp:250788123123", + Status: "W", ExternalID: "157b5e14568e8", + Metadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "language": "eng", "country": "US", "variables": ["Chef", "tomorrow"]}}`), + Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, + Responses: map[MockedRequest]MockedResponse{ + { + Method: "POST", + Path: "/12345_ID/messages", + BodyContains: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en_US"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]},{"type":"header","parameters":[{"type":"image","image":{"link":"`, + }: { Status: 201, Body: `{ "messages": [{"id": "157b5e14568e8"}] }`, }, @@ -972,9 +1233,17 @@ func TestSending(t *testing.T) { res.WriteHeader(200) res.Write([]byte("media bytes")) })) + + failingMediaServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + defer req.Body.Close() + res.WriteHeader(400) + res.Write([]byte("error")) + })) defer mediaServer.Close() CachedSendTestCasesWAC := mockAttachmentURLs(mediaServer, CachedSendTestCasesWAC) + FailingCachedSendTestCasesWAC := mockAttachmentURLs(failingMediaServer, FailingCachedSendTestCasesWAC) SendTestCasesWAC = append(SendTestCasesWAC, CachedSendTestCasesWAC...) + SendTestCasesWAC = append(SendTestCasesWAC, FailingCachedSendTestCasesWAC...) // shorter max msg length for testing maxMsgLengthFBA = 100