Skip to content

Commit

Permalink
Merge pull request #726 from nyaruka/fix-legacy-WA-variables
Browse files Browse the repository at this point in the history
Adjust legacy WA channel handler to use templating components
  • Loading branch information
rowanseymour authored Apr 3, 2024
2 parents 0c80de5 + 9d15c51 commit f3c6c96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
29 changes: 12 additions & 17 deletions handlers/whatsapp_legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,10 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]
payload.Template.Language.Policy = "deterministic"
payload.Template.Language.Code = langCode

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

}

if len(templating.Params) == 0 && len(templating.Variables) != 0 {
component := &Component{Type: "body"}
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)

} else {
Expand Down Expand Up @@ -1116,12 +1108,15 @@ 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"`
Params map[string][]struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"params"`
Namespace string `json:"namespace"`
Components []struct {
Type string `json:"type"`
Name string `json:"name"`
Params []struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"params"`
} `json:"components"`
}

func getSupportedLanguage(lc i18n.Locale) string {
Expand Down
10 changes: 5 additions & 5 deletions handlers/whatsapp_legacy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,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" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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"}]}]}}`),
MockResponses: map[string][]*httpx.MockResponse{
"*/v1/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -784,7 +784,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" }, "params": {}}}`),
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": []}}`),
MockResponses: map[string][]*httpx.MockResponse{
"*/v1/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -802,7 +802,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" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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"}]}]}}`),
MockResponses: map[string][]*httpx.MockResponse{
"*/v1/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -820,7 +820,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", "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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"}]}]}}`),
MockResponses: map[string][]*httpx.MockResponse{
"*/v1/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -838,7 +838,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" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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"}]}]}}`),
MockResponses: map[string][]*httpx.MockResponse{
"*/v1/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand Down

0 comments on commit f3c6c96

Please sign in to comment.