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, } } diff --git a/flows/msg_test.go b/flows/msg_test.go index 0b254a241..83c98b5a9 100644 --- a/flows/msg_test.go +++ b/flows/msg_test.go @@ -156,3 +156,40 @@ func TestBroadcastTranslations(t *testing.T) { assert.NoError(t, err) assert.Equal(t, flows.BroadcastTranslations{"spa": {Text: "Adios"}}, bt) } + +func TestMsgTemplating(t *testing.T) { + uuids.SetGenerator(uuids.NewSeededGenerator(12345)) + defer uuids.SetGenerator(uuids.DefaultGenerator) + + templateRef := assets.NewTemplateReference("61602f3e-f603-4c70-8a8f-c477505bf4bf", "Affirmation") + + msgTemplating := flows.NewMsgTemplating(templateRef, []string{"Ryan Lewis", "boy"}, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b") + + assert.Equal(t, templateRef, msgTemplating.Template()) + assert.Equal(t, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", msgTemplating.Namespace()) + assert.Equal(t, map[string][]flows.TemplateParam{"body": {{Type: "text", Value: "Ryan Lewis"}, {Type: "text", Value: "boy"}}}, msgTemplating.Params()) + + // test marshaling our msg + marshaled, err := jsonx.Marshal(msgTemplating) + require.NoError(t, err) + + test.AssertEqualJSON(t, []byte(`{ + "namespace":"0162a7f4_dfe4_4c96_be07_854d5dba3b2b", + "params":{ + "body":[ + { + "type": "text", + "value": "Ryan Lewis" + }, + { + "type": "text", + "value": "boy" + } + ] + }, + "template":{ + "name":"Affirmation", + "uuid":"61602f3e-f603-4c70-8a8f-c477505bf4bf" + } + }`), marshaled, "JSON mismatch") +}