From e4a7f714f73d09f9bb9cb7b407d39e17a5519e61 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Thu, 30 Nov 2023 13:27:51 +0200 Subject: [PATCH] Keep supporting templating variables until engine completely switch to use only params --- handlers/dialog360/handler_test.go | 13 +++++++++++++ handlers/meta/whataspp_test.go | 13 +++++++++++++ handlers/meta/whatsapp/templates.go | 13 ++++++++++++- handlers/whatsapp_legacy/handler.go | 6 +++++- handlers/whatsapp_legacy/handler_test.go | 13 +++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/handlers/dialog360/handler_test.go b/handlers/dialog360/handler_test.go index a7095ca5b..6c88e369a 100644 --- a/handlers/dialog360/handler_test.go +++ b/handlers/dialog360/handler_test.go @@ -417,6 +417,19 @@ var SendTestCasesD3C = []OutgoingTestCase{ ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, SendPrep: setSendURL, }, + { + Label: "Template Send, old variables", + MsgText: "templated message", + MsgURN: "whatsapp:250788123123", + MsgLocale: "eng", + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`), + ExpectedMsgStatus: "W", + ExpectedExternalID: "157b5e14568e8", + MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, + MockResponseStatus: 200, + ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, + SendPrep: setSendURL, + }, { Label: "Template Country Language", MsgText: "templated message", diff --git a/handlers/meta/whataspp_test.go b/handlers/meta/whataspp_test.go index 15d6c8f11..81a1b3de6 100644 --- a/handlers/meta/whataspp_test.go +++ b/handlers/meta/whataspp_test.go @@ -381,6 +381,19 @@ var whatsappOutgoingTests = []OutgoingTestCase{ ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, SendPrep: setSendURL, }, + { + Label: "Template Send, old variables", + MsgText: "templated message", + MsgURN: "whatsapp:250788123123", + MsgLocale: "eng", + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`), + ExpectedMsgStatus: "W", + ExpectedExternalID: "157b5e14568e8", + MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, + MockResponseStatus: 200, + ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, + SendPrep: setSendURL, + }, { Label: "Template Country Language", MsgText: "templated message", diff --git a/handlers/meta/whatsapp/templates.go b/handlers/meta/whatsapp/templates.go index d92a80d1b..8e2519c14 100644 --- a/handlers/meta/whatsapp/templates.go +++ b/handlers/meta/whatsapp/templates.go @@ -14,7 +14,8 @@ type MsgTemplating struct { Name string `json:"name" validate:"required"` UUID string `json:"uuid" validate:"required"` } `json:"template" validate:"required,dive"` - Namespace string `json:"namespace"` + Namespace string `json:"namespace"` + Variables []string `json:"variables"` Params map[string][]struct { Type string `json:"type"` Value string `json:"value"` @@ -94,6 +95,16 @@ func GetTemplatePayload(templating MsgTemplating, lang string) *Template { } + if len(templating.Params) == 0 { + component := &Component{Type: "body"} + + for _, v := range templating.Variables { + component.Params = append(component.Params, &Param{Type: "text", Text: v}) + } + template.Components = append(template.Components, component) + + } + return &template } diff --git a/handlers/whatsapp_legacy/handler.go b/handlers/whatsapp_legacy/handler.go index b3823655d..a63dde0ce 100644 --- a/handlers/whatsapp_legacy/handler.go +++ b/handlers/whatsapp_legacy/handler.go @@ -747,6 +747,9 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([] if len(templating.Params) == 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) } @@ -1127,7 +1130,8 @@ type MsgTemplating struct { Name string `json:"name" validate:"required"` UUID string `json:"uuid" validate:"required"` } `json:"template" validate:"required,dive"` - Namespace string `json:"namespace"` + Namespace string `json:"namespace"` + Variables []string `json:"variables"` Params map[string][]struct { Type string `json:"type"` Value string `json:"value"` diff --git a/handlers/whatsapp_legacy/handler_test.go b/handlers/whatsapp_legacy/handler_test.go index ea9cf4aa0..cff0144cb 100644 --- a/handlers/whatsapp_legacy/handler_test.go +++ b/handlers/whatsapp_legacy/handler_test.go @@ -726,6 +726,19 @@ var defaultSendTestCases = []OutgoingTestCase{ ExpectedExternalID: "157b5e14568e8", SendPrep: setSendURL, }, + { + Label: "Template Send, old variables", + MsgText: "templated message", + MsgURN: "whatsapp:250788123123", + MsgLocale: "eng", + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" },"variables": ["Chef", "tomorrow"]}}`), + MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, + MockResponseStatus: 200, + ExpectedRequestBody: `{"to":"250788123123","type":"template","template":{"namespace":"waba_namespace","name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, + ExpectedMsgStatus: "W", + ExpectedExternalID: "157b5e14568e8", + SendPrep: setSendURL, + }, { Label: "Template Send no variables", MsgText: "templated message",