From cc26883d1716f4318c9f044fc49382d8bf03060d Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Wed, 5 Jun 2024 17:48:28 -0500 Subject: [PATCH 1/2] Include header images from template messages as attachments on preview message --- flows/actions/send_msg.go | 16 +++-- flows/actions/testdata/_assets.json | 43 ++++++++++- flows/actions/testdata/send_msg.json | 104 +++++++++++++++++++++++++-- 3 files changed, 150 insertions(+), 13 deletions(-) diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index b16c983e9..eb859028a 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -11,6 +11,7 @@ import ( "github.com/nyaruka/goflow/assets" "github.com/nyaruka/goflow/flows" "github.com/nyaruka/goflow/flows/events" + "github.com/nyaruka/goflow/utils" ) func init() { @@ -150,27 +151,32 @@ func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef * } // the message we return is an approximate preview of what the channel will send using the template - var previewParts []string + var previewText []string + var previewAttachments []utils.Attachment var previewQRs []string for _, comp := range translation.Components() { previewContent := comp.Content() for key, index := range comp.Variables() { - previewContent = strings.ReplaceAll(previewContent, fmt.Sprintf("{{%s}}", key), variables[index].Value) + variable := variables[index] + if variable.Type == "text" { + previewContent = strings.ReplaceAll(previewContent, fmt.Sprintf("{{%s}}", key), variable.Value) + } else if variable.Type == "image" && variable.Value != "" { + previewAttachments = append(previewAttachments, utils.Attachment("image:"+variable.Value)) + } } if previewContent != "" { if comp.Type() == "header" || comp.Type() == "body" || comp.Type() == "footer" { - previewParts = append(previewParts, previewContent) + previewText = append(previewText, previewContent) } else if strings.HasPrefix(comp.Type(), "button/") { previewQRs = append(previewQRs, stringsx.TruncateEllipsis(previewContent, maxQuickReplyLength)) } } } - previewText := strings.Join(previewParts, "\n\n") locale := translation.Locale() templating := flows.NewMsgTemplating(a.Template, translation.Namespace(), components, variables) - return flows.NewMsgOut(urn, channelRef, previewText, nil, previewQRs, templating, flows.NilMsgTopic, locale, unsendableReason) + return flows.NewMsgOut(urn, channelRef, strings.Join(previewText, "\n\n"), previewAttachments, previewQRs, templating, flows.NilMsgTopic, locale, unsendableReason) } diff --git a/flows/actions/testdata/_assets.json b/flows/actions/testdata/_assets.json index 5383a7cc9..7a4d306ad 100644 --- a/flows/actions/testdata/_assets.json +++ b/flows/actions/testdata/_assets.json @@ -341,7 +341,7 @@ { "type": "header", "name": "header", - "content": "", + "content": "Update", "variables": {} }, { @@ -384,7 +384,7 @@ { "type": "header", "name": "header", - "content": "", + "content": "Actualizar", "variables": {} }, { @@ -424,6 +424,45 @@ ] } ] + }, + { + "uuid": "be68beff-1a5b-424b-815e-023cc53c1ddc", + "name": "cat_fact", + "translations": [ + { + "channel": { + "uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d", + "name": "My Android Phone" + }, + "locale": "eng-US", + "components": [ + { + "type": "header", + "name": "header", + "content": "", + "variables": { + "1": 0 + } + }, + { + "type": "body", + "name": "body", + "content": "{{1}}", + "variables": { + "1": 1 + } + } + ], + "variables": [ + { + "type": "image" + }, + { + "type": "text" + } + ] + } + ] } ], "topics": [ diff --git a/flows/actions/testdata/send_msg.json b/flows/actions/testdata/send_msg.json index 38cc235c2..e6525ad96 100644 --- a/flows/actions/testdata/send_msg.json +++ b/flows/actions/testdata/send_msg.json @@ -791,9 +791,6 @@ "type": "send_msg", "uuid": "ad154980-7bf7-4ab8-8728-545fd6378912", "text": "Hey Ryan Lewis, your gender is saved as boy.", - "attachments": [ - "http://example.com/red.jpg" - ], "quick_replies": [ "Yes", "No" @@ -833,7 +830,7 @@ "uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d", "name": "My Android Phone" }, - "text": "Hola, Ryan Lewis, tu género está guardado como niño.", + "text": "Actualizar\n\nHola, Ryan Lewis, tu género está guardado como niño.", "quick_replies": [ "Sip", "No" @@ -882,7 +879,6 @@ ], "templates": [ "Hey Ryan Lewis, your gender is saved as boy.", - "http://example.com/red.jpg", "Yes", "No", "@contact.name", @@ -896,7 +892,6 @@ ], "localizables": [ "Hey Ryan Lewis, your gender is saved as boy.", - "http://example.com/red.jpg", "Yes", "No", "@contact.name", @@ -917,5 +912,102 @@ "waiting_exits": [], "parent_refs": [] } + }, + { + "description": "Template with header image component", + "action": { + "type": "send_msg", + "uuid": "ad154980-7bf7-4ab8-8728-545fd6378912", + "text": "The Maine Coone is the only native American long haired breed.", + "attachments": [ + "image/jpeg:http://example.com/cat1.jpg" + ], + "template": { + "uuid": "be68beff-1a5b-424b-815e-023cc53c1ddc", + "name": "cat_fact" + }, + "template_variables": [ + "http://example.com/cat2.jpg", + "The first true cats came into existence about 12 million years ago and were the Proailurus." + ] + }, + "events": [ + { + "type": "msg_created", + "created_on": "2018-10-18T14:20:30.000123456Z", + "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", + "msg": { + "uuid": "9688d21d-95aa-4bed-afc7-f31b35731a3d", + "urn": "tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d&id=123", + "channel": { + "uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d", + "name": "My Android Phone" + }, + "text": "The first true cats came into existence about 12 million years ago and were the Proailurus.", + "attachments": [ + "image:http://example.com/cat2.jpg" + ], + "templating": { + "template": { + "uuid": "be68beff-1a5b-424b-815e-023cc53c1ddc", + "name": "cat_fact" + }, + "namespace": "", + "components": [ + { + "type": "header", + "name": "header", + "variables": { + "1": 0 + } + }, + { + "type": "body", + "name": "body", + "variables": { + "1": 1 + } + } + ], + "variables": [ + { + "type": "image", + "value": "http://example.com/cat2.jpg" + }, + { + "type": "text", + "value": "The first true cats came into existence about 12 million years ago and were the Proailurus." + } + ] + }, + "locale": "eng-US" + } + } + ], + "templates": [ + "The Maine Coone is the only native American long haired breed.", + "image/jpeg:http://example.com/cat1.jpg", + "http://example.com/cat2.jpg", + "The first true cats came into existence about 12 million years ago and were the Proailurus." + ], + "localizables": [ + "The Maine Coone is the only native American long haired breed.", + "image/jpeg:http://example.com/cat1.jpg", + "http://example.com/cat2.jpg", + "The first true cats came into existence about 12 million years ago and were the Proailurus." + ], + "inspection": { + "dependencies": [ + { + "uuid": "be68beff-1a5b-424b-815e-023cc53c1ddc", + "name": "cat_fact", + "type": "template" + } + ], + "issues": [], + "results": [], + "waiting_exits": [], + "parent_refs": [] + } } ] \ No newline at end of file From fdbdcc8a819e2ce6283c805d22c08c33fd5c3447 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 6 Jun 2024 09:40:18 -0500 Subject: [PATCH 2/2] Support template header components of type video or document as well --- flows/actions/send_msg.go | 15 +++++++++------ flows/actions/testdata/send_msg.json | 10 +++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index eb859028a..6a331a11e 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -128,14 +128,16 @@ func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef * evaluatedVariables[i] = v } - // cross-reference with asset to get variable types + // cross-reference with asset to get variable types and filter out invalid values variables := make([]*flows.TemplatingVariable, len(translation.Variables())) for i, v := range translation.Variables() { + // we pad out any missing variables with empty values + value := "" if i < len(evaluatedVariables) { - variables[i] = &flows.TemplatingVariable{Type: v.Type(), Value: evaluatedVariables[i]} - } else { - variables[i] = &flows.TemplatingVariable{Type: v.Type(), Value: ""} + value = evaluatedVariables[i] } + + variables[i] = &flows.TemplatingVariable{Type: v.Type(), Value: value} } // create a list of components that have variables @@ -159,10 +161,11 @@ func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef * previewContent := comp.Content() for key, index := range comp.Variables() { variable := variables[index] + if variable.Type == "text" { previewContent = strings.ReplaceAll(previewContent, fmt.Sprintf("{{%s}}", key), variable.Value) - } else if variable.Type == "image" && variable.Value != "" { - previewAttachments = append(previewAttachments, utils.Attachment("image:"+variable.Value)) + } else if variable.Type == "image" || variable.Type == "video" || variable.Type == "document" { + previewAttachments = append(previewAttachments, utils.Attachment(variable.Value)) } } diff --git a/flows/actions/testdata/send_msg.json b/flows/actions/testdata/send_msg.json index e6525ad96..cee49151d 100644 --- a/flows/actions/testdata/send_msg.json +++ b/flows/actions/testdata/send_msg.json @@ -927,7 +927,7 @@ "name": "cat_fact" }, "template_variables": [ - "http://example.com/cat2.jpg", + "image/jpeg:http://example.com/cat2.jpg", "The first true cats came into existence about 12 million years ago and were the Proailurus." ] }, @@ -945,7 +945,7 @@ }, "text": "The first true cats came into existence about 12 million years ago and were the Proailurus.", "attachments": [ - "image:http://example.com/cat2.jpg" + "image/jpeg:http://example.com/cat2.jpg" ], "templating": { "template": { @@ -972,7 +972,7 @@ "variables": [ { "type": "image", - "value": "http://example.com/cat2.jpg" + "value": "image/jpeg:http://example.com/cat2.jpg" }, { "type": "text", @@ -987,13 +987,13 @@ "templates": [ "The Maine Coone is the only native American long haired breed.", "image/jpeg:http://example.com/cat1.jpg", - "http://example.com/cat2.jpg", + "image/jpeg:http://example.com/cat2.jpg", "The first true cats came into existence about 12 million years ago and were the Proailurus." ], "localizables": [ "The Maine Coone is the only native American long haired breed.", "image/jpeg:http://example.com/cat1.jpg", - "http://example.com/cat2.jpg", + "image/jpeg:http://example.com/cat2.jpg", "The first true cats came into existence about 12 million years ago and were the Proailurus." ], "inspection": {