Skip to content

Commit

Permalink
Support template header components of type video or document as well
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jun 6, 2024
1 parent cc26883 commit fdbdcc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 9 additions & 6 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}

Expand Down
10 changes: 5 additions & 5 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
},
Expand All @@ -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": {
Expand All @@ -972,7 +972,7 @@
"variables": [
{
"type": "image",
"value": "http://example.com/cat2.jpg"
"value": "image/jpeg:http://example.com/cat2.jpg"
},
{
"type": "text",
Expand All @@ -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": {
Expand Down

0 comments on commit fdbdcc8

Please sign in to comment.