From 822e87c901590cb37833ed9117382b4f8f147f08 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Thu, 7 Dec 2023 15:53:54 +0200 Subject: [PATCH] Extract function to evaluate message template --- flows/actions/send_msg.go | 109 ++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index 688d88681..2ff588701 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -1,12 +1,15 @@ package actions import ( + "strings" + "github.com/nyaruka/gocommon/i18n" "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/gocommon/uuids" "github.com/nyaruka/goflow/assets" "github.com/nyaruka/goflow/flows" "github.com/nyaruka/goflow/flows/events" + "github.com/nyaruka/goflow/utils" ) func init() { @@ -102,48 +105,9 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow // do we have a template defined? var templating *flows.MsgTemplating if template != nil { - // looks for a translation in the contact locale or environment default - locales := []i18n.Locale{ - run.Session().MergedEnvironment().DefaultLocale(), - run.Session().Environment().DefaultLocale(), - } - - translation := template.FindTranslation(dest.Channel, locales) - if translation != nil { - localizedVariables, _ := run.GetTextArray(uuids.UUID(a.Templating.UUID), "variables", a.Templating.Variables, nil) - - // evaluate our variables - 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 - } - evaluatedText = translation.Substitute(evaluatedVariables) - - evaluatedParams := make(map[string][]flows.TemplateParam) - for compKey, compParams := range a.Templating.Params { - compVariables := make([]flows.TemplateParam, len(compParams)) - for i, templateParam := range compParams { - - localizedParamVariables, _ := run.GetTextArray(uuids.UUID(templateParam.UUID()), "value", []string{templateParam.Value()}, nil) - sub, err := run.EvaluateTemplate(localizedParamVariables[0]) - if err != nil { - logEvent(events.NewError(err)) - } - evaluatedParam := flows.NewTemplateParam(templateParam.Type(), templateParam.UUID(), sub) - compVariables[i] = evaluatedParam - } - evaluatedParams[compKey] = compVariables - - } - - templating = flows.NewMsgTemplating(a.Templating.Template, evaluatedVariables, translation.Namespace(), evaluatedParams) - locale = translation.Locale() - } + // evaluate our templates params + evaluatedText, templating, locale = evaluateMsgTemplating(run, template, dest, a, logEvent, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, templating, locale) } msg := flows.NewMsgOut(urn, channelRef, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, templating, a.Topic, locale, unsendableReason) @@ -159,3 +123,66 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow return nil } + +func evaluateMsgTemplating(run flows.Run, template *flows.Template, dest flows.Destination, a *SendMsgAction, logEvent flows.EventCallback, evaluatedText string, attachments []utils.Attachment, quickReplies []string, templating *flows.MsgTemplating, locale i18n.Locale) (string, *flows.MsgTemplating, i18n.Locale) { + // looks for a translation in the contact locale or environment default + locales := []i18n.Locale{ + run.Session().MergedEnvironment().DefaultLocale(), + run.Session().Environment().DefaultLocale(), + } + + qrIndex := 0 + + translation := template.FindTranslation(dest.Channel, locales) + if translation != nil { + localizedVariables, _ := run.GetTextArray(uuids.UUID(a.Templating.UUID), "variables", a.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 + } + evaluatedText = translation.Substitute(evaluatedVariables) + + evaluatedParams := make(map[string][]flows.TemplateParam) + + for compKey, compParams := range a.Templating.Params { + compVariables := make([]flows.TemplateParam, len(compParams)) + for i, templateParam := range compParams { + var sub string + var err error + + if strings.HasPrefix(compKey, "button.") { + sub = quickReplies[qrIndex] + qrIndex++ + } else if templateParam.Type() != "text" { + sub = "" + for _, att := range attachments { + if att.ContentType() == templateParam.Type() { + sub = att.URL() + break + } + } + + } else { + localizedParamVariables, _ := run.GetTextArray(uuids.UUID(templateParam.UUID()), "value", []string{templateParam.Value()}, nil) + sub, err = run.EvaluateTemplate(localizedParamVariables[0]) + if err != nil { + logEvent(events.NewError(err)) + } + } + evaluatedParam := flows.NewTemplateParam(templateParam.Type(), templateParam.UUID(), sub) + compVariables[i] = evaluatedParam + } + evaluatedParams[compKey] = compVariables + + } + + templating = flows.NewMsgTemplating(a.Templating.Template, evaluatedVariables, translation.Namespace(), evaluatedParams) + locale = translation.Locale() + } + return evaluatedText, templating, locale +}