Skip to content

Commit 9d15c51

Browse files
committed
Adjust legacy WA channel handler to use templating components
1 parent 0c80de5 commit 9d15c51

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

Diff for: handlers/whatsapp_legacy/handler.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,10 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]
719719
payload.Template.Language.Policy = "deterministic"
720720
payload.Template.Language.Code = langCode
721721

722-
for k, v := range templating.Params {
723-
if k == "body" {
722+
for _, comp := range templating.Components {
723+
if comp.Type == "body" {
724724
component := &Component{Type: "body"}
725-
for _, p := range v {
725+
for _, p := range comp.Params {
726726
component.Parameters = append(component.Parameters, Param{Type: p.Type, Text: p.Value})
727727
}
728728
payload.Template.Components = append(payload.Template.Components, *component)
@@ -731,14 +731,6 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]
731731

732732
}
733733

734-
if len(templating.Params) == 0 && len(templating.Variables) != 0 {
735-
component := &Component{Type: "body"}
736-
for _, v := range templating.Variables {
737-
component.Parameters = append(component.Parameters, Param{Type: "text", Text: v})
738-
}
739-
payload.Template.Components = append(payload.Template.Components, *component)
740-
}
741-
742734
payloads = append(payloads, payload)
743735

744736
} else {
@@ -1116,12 +1108,15 @@ type MsgTemplating struct {
11161108
Name string `json:"name" validate:"required"`
11171109
UUID string `json:"uuid" validate:"required"`
11181110
} `json:"template" validate:"required,dive"`
1119-
Namespace string `json:"namespace"`
1120-
Variables []string `json:"variables"`
1121-
Params map[string][]struct {
1122-
Type string `json:"type"`
1123-
Value string `json:"value"`
1124-
} `json:"params"`
1111+
Namespace string `json:"namespace"`
1112+
Components []struct {
1113+
Type string `json:"type"`
1114+
Name string `json:"name"`
1115+
Params []struct {
1116+
Type string `json:"type"`
1117+
Value string `json:"value"`
1118+
} `json:"params"`
1119+
} `json:"components"`
11251120
}
11261121

11271122
func getSupportedLanguage(lc i18n.Locale) string {

Diff for: handlers/whatsapp_legacy/handler_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ var defaultSendTestCases = []OutgoingTestCase{
766766
MsgText: "templated message",
767767
MsgURN: "whatsapp:250788123123",
768768
MsgLocale: "eng",
769-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
769+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`),
770770
MockResponses: map[string][]*httpx.MockResponse{
771771
"*/v1/messages": {
772772
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -784,7 +784,7 @@ var defaultSendTestCases = []OutgoingTestCase{
784784
MsgText: "templated message",
785785
MsgURN: "whatsapp:250788123123",
786786
MsgLocale: "eng",
787-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {}}}`),
787+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": []}}`),
788788
MockResponses: map[string][]*httpx.MockResponse{
789789
"*/v1/messages": {
790790
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -802,7 +802,7 @@ var defaultSendTestCases = []OutgoingTestCase{
802802
MsgText: "templated message",
803803
MsgURN: "whatsapp:250788123123",
804804
MsgLocale: "eng-US",
805-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
805+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`),
806806
MockResponses: map[string][]*httpx.MockResponse{
807807
"*/v1/messages": {
808808
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -820,7 +820,7 @@ var defaultSendTestCases = []OutgoingTestCase{
820820
MsgText: "templated message",
821821
MsgURN: "whatsapp:250788123123",
822822
MsgLocale: "eng-US",
823-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
823+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`),
824824
MockResponses: map[string][]*httpx.MockResponse{
825825
"*/v1/messages": {
826826
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
@@ -838,7 +838,7 @@ var defaultSendTestCases = []OutgoingTestCase{
838838
MsgText: "templated message",
839839
MsgURN: "whatsapp:250788123123",
840840
MsgLocale: "bnt",
841-
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
841+
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}]}}`),
842842
MockResponses: map[string][]*httpx.MockResponse{
843843
"*/v1/messages": {
844844
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),

0 commit comments

Comments
 (0)