From a9bdd48abd1013fad7c34d654a70150806e4dc7d Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 15 Mar 2024 13:34:46 +0200 Subject: [PATCH 1/3] Make parameter with type url to be sent as url buttons --- handlers/meta/whatsapp/templates.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/meta/whatsapp/templates.go b/handlers/meta/whatsapp/templates.go index a9c555117..3105c6c7f 100644 --- a/handlers/meta/whatsapp/templates.go +++ b/handlers/meta/whatsapp/templates.go @@ -52,9 +52,9 @@ func GetTemplatePayload(templating MsgTemplating) *Template { if strings.HasPrefix(k, "button.") { for _, p := range v { - if strings.HasPrefix(p.Value, "http") { + if p.Type == "url" || strings.HasPrefix(p.Value, "http") { component := &Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "quick_reply"} - component.Params = append(component.Params, &Param{Type: "url", Text: p.Value}) + component.Params = append(component.Params, &Param{Type: "text", Text: p.Value}) template.Components = append(template.Components, component) } else { component := &Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "quick_reply"} From 8e6d57d814ac0b8efc488c6b4074df8e39fb4e88 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 15 Mar 2024 14:13:28 +0200 Subject: [PATCH 2/3] Add tests for templating with params --- handlers/meta/whataspp_test.go | 39 ++++++++++++++++++++++++ handlers/meta/whatsapp/templates_test.go | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/handlers/meta/whataspp_test.go b/handlers/meta/whataspp_test.go index a52b93cef..1baf97a94 100644 --- a/handlers/meta/whataspp_test.go +++ b/handlers/meta/whataspp_test.go @@ -421,6 +421,45 @@ var whatsappOutgoingTests = []OutgoingTestCase{ }}, ExpectedExtIDs: []string{"157b5e14568e8"}, }, + { + Label: "Template Send, buttons params", + 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": "Ryan Lewis" + }, + { + "type": "text", + "value": "niño" + } + ], + "button.0": [ + { + "type": "text", + "value": "Sip" + } + ], + "button.1": [ + { + "type": "url", + "value": "id00231" + } + ] + }, "language": "en_US"}}`), + MockResponses: map[string][]*httpx.MockResponse{ + "*/12345_ID/messages": { + httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)), + }, + }, + ExpectedRequests: []ExpectedRequest{{ + Body: `{"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":"Ryan Lewis"},{"type":"text","text":"niño"}]},{"type":"button","sub_type":"quick_reply","index":"0","parameters":[{"type":"payload","payload":"Sip"}]},{"type":"button","sub_type":"quick_reply","index":"1","parameters":[{"type":"text","text":"id00231"}]}]}}`, + }}, + ExpectedExtIDs: []string{"157b5e14568e8"}, + }, { Label: "Interactive Button Message Send", MsgText: "Interactive Button Msg", diff --git a/handlers/meta/whatsapp/templates_test.go b/handlers/meta/whatsapp/templates_test.go index 19c3d81ac..34000cf60 100644 --- a/handlers/meta/whatsapp/templates_test.go +++ b/handlers/meta/whatsapp/templates_test.go @@ -33,7 +33,7 @@ func TestGetTemplating(t *testing.T) { msg.WithMetadata(json.RawMessage(`{"templating": {"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"}}}`)) - // invalid templating in metadata, error + // valid templating, no error tpl, err = whatsapp.GetTemplating(msg) assert.NoError(t, err) assert.Equal(t, "4ed5000f-5c94-4143-9697-b7cbd230a381", tpl.Template.UUID) From 4a570e6a29da3f17b3cc0fed2ff902a535a13554 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 15 Mar 2024 15:10:20 +0200 Subject: [PATCH 3/3] Adjust if block condition for URL buttons --- handlers/meta/whatsapp/templates.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/meta/whatsapp/templates.go b/handlers/meta/whatsapp/templates.go index 3105c6c7f..b366ec868 100644 --- a/handlers/meta/whatsapp/templates.go +++ b/handlers/meta/whatsapp/templates.go @@ -52,7 +52,7 @@ func GetTemplatePayload(templating MsgTemplating) *Template { if strings.HasPrefix(k, "button.") { for _, p := range v { - if p.Type == "url" || strings.HasPrefix(p.Value, "http") { + if p.Type == "url" { component := &Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "quick_reply"} component.Params = append(component.Params, &Param{Type: "text", Text: p.Value}) template.Components = append(template.Components, component)