Skip to content

Commit

Permalink
Support more WA templates components
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 18, 2024
1 parent f954958 commit 0771b60
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 17 deletions.
77 changes: 60 additions & 17 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package actions

import (
"strconv"
"strings"

"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
Expand Down Expand Up @@ -51,11 +54,18 @@ type SendMsgAction struct {
Topic flows.MsgTopic `json:"topic,omitempty" validate:"omitempty,msg_topic"`
}

// TemplateComponentVariables represents the variable for a component in the WA template
type TemplateComponentVariables struct {
UUID uuids.UUID `json:"uuid"`
Variables []string `json:"variables" engine:"localized,evaluated"`
}

// Templating represents the templating that should be used if possible
type Templating struct {
UUID uuids.UUID `json:"uuid" validate:"required,uuid4"`
Template *assets.TemplateReference `json:"template" validate:"required"`
Variables []string `json:"variables" engine:"localized,evaluated"`
UUID uuids.UUID `json:"uuid" validate:"required,uuid4"`
Template *assets.TemplateReference `json:"template" validate:"required"`
Variables []string `json:"variables" engine:"localized,evaluated"`
Params map[string]TemplateComponentVariables `json:"params"`
}

// LocalizationUUID gets the UUID which identifies this object for localization
Expand Down Expand Up @@ -126,27 +136,60 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
}

func getTemplatingMsg(action *SendMsgAction, run flows.Run, urn urns.URN, channelRef *assets.ChannelReference, templateTranslation *flows.TemplateTranslation, evaluatedAttachments []utils.Attachment, evaluatedQuickReplies []string, unsendableReason flows.UnsendableReason, logEvent flows.EventCallback) *flows.MsgOut {
evaluatedParams := make(map[string]TemplateComponentVariables)

if bodyComp, ok := action.Templating.Params["body"]; ok {
localizedVariables, _ := run.GetTextArray(uuids.UUID(action.Templating.UUID), "variables", action.Templating.Variables, nil)
evaluatedVariables := make([]string, len(localizedVariables))
for i, variable := range localizedVariables {
sub, err := run.EvaluateTemplate(variable)
if err != nil {
logEvent(events.NewError(err))

Check warning on line 147 in flows/actions/send_msg.go

View check run for this annotation

Codecov / codecov/patch

flows/actions/send_msg.go#L147

Added line #L147 was not covered by tests
}
evaluatedVariables[i] = sub
}

localizedVariables, _ := run.GetTextArray(uuids.UUID(action.Templating.UUID), "variables", action.Templating.Variables, nil)
evaluatedParams["body"] = TemplateComponentVariables{UUID: bodyComp.UUID, Variables: evaluatedVariables}
}

evaluatedVariables := make([]string, len(localizedVariables))
for i, variable := range localizedVariables {
sub, err := run.EvaluateTemplate(variable)
if err != nil {
logEvent(events.NewError(err))
for compKey, compVars := range action.Templating.Params {

var evaluatedComponentVariables []string
if strings.HasPrefix(compKey, "button.") {
qrIndex, err := strconv.Atoi(strings.TrimPrefix(compKey, "button."))
if err != nil {
logEvent(events.NewError(err))

Check warning on line 161 in flows/actions/send_msg.go

View check run for this annotation

Codecov / codecov/patch

flows/actions/send_msg.go#L161

Added line #L161 was not covered by tests
}
paramValue := evaluatedQuickReplies[qrIndex]
evaluatedComponentVariables = []string{paramValue}

} else {

localizedComponentVariables, _ := run.GetTextArray(uuids.UUID(compVars.UUID), "variables", compVars.Variables, nil)
evaluatedComponentVariables = make([]string, len(localizedComponentVariables))
for i, variable := range localizedComponentVariables {
sub, err := run.EvaluateTemplate(variable)
if err != nil {
logEvent(events.NewError(err))

Check warning on line 173 in flows/actions/send_msg.go

View check run for this annotation

Codecov / codecov/patch

flows/actions/send_msg.go#L173

Added line #L173 was not covered by tests
}
evaluatedComponentVariables[i] = sub
}
}
evaluatedVariables[i] = sub
evaluatedParams[compKey] = TemplateComponentVariables{UUID: compVars.UUID, Variables: evaluatedComponentVariables}

}

evaluatedText := templateTranslation.Substitute(evaluatedVariables)
evaluatedText := templateTranslation.Substitute(evaluatedParams["body"].Variables)

params := make(map[string][]flows.TemplateParam, 1)
templateTranslationParams := templateTranslation.Params()
params := make(map[string][]flows.TemplateParam, len(evaluatedParams))

// TODO add support for params in other components besides body
if len(evaluatedVariables) > 0 {
params["body"] = make([]flows.TemplateParam, len(evaluatedVariables))
for i, v := range evaluatedVariables {
params["body"][i] = flows.TemplateParam{Type: "text", Value: v}
for compKey, templateComponentVariables := range evaluatedParams {
if len(templateComponentVariables.Variables) > 0 {
params[compKey] = make([]flows.TemplateParam, len(templateComponentVariables.Variables))
for i, v := range templateComponentVariables.Variables {
params[compKey][i] = flows.TemplateParam{Type: templateTranslationParams[compKey][i].Type(), Value: v}
}
}
}

Expand Down
62 changes: 62 additions & 0 deletions flows/actions/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,68 @@
"content": "Hi there, it's time to get up!"
}
]
},
{
"uuid": "ce00c80e-991a-4c03-b373-3273c23ee042",
"name": "gender_update",
"translations": [
{
"channel": {
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"locale": "eng-US",
"content": "Hey {{1}}, your gender is saved as {{2}}.",
"params": {
"body": [
{
"type": "text"
},
{
"type": "text"
}
],
"header": [
{
"type": "image"
}
]
}
},
{
"channel": {
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"locale": "spa",
"content": "Hola, {{1}}, tu género está guardado como {{2}}.",
"params": {
"body": [
{
"type": "text"
},
{
"type": "text"
}
],
"header": [
{
"type": "image"
}
],
"button.0": [
{
"type": "text"
}
],
"button.1": [
{
"type": "text"
}
]
}
}
]
}
],
"topics": [
Expand Down
Loading

0 comments on commit 0771b60

Please sign in to comment.