diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index 3c7ac9a93..1a2c1fcab 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -85,13 +85,29 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow for _, dest := range destinations { urn := dest.URN.URN() channelRef := assets.NewChannelReference(dest.Channel.UUID(), dest.Channel.Name()) - var msg *flows.MsgOut + if template != nil { locales := []i18n.Locale{run.Session().MergedEnvironment().DefaultLocale(), run.Session().Environment().DefaultLocale()} - templateTranslation := template.FindTranslation(dest.Channel, locales) - if templateTranslation != nil { - msg = a.getTemplateMsg(run, urn, channelRef, templateTranslation, unsendableReason, logEvent) + translation := template.FindTranslation(dest.Channel, locales) + if translation != nil { + // TODO in future we won't be localizing template variables + localizedVariables, _ := run.GetTextArray(uuids.UUID(a.UUID()), "template_variables", a.TemplateVariables, nil) + + // evaluate the variables + evaluatedVariables := make([]string, len(localizedVariables)) + for i, varExp := range localizedVariables { + v, _ := run.EvaluateTemplate(varExp, logEvent) + evaluatedVariables[i] = v + } + + templating := template.Templating(translation, evaluatedVariables) + + // the message we return is an approximate preview of what the channel will send using the template + preview := translation.Preview(templating.Variables) + locale := translation.Locale() + + msg = flows.NewMsgOut(urn, channelRef, preview.Text, preview.Attachments, preview.QuickReplies, templating, flows.NilMsgTopic, locale, unsendableReason) } } @@ -111,46 +127,3 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow return nil } - -// for message actions that specify a template, this generates a mesage with templating information and content that can -// be used as a preview -func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef *assets.ChannelReference, translation *flows.TemplateTranslation, unsendableReason flows.UnsendableReason, logEvent flows.EventCallback) *flows.MsgOut { - // localize and evaluate the variables - localizedVariables, _ := run.GetTextArray(uuids.UUID(a.UUID()), "template_variables", a.TemplateVariables, nil) - evaluatedVariables := make([]string, len(localizedVariables)) - for i, varExp := range localizedVariables { - v, _ := run.EvaluateTemplate(varExp, logEvent) - evaluatedVariables[i] = v - } - - // 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) { - value = evaluatedVariables[i] - } - - variables[i] = &flows.TemplatingVariable{Type: v.Type(), Value: value} - } - - // create a list of components that have variables - components := make([]*flows.TemplatingComponent, 0, len(translation.Components())) - for _, comp := range translation.Components() { - if len(comp.Variables()) > 0 { - components = append(components, &flows.TemplatingComponent{ - Type: comp.Type(), - Name: comp.Name(), - Variables: comp.Variables(), - }) - } - } - - // the message we return is an approximate preview of what the channel will send using the template - preview := translation.Preview(variables) - locale := translation.Locale() - templating := flows.NewMsgTemplating(a.Template, components, variables) - - return flows.NewMsgOut(urn, channelRef, preview.Text, preview.Attachments, preview.QuickReplies, templating, flows.NilMsgTopic, locale, unsendableReason) -} diff --git a/flows/template.go b/flows/template.go index 82b0f4c16..bbec515de 100644 --- a/flows/template.go +++ b/flows/template.go @@ -50,6 +50,34 @@ func (t *Template) FindTranslation(channel *Channel, locales []i18n.Locale) *Tem return candidates[match] } +func (t *Template) Templating(tt *TemplateTranslation, vars []string) *MsgTemplating { + // cross-reference with asset to get variable types and filter out invalid values + variables := make([]*TemplatingVariable, len(tt.Variables())) + for i, v := range tt.Variables() { + // we pad out any missing variables with empty values + value := "" + if i < len(vars) { + value = vars[i] + } + + variables[i] = &TemplatingVariable{Type: v.Type(), Value: value} + } + + // create a list of components that have variables + components := make([]*TemplatingComponent, 0, len(tt.Components())) + for _, comp := range tt.Components() { + if len(comp.Variables()) > 0 { + components = append(components, &TemplatingComponent{ + Type: comp.Type(), + Name: comp.Name(), + Variables: comp.Variables(), + }) + } + } + + return NewMsgTemplating(t.Reference(), components, variables) +} + // TemplateTranslation represents a single translation for a template type TemplateTranslation struct { assets.TemplateTranslation