Skip to content

Commit 5e66496

Browse files
authored
Merge pull request #730 from nyaruka/params_to_variables
Use variables in templating instead of params
2 parents 9be759a + b127728 commit 5e66496

File tree

7 files changed

+167
-97
lines changed

7 files changed

+167
-97
lines changed

Diff for: handlers/dialog360/handler_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,16 @@ var SendTestCasesD3C = []OutgoingTestCase{
420420
ExpectedExtIDs: []string{"157b5e14568e8"},
421421
},
422422
{
423-
Label: "Template Send",
424-
MsgText: "templated message",
425-
MsgURN: "whatsapp:250788123123",
426-
MsgLocale: "eng",
427-
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}`,
423+
Label: "Template Send",
424+
MsgText: "templated message",
425+
MsgURN: "whatsapp:250788123123",
426+
MsgLocale: "eng",
427+
MsgTemplating: `{
428+
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
429+
"components": [{"type": "body", "name": "body", "variables": {"1": 0, "2": 1}}],
430+
"variables": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}],
431+
"language": "en_US"
432+
}`,
428433
MockResponses: map[string][]*httpx.MockResponse{
429434
"https://waba-v2.360dialog.io/messages": {
430435
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),

Diff for: handlers/meta/whataspp_test.go

+37-46
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,21 @@ var whatsappOutgoingTests = []OutgoingTestCase{
389389
ExpectedExtIDs: []string{"157b5e14568e8"},
390390
},
391391
{
392-
Label: "Template Send",
393-
MsgText: "templated message",
394-
MsgURN: "whatsapp:250788123123",
395-
MsgLocale: "eng",
396-
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}`,
392+
Label: "Template Send",
393+
MsgText: "templated message",
394+
MsgURN: "whatsapp:250788123123",
395+
MsgLocale: "eng",
396+
MsgTemplating: `{
397+
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
398+
"components": [
399+
{"type": "body", "name": "body", "variables": {"1": 0, "2": 1}}
400+
],
401+
"variables": [
402+
{"type": "text", "value": "Chef"},
403+
{"type": "text" , "value": "tomorrow"}
404+
],
405+
"language": "en_US"
406+
}`,
397407
MockResponses: map[string][]*httpx.MockResponse{
398408
"*/12345_ID/messages": {
399409
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -405,11 +415,14 @@ var whatsappOutgoingTests = []OutgoingTestCase{
405415
ExpectedExtIDs: []string{"157b5e14568e8"},
406416
},
407417
{
408-
Label: "Template Send, no variables",
409-
MsgText: "templated message",
410-
MsgURN: "whatsapp:250788123123",
411-
MsgLocale: "eng",
412-
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [], "variables": [], "language": "en_US"}`,
418+
Label: "Template Send, no variables",
419+
MsgText: "templated message",
420+
MsgURN: "whatsapp:250788123123",
421+
MsgLocale: "eng",
422+
MsgTemplating: `{
423+
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
424+
"language": "en_US"
425+
}`,
413426
MockResponses: map[string][]*httpx.MockResponse{
414427
"*/12345_ID/messages": {
415428
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -425,43 +438,21 @@ var whatsappOutgoingTests = []OutgoingTestCase{
425438
MsgText: "templated message",
426439
MsgURN: "whatsapp:250788123123",
427440
MsgLocale: "eng",
428-
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" },
441+
MsgTemplating: `{
442+
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
429443
"components": [
430-
{
431-
"type": "body",
432-
"name": "body",
433-
"params": [
434-
{
435-
"type": "text",
436-
"value": "Ryan Lewis"
437-
},
438-
{
439-
"type": "text",
440-
"value": "niño"
441-
}
442-
]
443-
},
444-
{
445-
"type": "button/quick_reply",
446-
"name": "button.0",
447-
"params": [
448-
{
449-
"type": "text",
450-
"value": "Sip"
451-
}
452-
]
453-
},
454-
{
455-
"type": "button/url",
456-
"name": "button.1",
457-
"params": [
458-
{
459-
"type": "url",
460-
"value": "id00231"
461-
}
462-
]
463-
}
464-
], "language": "en_US"}`,
444+
{"type": "body", "name": "body", "variables": {"1": 0, "2": 1}},
445+
{"type": "button/quick_reply", "name": "button.0", "variables": {"1": 2}},
446+
{"type": "button/url", "name": "button.1", "variables": {"1": 3}}
447+
],
448+
"variables": [
449+
{"type": "text", "value": "Ryan Lewis"},
450+
{"type": "text", "value": "niño"},
451+
{"type": "text", "value": "Sip"},
452+
{"type": "text", "value": "id00231"}
453+
],
454+
"language": "en_US"
455+
}`,
465456
MockResponses: map[string][]*httpx.MockResponse{
466457
"*/12345_ID/messages": {
467458
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),

Diff for: handlers/meta/whatsapp/templates.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package whatsapp
22

33
import (
4+
"sort"
45
"strings"
56

67
"github.com/nyaruka/courier"
8+
"golang.org/x/exp/maps"
79
)
810

911
func GetTemplatePayload(templating *courier.Templating) *Template {
@@ -14,12 +16,20 @@ func GetTemplatePayload(templating *courier.Templating) *Template {
1416
}
1517

1618
for _, comp := range templating.Components {
19+
// get the variables used by this component in order of their names 1, 2 etc
20+
compParams := make([]courier.TemplatingVariable, 0, len(comp.Variables))
21+
varNames := maps.Keys(comp.Variables)
22+
sort.Strings(varNames)
23+
for _, varName := range varNames {
24+
compParams = append(compParams, templating.Variables[comp.Variables[varName]])
25+
}
26+
1727
var component *Component
1828

1929
if comp.Type == "header" {
2030
component = &Component{Type: comp.Type}
2131

22-
for _, p := range comp.Params {
32+
for _, p := range compParams {
2333
if p.Type == "image" {
2434
component.Params = append(component.Params, &Param{Type: p.Type, Image: &struct {
2535
Link string "json:\"link,omitempty\""
@@ -39,13 +49,13 @@ func GetTemplatePayload(templating *courier.Templating) *Template {
3949
} else if comp.Type == "body" {
4050
component = &Component{Type: comp.Type}
4151

42-
for _, p := range comp.Params {
52+
for _, p := range compParams {
4353
component.Params = append(component.Params, &Param{Type: p.Type, Text: p.Value})
4454
}
4555
} else if strings.HasPrefix(comp.Type, "button/") {
4656
component = &Component{Type: "button", Index: strings.TrimPrefix(comp.Name, "button."), SubType: strings.TrimPrefix(comp.Type, "button/"), Params: []*Param{}}
4757

48-
for _, p := range comp.Params {
58+
for _, p := range compParams {
4959
if comp.Type == "button/url" {
5060
component.Params = append(component.Params, &Param{Type: "text", Text: p.Value})
5161
} else {

Diff for: handlers/meta/whatsapp/templates_test.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ func TestGetTemplatePayload(t *testing.T) {
1919
templating: `{
2020
"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"},
2121
"namespace": "12345",
22-
"language": "en",
23-
"params": {}
22+
"language": "en"
2423
}`,
2524
expected: &whatsapp.Template{
2625
Name: "Update",
@@ -36,13 +35,18 @@ func TestGetTemplatePayload(t *testing.T) {
3635
{
3736
"type": "header",
3837
"name": "header",
39-
"params": [{"type": "text", "value": "Welcome"}]
38+
"variables": {"1": 0}
4039
},
4140
{
4241
"type": "body",
4342
"name": "body",
44-
"params": [{"type": "text", "value": "Hello"}, {"type": "text", "value": "Bob"}]
43+
"variables": {"1": 1, "2": 2}
4544
}
45+
],
46+
"variables": [
47+
{"type": "text", "value": "Welcome"},
48+
{"type": "text", "value": "Hello"},
49+
{"type": "text", "value": "Bob"}
4650
]
4751
}`,
4852
expected: &whatsapp.Template{
@@ -62,18 +66,24 @@ func TestGetTemplatePayload(t *testing.T) {
6266
{
6367
"type": "button/quick_reply",
6468
"name": "button.0",
65-
"params": [{"type": "text", "value": "Yes"}, {"type": "text", "value": "Bob"}]
69+
"variables": {"1": 0, "2": 1}
6670
},
6771
{
6872
"type": "button/quick_reply",
6973
"name": "button.1",
70-
"params" : [{"type": "text", "value": "No"}]
74+
"variables": {"1": 2}
7175
},
7276
{
7377
"type": "button/url",
7478
"name": "button.2",
75-
"params": [{"type": "url", "value": "id0023"}]
79+
"variables": {"1": 3}
7680
}
81+
],
82+
"variables": [
83+
{"type": "text", "value": "Yes"},
84+
{"type": "text", "value": "Bob"},
85+
{"type": "text", "value": "No"},
86+
{"type": "text", "value": "id0023"}
7787
]
7888
}`,
7989
expected: &whatsapp.Template{
@@ -88,13 +98,13 @@ func TestGetTemplatePayload(t *testing.T) {
8898
},
8999
}
90100

91-
for _, tc := range tcs {
101+
for i, tc := range tcs {
92102
templating := &courier.Templating{}
93103
jsonx.MustUnmarshal([]byte(tc.templating), templating)
94104

95105
msg := test.NewMockMsg(1, "87995844-2017-4ba0-bc73-f3da75b32f9b", nil, "tel:+1234567890", "hi", nil).WithTemplating(templating)
96106
actual := whatsapp.GetTemplatePayload(msg.Templating())
97107

98-
assert.Equal(t, tc.expected, actual)
108+
assert.Equal(t, tc.expected, actual, "%d: template payload mismatch", i)
99109
}
100110
}

Diff for: handlers/whatsapp_legacy/handler.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log/slog"
99
"net/http"
1010
"net/url"
11+
"sort"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -24,6 +25,7 @@ import (
2425
"github.com/nyaruka/redisx"
2526
"github.com/patrickmn/go-cache"
2627
"github.com/pkg/errors"
28+
"golang.org/x/exp/maps"
2729
"golang.org/x/mod/semver"
2830
)
2931

@@ -716,9 +718,17 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]
716718
payload.Template.Language.Code = langCode
717719

718720
for _, comp := range msg.Templating().Components {
721+
// get the variables used by this component in order of their names 1, 2 etc
722+
compParams := make([]courier.TemplatingVariable, 0, len(comp.Variables))
723+
varNames := maps.Keys(comp.Variables)
724+
sort.Strings(varNames)
725+
for _, varName := range varNames {
726+
compParams = append(compParams, msg.Templating().Variables[comp.Variables[varName]])
727+
}
728+
719729
if comp.Type == "body" {
720730
component := &Component{Type: "body"}
721-
for _, p := range comp.Params {
731+
for _, p := range compParams {
722732
component.Parameters = append(component.Parameters, Param{Type: p.Type, Text: p.Value})
723733
}
724734
payload.Template.Components = append(payload.Template.Components, *component)

0 commit comments

Comments
 (0)