From 08e87fccd369942f73c0410637931faee0c43afe Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Thu, 6 Jun 2024 16:38:57 +0200 Subject: [PATCH] Use split attachments for templates media attachment for consistency with messages attachments --- handlers/meta/whatsapp/templates.go | 30 +++++--- handlers/meta/whatsapp/templates_test.go | 93 ++++++++++++++++++++---- 2 files changed, 95 insertions(+), 28 deletions(-) diff --git a/handlers/meta/whatsapp/templates.go b/handlers/meta/whatsapp/templates.go index 7b889814d..b22d00379 100644 --- a/handlers/meta/whatsapp/templates.go +++ b/handlers/meta/whatsapp/templates.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/nyaruka/courier" + "github.com/nyaruka/courier/handlers" "golang.org/x/exp/maps" ) @@ -30,18 +31,23 @@ func GetTemplatePayload(templating *courier.Templating) *Template { component = &Component{Type: comp.Type} for _, p := range compParams { - if p.Type == "image" { - component.Params = append(component.Params, &Param{Type: p.Type, Image: &struct { - Link string "json:\"link,omitempty\"" - }{Link: p.Value}}) - } else if p.Type == "video" { - component.Params = append(component.Params, &Param{Type: p.Type, Video: &struct { - Link string "json:\"link,omitempty\"" - }{Link: p.Value}}) - } else if p.Type == "document" { - component.Params = append(component.Params, &Param{Type: p.Type, Document: &struct { - Link string "json:\"link,omitempty\"" - }{Link: p.Value}}) + if p.Type != "text" { + attType, attURL := handlers.SplitAttachment(p.Value) + attType = strings.Split(attType, "/")[0] + + if attType == "image" { + component.Params = append(component.Params, &Param{Type: "image", Image: &struct { + Link string "json:\"link,omitempty\"" + }{Link: attURL}}) + } else if attType == "video" { + component.Params = append(component.Params, &Param{Type: "video", Video: &struct { + Link string "json:\"link,omitempty\"" + }{Link: attURL}}) + } else if attType == "application" { + component.Params = append(component.Params, &Param{Type: "document", Document: &struct { + Link string "json:\"link,omitempty\"" + }{Link: attURL}}) + } } else { component.Params = append(component.Params, &Param{Type: p.Type, Text: p.Value}) } diff --git a/handlers/meta/whatsapp/templates_test.go b/handlers/meta/whatsapp/templates_test.go index c45ccaaf9..b51af1892 100644 --- a/handlers/meta/whatsapp/templates_test.go +++ b/handlers/meta/whatsapp/templates_test.go @@ -64,35 +64,96 @@ func TestGetTemplatePayload(t *testing.T) { "language": "en", "components": [ { - "type": "button/quick_reply", - "name": "button.0", - "variables": {"1": 0, "2": 1} + "type": "header", + "name": "header", + "variables": {"1": 0} }, { - "type": "button/quick_reply", - "name": "button.1", - "variables": {"1": 2} + "type": "body", + "name": "body", + "variables": {"1": 1, "2": 2} + } + ], + "variables": [ + {"type": "image", "value": "image/jpeg:http://example.com/cat2.jpg"}, + {"type": "text", "value": "Hello"}, + {"type": "text", "value": "Bob"} + ] + }`, + expected: &whatsapp.Template{ + Name: "Update", + Language: &whatsapp.Language{Policy: "deterministic", Code: "en"}, + Components: []*whatsapp.Component{ + {Type: "header", Params: []*whatsapp.Param{{Type: "image", Image: &struct { + Link string "json:\"link,omitempty\"" + }{Link: "http://example.com/cat2.jpg"}}}}, + {Type: "body", Params: []*whatsapp.Param{{Type: "text", Text: "Hello"}, {Type: "text", Text: "Bob"}}}, + }, + }, + }, + { + templating: `{ + "template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"}, + "language": "en", + "components": [ + { + "type": "header", + "name": "header", + "variables": {"1": 0} }, { - "type": "button/url", - "name": "button.2", - "variables": {"1": 3} + "type": "body", + "name": "body", + "variables": {"1": 1, "2": 2} } ], "variables": [ - {"type": "text", "value": "Yes"}, - {"type": "text", "value": "Bob"}, - {"type": "text", "value": "No"}, - {"type": "text", "value": "id0023"} + {"type": "video", "value": "video/mp4:http://example.com/video.mp4"}, + {"type": "text", "value": "Hello"}, + {"type": "text", "value": "Bob"} + ] + }`, + expected: &whatsapp.Template{ + Name: "Update", + Language: &whatsapp.Language{Policy: "deterministic", Code: "en"}, + Components: []*whatsapp.Component{ + {Type: "header", Params: []*whatsapp.Param{{Type: "video", Video: &struct { + Link string "json:\"link,omitempty\"" + }{Link: "http://example.com/video.mp4"}}}}, + {Type: "body", Params: []*whatsapp.Param{{Type: "text", Text: "Hello"}, {Type: "text", Text: "Bob"}}}, + }, + }, + }, + { + templating: `{ + "template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"}, + "language": "en", + "components": [ + { + "type": "header", + "name": "header", + "variables": {"1": 0} + }, + { + "type": "body", + "name": "body", + "variables": {"1": 1, "2": 2} + } + ], + "variables": [ + {"type": "document", "value": "application/pdf:http://example.com/doc.pdf"}, + {"type": "text", "value": "Hello"}, + {"type": "text", "value": "Bob"} ] }`, expected: &whatsapp.Template{ Name: "Update", Language: &whatsapp.Language{Policy: "deterministic", Code: "en"}, Components: []*whatsapp.Component{ - {Type: "button", SubType: "quick_reply", Index: "0", Params: []*whatsapp.Param{{Type: "payload", Payload: "Yes"}, {Type: "payload", Payload: "Bob"}}}, - {Type: "button", SubType: "quick_reply", Index: "1", Params: []*whatsapp.Param{{Type: "payload", Payload: "No"}}}, - {Type: "button", SubType: "url", Index: "2", Params: []*whatsapp.Param{{Type: "text", Text: "id0023"}}}, + {Type: "header", Params: []*whatsapp.Param{{Type: "document", Document: &struct { + Link string "json:\"link,omitempty\"" + }{Link: "http://example.com/doc.pdf"}}}}, + {Type: "body", Params: []*whatsapp.Param{{Type: "text", Text: "Hello"}, {Type: "text", Text: "Bob"}}}, }, }, },