diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index 52e78a98c..cd0ac04bb 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -1,6 +1,9 @@ package actions import ( + "strconv" + "strings" + "github.com/nyaruka/gocommon/i18n" "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/gocommon/uuids" @@ -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 @@ -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)) + } + 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)) + } + 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 + } } - 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} + } } } diff --git a/flows/actions/testdata/_assets.json b/flows/actions/testdata/_assets.json index ce93326c1..cfc7a194c 100644 --- a/flows/actions/testdata/_assets.json +++ b/flows/actions/testdata/_assets.json @@ -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": [ diff --git a/flows/actions/testdata/send_msg.json b/flows/actions/testdata/send_msg.json index 93e8c4132..04b3bb8dc 100644 --- a/flows/actions/testdata/send_msg.json +++ b/flows/actions/testdata/send_msg.json @@ -347,6 +347,7 @@ "text": "Hi @contact.name, who's a good boy?", "templating": { "uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785", + "params": null, "template": { "uuid": "b620b463-8d15-427f-b2e3-4f44f9f071ec", "name": "missing" @@ -419,6 +420,12 @@ "text": "Hi Ryan Lewis, who's a good boy?", "templating": { "uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785", + "params": { + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "variables": ["@contact.name", "boy"] + } + }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", "name": "affirmation" @@ -500,6 +507,12 @@ "text": "Hi Ryan Lewis, who's a good boy?", "templating": { "uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785", + "params": { + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "variables": ["@contact.name", "boy"] + } + }, "template": { "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", "name": "affirmation" @@ -517,6 +530,9 @@ "@contact.name", "niño" ] + }, + "74d59988-18c0-4919-9cb4-9f9e3847ed50": { + "variables": ["@contact.name", "niño"] } } }, @@ -591,6 +607,7 @@ "text": "Hi there, it's time to get up!", "templating": { "uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900", + "params": null, "template": { "uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900", "name": "wakeup" @@ -650,6 +667,7 @@ "text": "Hi Ryan, wakeup!", "templating": { "uuid": "dce1ab9c-81d7-4e2f-b7d6-33e577c731ba", + "params": null, "template": { "uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900", "name": "wakeup" @@ -788,5 +806,178 @@ "waiting_exits": [], "parent_refs": [] } + }, + { + "description": "Use template translation with attachments and quick replies and localized", + "action": { + "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" + ], + "templating": { + "uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785", + "params": { + "header":{ + "uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1", + "variables": ["http://example.com/red.jpg"] + }, + "body": { + "uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50", + "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": "ce00c80e-991a-4c03-b373-3273c23ee042", + "name": "gender_update" + }, + "variables": [ + "@contact.name", + "boy" + ] + } + }, + "localization": { + "spa": { + "ad154980-7bf7-4ab8-8728-545fd6378912": { + "text": [ + "Hola!" + ], + "attachments": [ + "http://example.com/rojo.jpg" + ], + "quick_replies": [ + "Si", + "No" + ] + }, + "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785": { + "variables": [ + "@contact.name", + "niño" + ] + }, + "98788f6d-775e-4c6c-a11c-d6169c3551c1": { + "variables": [ + "http://example.com/rojo.jpg" + ] + }, + "f383e94d-0598-4fd9-81d7-f0aea397d63b": { + "variables": ["niño"] + }, + "74d59988-18c0-4919-9cb4-9f9e3847ed50": { + "variables": ["@contact.name", "niño"] + } + } + }, + "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": "Hola, Ryan Lewis, tu género está guardado como niño.", + "attachments": [ + "http://example.com/rojo.jpg" + ], + "quick_replies": [ + "Si", + "No" + ], + "templating": { + "params": { + "body": [ + { + "type": "text", + "value": "Ryan Lewis" + }, + { + "type": "text", + "value": "niño" + } + ], + "header": [ + { + "type": "image", + "value": "http://example.com/rojo.jpg" + } + ], + "button.0": [ + { + "type": "text", + "value": "Si" + } + ], + "button.1": [ + { + "type": "text", + "value": "No" + } + ] + }, + "template": { + "uuid": "ce00c80e-991a-4c03-b373-3273c23ee042", + "name": "gender_update" + }, + "namespace": "" + }, + "locale": "spa" + } + } + ], + "templates": [ + "Hey Ryan Lewis, your gender is saved as boy.", + "Hola!", + "http://example.com/red.jpg", + "http://example.com/rojo.jpg", + "Yes", + "No", + "Si", + "No", + "@contact.name", + "boy", + "@contact.name", + "niño" + ], + "localizables": [ + "Hey Ryan Lewis, your gender is saved as boy.", + "http://example.com/red.jpg", + "Yes", + "No", + "@contact.name", + "boy" + ], + "inspection": { + "dependencies": [ + { + "uuid": "ce00c80e-991a-4c03-b373-3273c23ee042", + "name": "gender_update", + "type": "template" + } + ], + "issues": [], + "results": [], + "waiting_exits": [], + "parent_refs": [] + } } ] \ No newline at end of file