Skip to content

Commit

Permalink
Omit components when no params are available to pass to the components
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Nov 30, 2023
1 parent e4a7f71 commit 9d2da92
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
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, no variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {}, "variables": []}}`),
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"}}}`,
SendPrep: setSendURL,
},
{
Label: "Template Send, old variables",
MsgText: "templated message",
Expand Down
2 changes: 1 addition & 1 deletion handlers/meta/whatsapp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type Language struct {
type Template struct {
Name string `json:"name"`
Language *Language `json:"language"`
Components []*Component `json:"components"`
Components []*Component `json:"components,omitempty"`
}

// see https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#interactive-object
Expand Down
2 changes: 1 addition & 1 deletion handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func GetTemplatePayload(templating MsgTemplating, lang string) *Template {

}

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

for _, v := range templating.Variables {
Expand Down
4 changes: 2 additions & 2 deletions handlers/whatsapp_legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ type templatePayload struct {
Policy string `json:"policy"`
Code string `json:"code"`
} `json:"language"`
Components []Component `json:"components"`
Components []Component `json:"components,omitempty"`
} `json:"template"`
}

Expand Down Expand Up @@ -745,7 +745,7 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]

}

if len(templating.Params) == 0 {
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})
Expand Down
2 changes: 1 addition & 1 deletion handlers/whatsapp_legacy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ var defaultSendTestCases = []OutgoingTestCase{
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {}}}`),
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"}]}}`,
ExpectedRequestBody: `{"to":"250788123123","type":"template","template":{"namespace":"waba_namespace","name":"revive_issue","language":{"policy":"deterministic","code":"en"}}}`,
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
Expand Down

0 comments on commit 9d2da92

Please sign in to comment.