From 9d15c514f66ffc9665287ced42aec5f339d76eb8 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 3 Apr 2024 15:25:38 +0200 Subject: [PATCH] Adjust legacy WA channel handler to use templating components --- handlers/whatsapp_legacy/handler.go | 29 ++++++++++-------------- handlers/whatsapp_legacy/handler_test.go | 10 ++++---- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/handlers/whatsapp_legacy/handler.go b/handlers/whatsapp_legacy/handler.go index 23ab9bb00..f7b147b65 100644 --- a/handlers/whatsapp_legacy/handler.go +++ b/handlers/whatsapp_legacy/handler.go @@ -719,10 +719,10 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([] payload.Template.Language.Policy = "deterministic" payload.Template.Language.Code = langCode - for k, v := range templating.Params { - if k == "body" { + for _, comp := range templating.Components { + if comp.Type == "body" { component := &Component{Type: "body"} - for _, p := range v { + for _, p := range comp.Params { component.Parameters = append(component.Parameters, Param{Type: p.Type, Text: p.Value}) } payload.Template.Components = append(payload.Template.Components, *component) @@ -731,14 +731,6 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([] } - if len(templating.Params) == 0 && len(templating.Variables) != 0 { - component := &Component{Type: "body"} - for _, v := range templating.Variables { - component.Parameters = append(component.Parameters, Param{Type: "text", Text: v}) - } - payload.Template.Components = append(payload.Template.Components, *component) - } - payloads = append(payloads, payload) } else { @@ -1116,12 +1108,15 @@ type MsgTemplating struct { Name string `json:"name" validate:"required"` UUID string `json:"uuid" validate:"required"` } `json:"template" validate:"required,dive"` - Namespace string `json:"namespace"` - Variables []string `json:"variables"` - Params map[string][]struct { - Type string `json:"type"` - Value string `json:"value"` - } `json:"params"` + Namespace string `json:"namespace"` + Components []struct { + Type string `json:"type"` + Name string `json:"name"` + Params []struct { + Type string `json:"type"` + Value string `json:"value"` + } `json:"params"` + } `json:"components"` } func getSupportedLanguage(lc i18n.Locale) string { diff --git a/handlers/whatsapp_legacy/handler_test.go b/handlers/whatsapp_legacy/handler_test.go index a49b11fec..dc5eea935 100644 --- a/handlers/whatsapp_legacy/handler_test.go +++ b/handlers/whatsapp_legacy/handler_test.go @@ -766,7 +766,7 @@ var defaultSendTestCases = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "eng", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`), MockResponses: map[string][]*httpx.MockResponse{ "*/v1/messages": { httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)), @@ -784,7 +784,7 @@ var defaultSendTestCases = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "eng", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {}}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": []}}`), MockResponses: map[string][]*httpx.MockResponse{ "*/v1/messages": { httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)), @@ -802,7 +802,7 @@ var defaultSendTestCases = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "eng-US", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`), MockResponses: map[string][]*httpx.MockResponse{ "*/v1/messages": { httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)), @@ -820,7 +820,7 @@ var defaultSendTestCases = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "eng-US", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`), MockResponses: map[string][]*httpx.MockResponse{ "*/v1/messages": { httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)), @@ -838,7 +838,7 @@ var defaultSendTestCases = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "bnt", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`), MockResponses: map[string][]*httpx.MockResponse{ "*/v1/messages": { httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),