From d4ac11c1de7b2b500d78ad33606926ca5b115587 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 17 Jan 2024 13:29:53 +0200 Subject: [PATCH] Replace flows.MsgTemplating variables by params --- flows/actions/testdata/send_msg.json | 34 +++++++++++++++++++++------- flows/msg.go | 27 ++++++++++++++++------ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/flows/actions/testdata/send_msg.json b/flows/actions/testdata/send_msg.json index d822fa210..93e8c4132 100644 --- a/flows/actions/testdata/send_msg.json +++ b/flows/actions/testdata/send_msg.json @@ -448,10 +448,19 @@ "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", "name": "affirmation" }, - "variables": [ - "Ryan Lewis", - "boy" - ], + "params": { + "body": [ + { + "type": "text", + "value": "Ryan Lewis" + }, + { + "type": "text", + "value": "boy" + } + + ] + }, "namespace": "" }, "topic": "account", @@ -529,10 +538,19 @@ "uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e", "name": "affirmation" }, - "variables": [ - "Ryan Lewis", - "niño" - ], + "params": { + "body": [ + { + "type": "text", + "value": "Ryan Lewis" + }, + { + "type": "text", + "value": "niño" + } + + ] + }, "namespace": "" }, "locale": "spa" diff --git a/flows/msg.go b/flows/msg.go index 924c00327..3642d0b71 100644 --- a/flows/msg.go +++ b/flows/msg.go @@ -168,28 +168,41 @@ func (m *MsgOut) Locale() i18n.Locale { return m.Locale_ } // UnsendableReason returns the reason this message can't be sent (if any) func (m *MsgOut) UnsendableReason() UnsendableReason { return m.UnsendableReason_ } +type TemplateParam struct { + Type string `json:"type"` + Value string `json:"value"` +} + // MsgTemplating represents any substituted message template that should be applied when sending this message type MsgTemplating struct { - Template_ *assets.TemplateReference `json:"template"` - Variables_ []string `json:"variables,omitempty"` - Namespace_ string `json:"namespace"` + Template_ *assets.TemplateReference `json:"template"` + Params_ map[string][]TemplateParam `json:"params,omitempty"` + Namespace_ string `json:"namespace"` } // Template returns the template this msg template is for func (t MsgTemplating) Template() *assets.TemplateReference { return t.Template_ } -// Variables returns the variables that should be substituted in the template -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][]TemplateParam { return t.Params_ } + // NewMsgTemplating creates and returns a new msg template func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string) *MsgTemplating { + params := map[string][]TemplateParam{} + if len(variables) > 0 { + params = map[string][]TemplateParam{"body": make([]TemplateParam, len(variables))} + for i, v := range variables { + params["body"][i] = TemplateParam{Type: "text", Value: v} + } + } + return &MsgTemplating{ Template_: template, - Variables_: variables, Namespace_: namespace, + Params_: params, } }