Skip to content

Commit

Permalink
Keep supporting templating variables until engine completely switch t…
Browse files Browse the repository at this point in the history
…o use only params
  • Loading branch information
norkans7 committed Nov 30, 2023
1 parent 741be31 commit e4a7f71
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
13 changes: 13 additions & 0 deletions handlers/dialog360/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,19 @@ var SendTestCasesD3C = []OutgoingTestCase{
ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
SendPrep: setSendURL,
},
{
Label: "Template Send, old variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`),
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`,
MockResponseStatus: 200,
ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
SendPrep: setSendURL,
},
{
Label: "Template Country Language",
MsgText: "templated message",
Expand Down
13 changes: 13 additions & 0 deletions handlers/meta/whataspp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,19 @@ var whatsappOutgoingTests = []OutgoingTestCase{
ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
SendPrep: setSendURL,
},
{
Label: "Template Send, old variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`),
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`,
MockResponseStatus: 200,
ExpectedRequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"template","template":{"name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
SendPrep: setSendURL,
},
{
Label: "Template Country Language",
MsgText: "templated message",
Expand Down
13 changes: 12 additions & 1 deletion handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type MsgTemplating struct {
Name string `json:"name" validate:"required"`
UUID string `json:"uuid" validate:"required"`
} `json:"template" validate:"required,dive"`
Namespace string `json:"namespace"`
Namespace string `json:"namespace"`
Variables []string `json:"variables"`
Params map[string][]struct {
Type string `json:"type"`
Value string `json:"value"`
Expand Down Expand Up @@ -94,6 +95,16 @@ func GetTemplatePayload(templating MsgTemplating, lang string) *Template {

}

if len(templating.Params) == 0 {
component := &Component{Type: "body"}

for _, v := range templating.Variables {
component.Params = append(component.Params, &Param{Type: "text", Text: v})
}
template.Components = append(template.Components, component)

Check warning on line 104 in handlers/meta/whatsapp/templates.go

View check run for this annotation

Codecov / codecov/patch

handlers/meta/whatsapp/templates.go#L98-L104

Added lines #L98 - L104 were not covered by tests

}

return &template

Check warning on line 108 in handlers/meta/whatsapp/templates.go

View check run for this annotation

Codecov / codecov/patch

handlers/meta/whatsapp/templates.go#L108

Added line #L108 was not covered by tests

}
6 changes: 5 additions & 1 deletion handlers/whatsapp_legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,9 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]

if len(templating.Params) == 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)
}

Expand Down Expand Up @@ -1127,7 +1130,8 @@ type MsgTemplating struct {
Name string `json:"name" validate:"required"`
UUID string `json:"uuid" validate:"required"`
} `json:"template" validate:"required,dive"`
Namespace string `json:"namespace"`
Namespace string `json:"namespace"`
Variables []string `json:"variables"`
Params map[string][]struct {
Type string `json:"type"`
Value string `json:"value"`
Expand Down
13 changes: 13 additions & 0 deletions handlers/whatsapp_legacy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,19 @@ var defaultSendTestCases = []OutgoingTestCase{
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
},
{
Label: "Template Send, old variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" },"variables": ["Chef", "tomorrow"]}}`),
MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`,
MockResponseStatus: 200,
ExpectedRequestBody: `{"to":"250788123123","type":"template","template":{"namespace":"waba_namespace","name":"revive_issue","language":{"policy":"deterministic","code":"en"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
},
{
Label: "Template Send no variables",
MsgText: "templated message",
Expand Down

0 comments on commit e4a7f71

Please sign in to comment.