Skip to content

Commit 71bfd89

Browse files
committed
Update to use templating components with type
1 parent 2a7887a commit 71bfd89

File tree

4 files changed

+89
-60
lines changed

4 files changed

+89
-60
lines changed

Diff for: handlers/dialog360/handler_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ var SendTestCasesD3C = []OutgoingTestCase{
425425
MsgText: "templated message",
426426
MsgURN: "whatsapp:250788123123",
427427
MsgLocale: "eng",
428-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}, "language": "en_US"}}`),
428+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body", "name": "body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}}`),
429429
MockResponses: map[string][]*httpx.MockResponse{
430430
"https://waba-v2.360dialog.io/messages": {
431431
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),

Diff for: handlers/meta/whataspp_test.go

+38-26
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{
394394
MsgText: "templated message",
395395
MsgURN: "whatsapp:250788123123",
396396
MsgLocale: "eng",
397-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}, "language": "en_US"}}`),
397+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "name": "body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}}`),
398398
MockResponses: map[string][]*httpx.MockResponse{
399399
"*/12345_ID/messages": {
400400
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -410,7 +410,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{
410410
MsgText: "templated message",
411411
MsgURN: "whatsapp:250788123123",
412412
MsgLocale: "eng",
413-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {}, "variables": [], "language": "en_US"}}`),
413+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [], "variables": [], "language": "en_US"}}`),
414414
MockResponses: map[string][]*httpx.MockResponse{
415415
"*/12345_ID/messages": {
416416
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -426,30 +426,42 @@ var whatsappOutgoingTests = []OutgoingTestCase{
426426
MsgText: "templated message",
427427
MsgURN: "whatsapp:250788123123",
428428
MsgLocale: "eng",
429-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {
430-
"body": [
431-
{
432-
"type": "text",
433-
"value": "Ryan Lewis"
434-
},
435-
{
436-
"type": "text",
437-
"value": "niño"
438-
}
439-
],
440-
"button.0": [
441-
{
442-
"type": "text",
443-
"value": "Sip"
444-
}
445-
],
446-
"button.1": [
447-
{
448-
"type": "url",
449-
"value": "id00231"
450-
}
451-
]
452-
}, "language": "en_US"}}`),
429+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" },"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"}}`),
453465
MockResponses: map[string][]*httpx.MockResponse{
454466
"*/12345_ID/messages": {
455467
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),

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

+20-23
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ package whatsapp
22

33
import (
44
"encoding/json"
5-
"sort"
65
"strings"
76

87
"github.com/nyaruka/courier"
98
"github.com/nyaruka/courier/utils"
109
"github.com/pkg/errors"
11-
"golang.org/x/exp/maps"
1210
)
1311

1412
type MsgTemplating struct {
1513
Template struct {
1614
Name string `json:"name" validate:"required"`
1715
UUID string `json:"uuid" validate:"required"`
1816
} `json:"template" validate:"required,dive"`
19-
Namespace string `json:"namespace"`
20-
Params map[string][]struct {
21-
Type string `json:"type"`
22-
Value string `json:"value"`
23-
} `json:"params"`
17+
Namespace string `json:"namespace"`
18+
Components []struct {
19+
Type string `json:"type"`
20+
Name string `json:"name"`
21+
Params []struct {
22+
Type string `json:"type"`
23+
Value string `json:"value"`
24+
} `json:"params"`
25+
} `json:"components"`
2426
Language string `json:"language"`
2527
}
2628

@@ -54,17 +56,13 @@ func GetTemplatePayload(templating *MsgTemplating) *Template {
5456
Components: []*Component{},
5557
}
5658

57-
compKeys := maps.Keys(templating.Params)
58-
sort.Strings(compKeys) // so that final component order is deterministic
59-
60-
for _, k := range compKeys {
61-
v := templating.Params[k]
59+
for _, comp := range templating.Components {
6260
var component *Component
6361

64-
if k == "header" {
65-
component = &Component{Type: "header"}
62+
if comp.Name == "header" {
63+
component = &Component{Type: comp.Type}
6664

67-
for _, p := range v {
65+
for _, p := range comp.Params {
6866
if p.Type == "image" {
6967
component.Params = append(component.Params, &Param{Type: p.Type, Image: &struct {
7068
Link string "json:\"link,omitempty\""
@@ -81,18 +79,17 @@ func GetTemplatePayload(templating *MsgTemplating) *Template {
8179
component.Params = append(component.Params, &Param{Type: p.Type, Text: p.Value})
8280
}
8381
}
84-
} else if k == "body" {
85-
component = &Component{Type: "body"}
82+
} else if comp.Name == "body" {
83+
component = &Component{Type: comp.Type}
8684

87-
for _, p := range v {
85+
for _, p := range comp.Params {
8886
component.Params = append(component.Params, &Param{Type: p.Type, Text: p.Value})
8987
}
90-
} else if strings.HasPrefix(k, "button.") {
91-
component = &Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "quick_reply", Params: []*Param{}}
88+
} else if strings.HasPrefix(comp.Name, "button.") {
89+
component = &Component{Type: "button", Index: strings.TrimPrefix(comp.Name, "button."), SubType: strings.TrimPrefix(comp.Type, "button/"), Params: []*Param{}}
9290

93-
for _, p := range v {
94-
if p.Type == "url" {
95-
component.SubType = "url"
91+
for _, p := range comp.Params {
92+
if comp.Type == "button/url" {
9693
component.Params = append(component.Params, &Param{Type: "text", Text: p.Value})
9794
} else {
9895
component.Params = append(component.Params, &Param{Type: "payload", Payload: p.Value})

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

+30-10
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,49 @@ func TestGetTemplatePayload(t *testing.T) {
6464
templating: `{
6565
"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"},
6666
"language": "en",
67-
"params": {
68-
"header": [{"type": "text", "value": "Welome"}],
69-
"body": [{"type": "text", "value": "Hello"}, {"type": "text", "value": "Bob"}]
70-
}
67+
"components": [
68+
{
69+
"type": "header",
70+
"name": "header",
71+
"params": [{"type": "text", "value": "Welcome"}]
72+
},
73+
{
74+
"type": "body",
75+
"name": "body",
76+
"params": [{"type": "text", "value": "Hello"}, {"type": "text", "value": "Bob"}]
77+
}
78+
]
7179
}`,
7280
expected: &whatsapp.Template{
7381
Name: "Update",
7482
Language: &whatsapp.Language{Policy: "deterministic", Code: "en"},
7583
Components: []*whatsapp.Component{
84+
{Type: "header", Params: []*whatsapp.Param{{Type: "text", Text: "Welcome"}}},
7685
{Type: "body", Params: []*whatsapp.Param{{Type: "text", Text: "Hello"}, {Type: "text", Text: "Bob"}}},
77-
{Type: "header", Params: []*whatsapp.Param{{Type: "text", Text: "Welome"}}},
7886
},
7987
},
8088
},
8189
{
8290
templating: `{
8391
"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"},
8492
"language": "en",
85-
"params": {
86-
"button.1": [{"type": "text", "value": "No"}],
87-
"button.0": [{"type": "text", "value": "Yes"}, {"type": "text", "value": "Bob"}],
88-
"button.2": [{"type": "url", "value": "id0023"}]
89-
}
93+
"components": [
94+
{
95+
"type": "button/quick_reply",
96+
"name": "button.0",
97+
"params": [{"type": "text", "value": "Yes"}, {"type": "text", "value": "Bob"}]
98+
},
99+
{
100+
"type": "button/quick_reply",
101+
"name": "button.1",
102+
"params" : [{"type": "text", "value": "No"}]
103+
},
104+
{
105+
"type": "button/url",
106+
"name": "button.2",
107+
"params": [{"type": "url", "value": "id0023"}]
108+
}
109+
]
90110
}`,
91111
expected: &whatsapp.Template{
92112
Name: "Update",

0 commit comments

Comments
 (0)