Skip to content

Commit

Permalink
Extract function to evaluate message template
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 7, 2023
1 parent 5e8be07 commit 822e87c
Showing 1 changed file with 68 additions and 41 deletions.
109 changes: 68 additions & 41 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit 822e87c

Please sign in to comment.