diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index 411b6f6c5..99e026a53 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -56,10 +56,10 @@ type SendMsgAction struct { // 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"` - Params map[string][]flows.TemplateParam `json:"params"` + 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]flows.ComponentVariables `json:"params"` } // LocalizationUUID gets the UUID which identifies this object for localization @@ -142,40 +142,34 @@ func getTemplatingMsg(action *SendMsgAction, run flows.Run, urn urns.URN, channe } evaluatedText := templateTranslation.Substitute(evaluatedVariables) - evaluatedParams := make(map[string][]flows.TemplateParam) - for compKey, compParams := range action.Templating.Params { - compVariables := make([]flows.TemplateParam, len(compParams)) - for i, templateParam := range compParams { - var paramValue string - var err error - if strings.HasPrefix(compKey, "button.") { + evaluatedParams := make(map[string]flows.ComponentVariables) + for compKey, compVars := range action.Templating.Params { - qrIndex, err := strconv.Atoi(strings.TrimPrefix(compKey, "button.")) - if err != nil { - logEvent(events.NewError(err)) - } - paramValue = evaluatedQuickReplies[qrIndex] - } else if templateParam.Type() != "text" { - paramValue = "" - for _, att := range evaluatedAttachments { - attType := strings.Split(att.ContentType(), "/")[0] - if templateParam.Type() == attType { - paramValue = att.URL() - break - } - } - } else { - localizedParamVariables, _ := run.GetTextArray(uuids.UUID(templateParam.UUID()), "value", []string{templateParam.Value()}, nil) - paramValue, err = run.EvaluateTemplate(localizedParamVariables[0]) + var evaluatedComponentVariables []string + if strings.HasPrefix(compKey, "button.") { + qrIndex, err := strconv.Atoi(strings.TrimPrefix(compKey, "button.")) + if err != nil { + logEvent(events.NewError(err)) + } + 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)) } + evaluatedComponentVariables[i] = sub } - evaluatedParam := flows.NewTemplateParam(templateParam.Type(), templateParam.UUID(), paramValue) - compVariables[i] = evaluatedParam } - evaluatedParams[compKey] = compVariables + evaluatedParams[compKey] = flows.ComponentVariables{UUID: compVars.UUID, Variables: evaluatedComponentVariables} + } + templating := flows.NewMsgTemplating(action.Templating.Template, evaluatedVariables, templateTranslation.Namespace(), evaluatedParams) locale := templateTranslation.Locale() diff --git a/flows/actions/testdata/send_msg.json b/flows/actions/testdata/send_msg.json index 4df17dfa1..5cd314a60 100644 --- a/flows/actions/testdata/send_msg.json +++ b/flows/actions/testdata/send_msg.json @@ -421,18 +421,10 @@ "templating": { "uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785", "params": { - "body": [ - { - "type": "text", - "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", - "value": "@contact.name" - }, - { - "type": "text", - "uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b", - "value": "boy" - } - ] + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "variables": ["@contact.name", "boy"] + } }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", @@ -460,18 +452,10 @@ "text": "Hi Ryan Lewis, who's an excellent boy?", "templating": { "params": { - "body": [ - { - "type": "text", - "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", - "value": "Ryan Lewis" - }, - { - "type": "text", - "uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b", - "value": "boy" - } - ] + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "variables": ["Ryan Lewis", "boy"] + } }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", @@ -521,18 +505,10 @@ "templating": { "uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785", "params": { - "body": [ - { - "type": "text", - "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", - "value": "@contact.name" - }, - { - "type": "text", - "uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b", - "value": "boy" - } - ] + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "variables": ["@contact.name", "boy"] + } }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", @@ -552,11 +528,8 @@ "niño" ] }, - "f383e94d-0598-4fd9-81d7-f0aea397d63b": { - "value": ["niño"] - }, "74d59988-18c0-4919-9cb4-9f9e3847ed50": { - "value": ["@contact.name"] + "variables": ["@contact.name", "niño"] } } }, @@ -575,18 +548,10 @@ "text": "Hola Ryan Lewis, quien es un niño excelente?", "templating": { "params": { - "body": [ - { - "type": "text", - "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", - "value": "Ryan Lewis" - }, - { - "type": "text", - "uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b", - "value": "niño" - } - ] + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "variables": ["Ryan Lewis", "niño"] + } }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", @@ -854,39 +819,22 @@ "templating": { "uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785", "params": { - "header": [ - { - "type": "image", - "uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1", - "value": "no_used" - } - ], - "body": [ - { - "type": "text", + "header":{ + "uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1", + "variables": ["http://example.com/red.jpg"] + }, + "body": { "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", - "value": "@contact.name" - }, - { - "type": "text", - "uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b", - "value": "boy" - } - ], - "button.0": [ - { - "type": "text", - "uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1", - "value": "no_used" - } - ], - "button.1": [ - { - "type": "text", - "uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea", - "value": "no_used" - } - ] + "variables": ["@contact.name", "boy"] + }, + "button.0": { + "uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1", + "variables": ["no_used"] + }, + "button.1": { + "uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea", + "variables": ["no_used"] + } }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", @@ -918,11 +866,16 @@ "niño" ] }, + "98788f6d-775e-4c6c-a11c-d6169c3551c1": { + "variables": [ + "image:http://example.com/rojo.jpg" + ] + }, "f383e94d-0598-4fd9-81d7-f0aea397d63b": { - "value": ["niño"] + "variables": ["niño"] }, "74d59988-18c0-4919-9cb4-9f9e3847ed50": { - "value": ["@contact.name"] + "variables": ["@contact.name", "niño"] } } }, @@ -948,40 +901,22 @@ ], "templating": { "params": { - "body": [ - { - "type": "text", - "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", - "value": "Ryan Lewis" - }, - { - "type": "text", - "uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b", - "value": "niño" - } - - ], - "header": [ - { - "type": "image", - "uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1", - "value": "http://example.com/rojo.jpg" - } - ], - "button.0": [ - { - "type": "text", - "uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1", - "value": "Si" - } - ], - "button.1": [ - { - "type": "text", - "uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea", - "value": "No" - } - ] + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "variables": ["Ryan Lewis", "niño"] + }, + "header": { + "uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1", + "variables": ["image:http://example.com/rojo.jpg"] + }, + "button.0": { + "uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1", + "variables": ["Si"] + }, + "button.1": { + "uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea", + "variables": ["No"] + } }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", diff --git a/flows/msg.go b/flows/msg.go index ce89b6d23..d4330025e 100644 --- a/flows/msg.go +++ b/flows/msg.go @@ -168,40 +168,17 @@ func (m *MsgOut) Locale() i18n.Locale { return m.Locale_ } // UnsendableReason returns the reason this message can't be sent (if any) func (m *MsgOut) UnsendableReason() UnsendableReason { return m.UnsendableReason_ } -// TemplateParamUUID is the UUID of a param -type TemplateParamUUID uuids.UUID - -// TemplateParam represents a single parameter -type TemplateParam struct { - Type_ string `json:"type"` - UUID_ TemplateParamUUID `json:"uuid"` - Value_ string `json:"value"` -} - -// Type returns the type for this parameter -func (t *TemplateParam) Type() string { return t.Type_ } - -// UUID returns the UUID for this parameter -func (t *TemplateParam) UUID() TemplateParamUUID { return t.UUID_ } - -// Value returns the value for this parameter -func (t *TemplateParam) Value() string { return t.Value_ } - -// NewMsgTemplating creates and returns a new msg template -func NewTemplateParam(paramType string, uuid TemplateParamUUID, value string) TemplateParam { - return TemplateParam{ - Type_: paramType, - UUID_: uuid, - Value_: value, - } +type ComponentVariables struct { + UUID uuids.UUID `json:"uuid"` + Variables []string `json:"variables"` } // MsgTemplating represents any substituted message template that should be applied when sending this message type MsgTemplating struct { - Template_ *assets.TemplateReference `json:"template"` - Variables_ []string `json:"variables,omitempty"` - Namespace_ string `json:"namespace"` - Params_ map[string][]TemplateParam `json:"params"` + Template_ *assets.TemplateReference `json:"template"` + Variables_ []string `json:"variables,omitempty"` + Namespace_ string `json:"namespace"` + Params_ map[string]ComponentVariables `json:"params"` } // Template returns the template this msg template is for @@ -214,10 +191,10 @@ func (t MsgTemplating) Variables() []string { return t.Variables_ } func (t MsgTemplating) Namespace() string { return t.Namespace_ } // Params returns the params that should be used for the template -func (t MsgTemplating) Params() map[string][]TemplateParam { return t.Params_ } +func (t MsgTemplating) Params() map[string]ComponentVariables { return t.Params_ } // NewMsgTemplating creates and returns a new msg template -func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string, params map[string][]TemplateParam) *MsgTemplating { +func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string, params map[string]ComponentVariables) *MsgTemplating { return &MsgTemplating{ Template_: template, Variables_: variables, diff --git a/flows/msg_test.go b/flows/msg_test.go index ebe1b132c..1aabdc080 100644 --- a/flows/msg_test.go +++ b/flows/msg_test.go @@ -163,18 +163,12 @@ func TestMsgTemplating(t *testing.T) { templateRef := assets.NewTemplateReference("61602f3e-f603-4c70-8a8f-c477505bf4bf", "Affirmation") - tp1 := flows.NewTemplateParam("text", "61f38f46-a856-4f90-899e-905691784159", "@contact.name") - assert.Equal(t, "text", tp1.Type()) - assert.Equal(t, flows.TemplateParamUUID("61f38f46-a856-4f90-899e-905691784159"), tp1.UUID()) - assert.Equal(t, "@contact.name", tp1.Value()) - tp2 := flows.NewTemplateParam("text", "b424c534-dacf-4b9c-9905-4fe136b9390a", "boy") - - msgTemplating := flows.NewMsgTemplating(templateRef, []string{"@contact.name", "boy"}, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", map[string][]flows.TemplateParam{"body": []flows.TemplateParam{tp1, tp2}}) + msgTemplating := flows.NewMsgTemplating(templateRef, []string{"@contact.name", "boy"}, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", map[string]flows.ComponentVariables{"body": flows.ComponentVariables{UUID: "61f38f46-a856-4f90-899e-905691784159", Variables: []string{"@contact.name", "boy"}}}) assert.Equal(t, templateRef, msgTemplating.Template()) assert.Equal(t, []string{"@contact.name", "boy"}, msgTemplating.Variables()) assert.Equal(t, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", msgTemplating.Namespace()) - assert.Equal(t, map[string][]flows.TemplateParam{"body": []flows.TemplateParam{tp1, tp2}}, msgTemplating.Params()) + assert.Equal(t, map[string]flows.ComponentVariables{"body": flows.ComponentVariables{UUID: "61f38f46-a856-4f90-899e-905691784159", Variables: []string{"@contact.name", "boy"}}}, msgTemplating.Params()) // test marshaling our msg marshaled, err := jsonx.Marshal(msgTemplating) @@ -183,18 +177,10 @@ func TestMsgTemplating(t *testing.T) { test.AssertEqualJSON(t, []byte(`{ "namespace":"0162a7f4_dfe4_4c96_be07_854d5dba3b2b", "params":{ - "body":[ - { - "type":"text", - "uuid":"61f38f46-a856-4f90-899e-905691784159", - "value":"@contact.name" - }, - { - "type":"text", - "uuid":"b424c534-dacf-4b9c-9905-4fe136b9390a", - "value":"boy" - } - ] + "body":{ + "uuid":"61f38f46-a856-4f90-899e-905691784159", + "variables": ["@contact.name", "boy"] + } }, "template":{ "name":"Affirmation",