From 47a9985fe424bbce32f29cc885ed907d381764fe Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 1 Dec 2023 15:45:36 +0200 Subject: [PATCH] WIp support WA template params --- assets/static/template.go | 12 +++++++++++- assets/static/template_test.go | 2 +- assets/template.go | 2 ++ flows/actions/send_msg.go | 32 ++++++++++++++++++++++++++++---- flows/msg.go | 7 ++++++- flows/template_test.go | 10 +++++----- 6 files changed, 53 insertions(+), 12 deletions(-) diff --git a/assets/static/template.go b/assets/static/template.go index f2a511cd2..14accfd4b 100644 --- a/assets/static/template.go +++ b/assets/static/template.go @@ -43,16 +43,20 @@ type TemplateTranslation struct { Locale_ i18n.Locale `json:"locale" validate:"required"` Namespace_ string `json:"namespace"` VariableCount_ int `json:"variable_count"` + Components_ []map[string]any `json:"components"` + Params_ map[string][]any `json:"params"` } // NewTemplateTranslation creates a new template translation -func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string) *TemplateTranslation { +func NewTemplateTranslation(channel *assets.ChannelReference, locale i18n.Locale, content string, variableCount int, namespace string, components []map[string]any, params map[string][]any) *TemplateTranslation { return &TemplateTranslation{ Channel_: channel, Content_: content, Namespace_: namespace, Locale_: locale, VariableCount_: variableCount, + Components_: components, + Params_: params, } } @@ -70,3 +74,9 @@ func (t *TemplateTranslation) VariableCount() int { return t.VariableCount_ } // Channel returns the channel this template translation is for func (t *TemplateTranslation) Channel() *assets.ChannelReference { return t.Channel_ } + +// Components returns the components for this template translation +func (t *TemplateTranslation) Components() []map[string]any { return t.Components_ } + +// Params returns the params for this template translation +func (t *TemplateTranslation) Params() map[string][]any { return t.Params_ } diff --git a/assets/static/template_test.go b/assets/static/template_test.go index 1136bc8bb..4025ffc55 100644 --- a/assets/static/template_test.go +++ b/assets/static/template_test.go @@ -12,7 +12,7 @@ import ( func TestTemplate(t *testing.T) { channel := assets.NewChannelReference("Test Channel", "ffffffff-9b24-92e1-ffff-ffffb207cdb4") - translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b") + translation := NewTemplateTranslation(channel, i18n.Locale("eng-US"), "Hello {{1}}", 1, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", []map[string]any{}, map[string][]any{}) assert.Equal(t, channel, translation.Channel()) assert.Equal(t, i18n.Locale("eng-US"), translation.Locale()) assert.Equal(t, "Hello {{1}}", translation.Content()) diff --git a/assets/template.go b/assets/template.go index d30bf608a..4919207b2 100644 --- a/assets/template.go +++ b/assets/template.go @@ -49,6 +49,8 @@ type TemplateTranslation interface { Namespace() string VariableCount() int Channel() *ChannelReference + Components() []map[string]any + Params() map[string][]any } // TemplateReference is used to reference a Template diff --git a/flows/actions/send_msg.go b/flows/actions/send_msg.go index 9ca8f79cd..a861ccd76 100644 --- a/flows/actions/send_msg.go +++ b/flows/actions/send_msg.go @@ -52,9 +52,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"` + 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][]map[string]string `json:"params"` } // LocalizationUUID gets the UUID which identifies this object for localization @@ -121,8 +122,31 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow evaluatedVariables[i] = sub } + evaluatedParams := make(map[string][]any) + + for compKey, compParams := range a.Templating.Params { + compVariables := make([]any, len(compParams)) + for _, variableMap := range compParams { + evaluatedMap := make(map[string]string) + for k, v := range variableMap { + if k == "value" { + sub, err := run.EvaluateTemplate(variableMap[k]) + if err != nil { + logEvent(events.NewError(err)) + } + evaluatedMap[k] = sub + } else { + evaluatedMap[k] = v + } + } + compVariables = append(compVariables, evaluatedMap) + } + evaluatedParams[compKey] = compVariables + + } + evaluatedText = translation.Substitute(evaluatedVariables) - templating = flows.NewMsgTemplating(a.Templating.Template, evaluatedVariables, translation.Namespace()) + templating = flows.NewMsgTemplating(a.Templating.Template, evaluatedVariables, translation.Namespace(), evaluatedParams) locale = translation.Locale() } } diff --git a/flows/msg.go b/flows/msg.go index 924c00327..9e276c82f 100644 --- a/flows/msg.go +++ b/flows/msg.go @@ -173,6 +173,7 @@ type MsgTemplating struct { Template_ *assets.TemplateReference `json:"template"` Variables_ []string `json:"variables,omitempty"` Namespace_ string `json:"namespace"` + Params_ map[string][]any `json:"params"` } // Template returns the template this msg template is for @@ -184,12 +185,16 @@ func (t MsgTemplating) Variables() []string { return t.Variables_ } // Namespace returns the namespace that should be for the template 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][]any { return t.Params_ } + // NewMsgTemplating creates and returns a new msg template -func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string) *MsgTemplating { +func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string, params map[string][]any) *MsgTemplating { return &MsgTemplating{ Template_: template, Variables_: variables, Namespace_: namespace, + Params_: params, } } diff --git a/flows/template_test.go b/flows/template_test.go index 8bc6f4187..94b8adc70 100644 --- a/flows/template_test.go +++ b/flows/template_test.go @@ -26,7 +26,7 @@ func TestTemplateTranslation(t *testing.T) { channel := assets.NewChannelReference("0bce5fd3-c215-45a0-bcb8-2386eb194175", "Test Channel") for i, tc := range tcs { - tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c")) + tt := flows.NewTemplateTranslation(static.NewTemplateTranslation(channel, i18n.Locale("eng-US"), tc.Content, len(tc.Variables), "a6a8863e_7879_4487_ad24_5e2ea429027c", []map[string]any{}, map[string][]any{})) result := tt.Substitute(tc.Variables) assert.Equal(t, tc.Expected, result, "%d: unexpected template substitution", i) } @@ -39,10 +39,10 @@ func TestTemplate(t *testing.T) { channel1Ref := assets.NewChannelReference(channel1.UUID(), channel1.Name()) channel2Ref := assets.NewChannelReference(channel2.UUID(), channel2.Name()) - tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "") - tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "") - tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "") - tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "") + tt1 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("eng"), "Hello {{1}}", 1, "", []map[string]any{}, map[string][]any{}) + tt2 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-EC"), "Que tal {{1}}", 1, "", []map[string]any{}, map[string][]any{}) + tt3 := static.NewTemplateTranslation(channel1Ref, i18n.Locale("spa-ES"), "Hola {{1}}", 1, "", []map[string]any{}, map[string][]any{}) + tt4 := static.NewTemplateTranslation(channel2Ref, i18n.Locale("en"), "Hello {{1}}", 1, "", []map[string]any{}, map[string][]any{}) template := flows.NewTemplate(static.NewTemplate("c520cbda-e118-440f-aaf6-c0485088384f", "greeting", []*static.TemplateTranslation{tt1, tt2, tt3, tt4})) tas := flows.NewTemplateAssets([]assets.Template{template})