From 03d5765f879868a9579237c896131a8c6fb4f1f1 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 29 Nov 2023 16:06:24 +0200 Subject: [PATCH] Prep for support for more WA templates components --- handlers/dialog360/handler.go | 147 +++++++++++----------- handlers/dialog360/handler_test.go | 6 +- handlers/meta/handlers.go | 150 +++++++++++------------ handlers/meta/whataspp_test.go | 6 +- handlers/meta/whatsapp/templates.go | 4 +- handlers/whatsapp_legacy/handler.go | 45 ++----- handlers/whatsapp_legacy/handler_test.go | 35 +----- 7 files changed, 169 insertions(+), 224 deletions(-) diff --git a/handlers/dialog360/handler.go b/handlers/dialog360/handler.go index dafe6623f..e7bc56c5f 100644 --- a/handlers/dialog360/handler.go +++ b/handlers/dialog360/handler.go @@ -324,28 +324,83 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch for i := 0; i < len(msgParts)+len(msg.Attachments()); i++ { payload := whatsapp.SendRequest{MessagingProduct: "whatsapp", RecipientType: "individual", To: msg.URN().Path()} - if len(msg.Attachments()) == 0 { - // do we have a template? - templating, err := whatsapp.GetTemplating(msg) - if err != nil { - return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID()) - } - if templating != nil { + // do we have a template? + templating, err := whatsapp.GetTemplating(msg) + if err != nil { + return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID()) + } + if templating != nil { - payload.Type = "template" + payload.Type = "template" - template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}} - payload.Template = &template + template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}} + payload.Template = &template - component := &whatsapp.Component{Type: "body"} + for _, v := range templating.Components { + template.Components = append(payload.Template.Components, &v) + } - for _, v := range templating.Variables { - component.Params = append(component.Params, &whatsapp.Param{Type: "text", Text: v}) + } else if len(msg.Attachments()) == 0 { + if i < (len(msgParts) + len(msg.Attachments()) - 1) { + // this is still a msg part + text := &whatsapp.Text{PreviewURL: false} + payload.Type = "text" + if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") { + text.PreviewURL = true } - template.Components = append(payload.Template.Components, component) - + text.Body = msgParts[i-len(msg.Attachments())] + payload.Text = text } else { - if i < (len(msgParts) + len(msg.Attachments()) - 1) { + if len(qrs) > 0 { + payload.Type = "interactive" + // We can use buttons + if len(qrs) <= 3 { + interactive := whatsapp.Interactive{Type: "button", Body: struct { + Text string "json:\"text\"" + }{Text: msgParts[i-len(msg.Attachments())]}} + + btns := make([]whatsapp.Button, len(qrs)) + for i, qr := range qrs { + btns[i] = whatsapp.Button{ + Type: "reply", + } + btns[i].Reply.ID = fmt.Sprint(i) + btns[i].Reply.Title = qr + } + interactive.Action = &struct { + Button string "json:\"button,omitempty\"" + Sections []whatsapp.Section "json:\"sections,omitempty\"" + Buttons []whatsapp.Button "json:\"buttons,omitempty\"" + }{Buttons: btns} + payload.Interactive = &interactive + } else if len(qrs) <= 10 { + interactive := whatsapp.Interactive{Type: "list", Body: struct { + Text string "json:\"text\"" + }{Text: msgParts[i-len(msg.Attachments())]}} + + section := whatsapp.Section{ + Rows: make([]whatsapp.SectionRow, len(qrs)), + } + for i, qr := range qrs { + section.Rows[i] = whatsapp.SectionRow{ + ID: fmt.Sprint(i), + Title: qr, + } + } + + interactive.Action = &struct { + Button string "json:\"button,omitempty\"" + Sections []whatsapp.Section "json:\"sections,omitempty\"" + Buttons []whatsapp.Button "json:\"buttons,omitempty\"" + }{Button: menuButton, Sections: []whatsapp.Section{ + section, + }} + + payload.Interactive = &interactive + } else { + return nil, fmt.Errorf("too many quick replies WAC supports only up to 10 quick replies") + } + } else { // this is still a msg part text := &whatsapp.Text{PreviewURL: false} payload.Type = "text" @@ -354,66 +409,6 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch } text.Body = msgParts[i-len(msg.Attachments())] payload.Text = text - } else { - if len(qrs) > 0 { - payload.Type = "interactive" - // We can use buttons - if len(qrs) <= 3 { - interactive := whatsapp.Interactive{Type: "button", Body: struct { - Text string "json:\"text\"" - }{Text: msgParts[i-len(msg.Attachments())]}} - - btns := make([]whatsapp.Button, len(qrs)) - for i, qr := range qrs { - btns[i] = whatsapp.Button{ - Type: "reply", - } - btns[i].Reply.ID = fmt.Sprint(i) - btns[i].Reply.Title = qr - } - interactive.Action = &struct { - Button string "json:\"button,omitempty\"" - Sections []whatsapp.Section "json:\"sections,omitempty\"" - Buttons []whatsapp.Button "json:\"buttons,omitempty\"" - }{Buttons: btns} - payload.Interactive = &interactive - } else if len(qrs) <= 10 { - interactive := whatsapp.Interactive{Type: "list", Body: struct { - Text string "json:\"text\"" - }{Text: msgParts[i-len(msg.Attachments())]}} - - section := whatsapp.Section{ - Rows: make([]whatsapp.SectionRow, len(qrs)), - } - for i, qr := range qrs { - section.Rows[i] = whatsapp.SectionRow{ - ID: fmt.Sprint(i), - Title: qr, - } - } - - interactive.Action = &struct { - Button string "json:\"button,omitempty\"" - Sections []whatsapp.Section "json:\"sections,omitempty\"" - Buttons []whatsapp.Button "json:\"buttons,omitempty\"" - }{Button: menuButton, Sections: []whatsapp.Section{ - section, - }} - - payload.Interactive = &interactive - } else { - return nil, fmt.Errorf("too many quick replies WAC supports only up to 10 quick replies") - } - } else { - // this is still a msg part - text := &whatsapp.Text{PreviewURL: false} - payload.Type = "text" - if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") { - text.PreviewURL = true - } - text.Body = msgParts[i-len(msg.Attachments())] - payload.Text = text - } } } diff --git a/handlers/dialog360/handler_test.go b/handlers/dialog360/handler_test.go index 24fb14dd0..940ef4b5e 100644 --- a/handlers/dialog360/handler_test.go +++ b/handlers/dialog360/handler_test.go @@ -409,7 +409,7 @@ var SendTestCasesD3C = []OutgoingTestCase{ 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"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), ExpectedMsgStatus: "W", ExpectedExternalID: "157b5e14568e8", MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, @@ -422,7 +422,7 @@ var SendTestCasesD3C = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "eng-US", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), 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_US"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, @@ -435,7 +435,7 @@ var SendTestCasesD3C = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "bnt", - MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), 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"}]}]}}`, diff --git a/handlers/meta/handlers.go b/handlers/meta/handlers.go index 9d39c3bdc..11fd6a226 100644 --- a/handlers/meta/handlers.go +++ b/handlers/meta/handlers.go @@ -814,28 +814,84 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog for i := 0; i < len(msgParts)+len(msg.Attachments()); i++ { payload := whatsapp.SendRequest{MessagingProduct: "whatsapp", RecipientType: "individual", To: msg.URN().Path()} - if len(msg.Attachments()) == 0 { - // do we have a template? - templating, err := whatsapp.GetTemplating(msg) - if err != nil { - return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID()) - } - if templating != nil { + // do we have a template? + templating, err := whatsapp.GetTemplating(msg) + if err != nil { + return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID()) + } + if templating != nil { - payload.Type = "template" + payload.Type = "template" - template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}} - payload.Template = &template + template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}} + payload.Template = &template - component := &whatsapp.Component{Type: "body"} + for _, v := range templating.Components { + template.Components = append(payload.Template.Components, &v) + } - for _, v := range templating.Variables { - component.Params = append(component.Params, &whatsapp.Param{Type: "text", Text: v}) - } - template.Components = append(payload.Template.Components, component) + } else if len(msg.Attachments()) == 0 { + if i < (len(msgParts) + len(msg.Attachments()) - 1) { + // this is still a msg part + text := &whatsapp.Text{PreviewURL: false} + payload.Type = "text" + if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") { + text.PreviewURL = true + } + text.Body = msgParts[i-len(msg.Attachments())] + payload.Text = text } else { - if i < (len(msgParts) + len(msg.Attachments()) - 1) { + if len(qrs) > 0 { + payload.Type = "interactive" + // We can use buttons + if len(qrs) <= 3 { + interactive := whatsapp.Interactive{Type: "button", Body: struct { + Text string "json:\"text\"" + }{Text: msgParts[i-len(msg.Attachments())]}} + + btns := make([]whatsapp.Button, len(qrs)) + for i, qr := range qrs { + btns[i] = whatsapp.Button{ + Type: "reply", + } + btns[i].Reply.ID = fmt.Sprint(i) + btns[i].Reply.Title = qr + } + interactive.Action = &struct { + Button string "json:\"button,omitempty\"" + Sections []whatsapp.Section "json:\"sections,omitempty\"" + Buttons []whatsapp.Button "json:\"buttons,omitempty\"" + }{Buttons: btns} + payload.Interactive = &interactive + } else if len(qrs) <= 10 { + interactive := whatsapp.Interactive{Type: "list", Body: struct { + Text string "json:\"text\"" + }{Text: msgParts[i-len(msg.Attachments())]}} + + section := whatsapp.Section{ + Rows: make([]whatsapp.SectionRow, len(qrs)), + } + for i, qr := range qrs { + section.Rows[i] = whatsapp.SectionRow{ + ID: fmt.Sprint(i), + Title: qr, + } + } + + interactive.Action = &struct { + Button string "json:\"button,omitempty\"" + Sections []whatsapp.Section "json:\"sections,omitempty\"" + Buttons []whatsapp.Button "json:\"buttons,omitempty\"" + }{Button: menuButton, Sections: []whatsapp.Section{ + section, + }} + + payload.Interactive = &interactive + } else { + return nil, fmt.Errorf("too many quick replies WAC supports only up to 10 quick replies") + } + } else { // this is still a msg part text := &whatsapp.Text{PreviewURL: false} payload.Type = "text" @@ -844,66 +900,6 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog } text.Body = msgParts[i-len(msg.Attachments())] payload.Text = text - } else { - if len(qrs) > 0 { - payload.Type = "interactive" - // We can use buttons - if len(qrs) <= 3 { - interactive := whatsapp.Interactive{Type: "button", Body: struct { - Text string "json:\"text\"" - }{Text: msgParts[i-len(msg.Attachments())]}} - - btns := make([]whatsapp.Button, len(qrs)) - for i, qr := range qrs { - btns[i] = whatsapp.Button{ - Type: "reply", - } - btns[i].Reply.ID = fmt.Sprint(i) - btns[i].Reply.Title = qr - } - interactive.Action = &struct { - Button string "json:\"button,omitempty\"" - Sections []whatsapp.Section "json:\"sections,omitempty\"" - Buttons []whatsapp.Button "json:\"buttons,omitempty\"" - }{Buttons: btns} - payload.Interactive = &interactive - } else if len(qrs) <= 10 { - interactive := whatsapp.Interactive{Type: "list", Body: struct { - Text string "json:\"text\"" - }{Text: msgParts[i-len(msg.Attachments())]}} - - section := whatsapp.Section{ - Rows: make([]whatsapp.SectionRow, len(qrs)), - } - for i, qr := range qrs { - section.Rows[i] = whatsapp.SectionRow{ - ID: fmt.Sprint(i), - Title: qr, - } - } - - interactive.Action = &struct { - Button string "json:\"button,omitempty\"" - Sections []whatsapp.Section "json:\"sections,omitempty\"" - Buttons []whatsapp.Button "json:\"buttons,omitempty\"" - }{Button: menuButton, Sections: []whatsapp.Section{ - section, - }} - - payload.Interactive = &interactive - } else { - return nil, fmt.Errorf("too many quick replies WAC supports only up to 10 quick replies") - } - } else { - // this is still a msg part - text := &whatsapp.Text{PreviewURL: false} - payload.Type = "text" - if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") { - text.PreviewURL = true - } - text.Body = msgParts[i-len(msg.Attachments())] - payload.Text = text - } } } @@ -1066,7 +1062,7 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog zeroIndex = true } - err := h.requestWAC(payload, accessToken, status, wacPhoneURL, zeroIndex, clog) + err = h.requestWAC(payload, accessToken, status, wacPhoneURL, zeroIndex, clog) if err != nil { return status, err } diff --git a/handlers/meta/whataspp_test.go b/handlers/meta/whataspp_test.go index f62585b70..ea87156c1 100644 --- a/handlers/meta/whataspp_test.go +++ b/handlers/meta/whataspp_test.go @@ -373,7 +373,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{ 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"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), ExpectedMsgStatus: "W", ExpectedExternalID: "157b5e14568e8", MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, @@ -386,7 +386,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "eng-US", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), 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_US"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, @@ -399,7 +399,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{ MsgText: "templated message", MsgURN: "whatsapp:250788123123", MsgLocale: "bnt", - MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), 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"}]}]}}`, diff --git a/handlers/meta/whatsapp/templates.go b/handlers/meta/whatsapp/templates.go index c4638c779..59aa6ae8c 100644 --- a/handlers/meta/whatsapp/templates.go +++ b/handlers/meta/whatsapp/templates.go @@ -13,8 +13,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"` - Variables []string `json:"variables"` + Namespace string `json:"namespace"` + Components []Component `json:"components"` } func GetTemplating(msg courier.MsgOut) (*MsgTemplating, error) { diff --git a/handlers/whatsapp_legacy/handler.go b/handlers/whatsapp_legacy/handler.go index de11ebcd9..80422002b 100644 --- a/handlers/whatsapp_legacy/handler.go +++ b/handlers/whatsapp_legacy/handler.go @@ -724,39 +724,18 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([] return nil, errors.Errorf("cannot send template message without Facebook namespace for channel: %s", msg.Channel().UUID()) } - if msg.Channel().BoolConfigForKey(configHSMSupport, false) { - payload := hsmPayload{ - To: msg.URN().Path(), - Type: "hsm", - } - payload.HSM.Namespace = namespace - payload.HSM.ElementName = templating.Template.Name - payload.HSM.Language.Policy = "deterministic" - payload.HSM.Language.Code = langCode - for _, v := range templating.Variables { - payload.HSM.LocalizableParams = append(payload.HSM.LocalizableParams, LocalizableParam{Default: v}) - } - payloads = append(payloads, payload) - } else { - - payload := templatePayload{ - To: msg.URN().Path(), - Type: "template", - } - payload.Template.Namespace = namespace - payload.Template.Name = templating.Template.Name - payload.Template.Language.Policy = "deterministic" - payload.Template.Language.Code = langCode - - component := &Component{Type: "body"} + payload := templatePayload{ + To: msg.URN().Path(), + Type: "template", + } + payload.Template.Namespace = namespace + payload.Template.Name = templating.Template.Name + payload.Template.Language.Policy = "deterministic" + payload.Template.Language.Code = langCode + payload.Template.Components = append(payload.Template.Components, templating.Components...) - 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) - payloads = append(payloads, payload) - } } else { if isInteractiveMsg { @@ -1132,8 +1111,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"` - Variables []string `json:"variables"` + Namespace string `json:"namespace"` + Components []Component `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 f2183103d..07a2d132a 100644 --- a/handlers/whatsapp_legacy/handler_test.go +++ b/handlers/whatsapp_legacy/handler_test.go @@ -718,7 +718,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" }, "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), 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"}]}]}}`, @@ -731,7 +731,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" }, "variables": []}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), 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"}]}}`, @@ -744,7 +744,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" }, "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, MockResponseStatus: 200, ExpectedRequestBody: `{"to":"250788123123","type":"template","template":{"namespace":"waba_namespace","name":"revive_issue","language":{"policy":"deterministic","code":"en_US"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, @@ -757,7 +757,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", "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, MockResponseStatus: 200, ExpectedRequestBody: `{"to":"250788123123","type":"template","template":{"namespace":"wa_template_namespace","name":"revive_issue","language":{"policy":"deterministic","code":"en_US"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`, @@ -770,7 +770,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" }, "variables": ["Chef", "tomorrow"]}}`), + MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body","text": "Hi {{1}}, are you still experiencing problems with {{2}}?","parameters": [{"type": "text", "text": "Chef"},{"type": "text", "text": "tomorrow"}],"example": {"body_text": [["Bob","Product_A"]]}}]}}`), 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"}]}]}}`, @@ -1070,21 +1070,6 @@ var mediaCacheSendTestCases = []OutgoingTestCase{ }, } -var hsmSupportSendTestCases = []OutgoingTestCase{ - { - Label: "Template Send", - MsgText: "templated message", - MsgURN: "whatsapp:250788123123", - MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "language": "eng", "variables": ["Chef", "tomorrow"]}}`), - MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, - MockResponseStatus: 200, - ExpectedRequestBody: `{"to":"250788123123","type":"hsm","hsm":{"namespace":"waba_namespace","element_name":"revive_issue","language":{"policy":"deterministic","code":"en"},"localizable_params":[{"default":"Chef"},{"default":"tomorrow"}]}}`, - ExpectedMsgStatus: "W", - ExpectedExternalID: "157b5e14568e8", - SendPrep: setSendURL, - }, -} - func mockAttachmentURLs(mediaServer *httptest.Server, testCases []OutgoingTestCase) []OutgoingTestCase { casesWithMockedUrls := make([]OutgoingTestCase, len(testCases)) @@ -1108,15 +1093,6 @@ func TestOutgoing(t *testing.T) { "version": "v2.35.2", }) - var hsmSupportChannel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "WA", "250788383383", "US", - map[string]any{ - "auth_token": "token123", - "base_url": "https://foo.bar/", - "fb_namespace": "waba_namespace", - "hsm_support": true, - "version": "v2.35.2", - }) - var d3Channel = test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c56ab", "D3", "250788383383", "US", map[string]any{ "auth_token": "token123", @@ -1134,7 +1110,6 @@ func TestOutgoing(t *testing.T) { }) RunOutgoingTestCases(t, defaultChannel, newWAHandler(courier.ChannelType("WA"), "WhatsApp"), defaultSendTestCases, []string{"token123"}, nil) - RunOutgoingTestCases(t, hsmSupportChannel, newWAHandler(courier.ChannelType("WA"), "WhatsApp"), hsmSupportSendTestCases, []string{"token123"}, nil) RunOutgoingTestCases(t, d3Channel, newWAHandler(courier.ChannelType("D3"), "360Dialog"), defaultSendTestCases, []string{"token123"}, nil) RunOutgoingTestCases(t, txwChannel, newWAHandler(courier.ChannelType("TXW"), "TextIt"), defaultSendTestCases, []string{"token123"}, nil)