Skip to content

Commit

Permalink
Prep for support for more WA templates components
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Nov 29, 2023
1 parent 7151820 commit c6cc2ef
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 20 deletions.
49 changes: 44 additions & 5 deletions handlers/dialog360/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,57 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch
return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID())
}
if templating != nil {

payload.Type = "template"

template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}}
payload.Template = &template

component := &whatsapp.Component{Type: "body"}
for k, v := range templating.Params {
if strings.HasPrefix(k, "button.") {

for _, p := range v {
if strings.HasPrefix(p.Value, "http") {
component := &whatsapp.Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "url"}
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
template.Components = append(template.Components, component)
} else {
component := &whatsapp.Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "payload"}
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
template.Components = append(template.Components, component)
}

Check warning on line 351 in handlers/dialog360/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dialog360/handler.go#L341-L351

Added lines #L341 - L351 were not covered by tests
}

} else if k == "header" {
component := &whatsapp.Component{Type: "header"}
for _, p := range v {
if p.Type == "image" {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Image: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else if p.Type == "video" {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Video: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else if p.Type == "document" {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Document: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
}

Check warning on line 371 in handlers/dialog360/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dialog360/handler.go#L355-L371

Added lines #L355 - L371 were not covered by tests
}
template.Components = append(template.Components, component)

Check warning on line 373 in handlers/dialog360/handler.go

View check run for this annotation

Codecov / codecov/patch

handlers/dialog360/handler.go#L373

Added line #L373 was not covered by tests

} else {
component := &whatsapp.Component{Type: "body"}
for _, p := range v {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
}
template.Components = append(template.Components, component)

}

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

} else {
if i < (len(msgParts) + len(msg.Attachments()) - 1) {
Expand Down
6 changes: 3 additions & 3 deletions handlers/dialog360/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ var SendTestCasesD3C = []OutgoingTestCase{
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"]}}`),
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`,
Expand All @@ -422,7 +422,7 @@ var SendTestCasesD3C = []OutgoingTestCase{
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng-US",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`),
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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_US"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
Expand All @@ -435,7 +435,7 @@ var SendTestCasesD3C = []OutgoingTestCase{
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "bnt",
MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "variables": ["Chef", "tomorrow"]}}`),
MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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"}]}]}}`,
Expand Down
51 changes: 46 additions & 5 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,15 +825,56 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog
payload.Type = "template"

template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: lang}}

payload.Template = &template

component := &whatsapp.Component{Type: "body"}
for k, v := range templating.Params {
if strings.HasPrefix(k, "button.") {

for _, p := range v {
if strings.HasPrefix(p.Value, "http") {
component := &whatsapp.Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "url"}
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
template.Components = append(template.Components, component)
} else {
component := &whatsapp.Component{Type: "button", Index: strings.TrimPrefix(k, "button."), SubType: "payload"}
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
template.Components = append(template.Components, component)
}

Check warning on line 843 in handlers/meta/handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers/meta/handlers.go#L833-L843

Added lines #L833 - L843 were not covered by tests
}

for _, v := range templating.Variables {
component.Params = append(component.Params, &whatsapp.Param{Type: "text", Text: v})
}
template.Components = append(payload.Template.Components, component)
} else if k == "header" {
component := &whatsapp.Component{Type: "header"}
for _, p := range v {
if p.Type == "image" {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Image: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else if p.Type == "video" {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Video: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else if p.Type == "document" {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Document: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
}

Check warning on line 863 in handlers/meta/handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers/meta/handlers.go#L847-L863

Added lines #L847 - L863 were not covered by tests

}
template.Components = append(template.Components, component)

Check warning on line 866 in handlers/meta/handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers/meta/handlers.go#L866

Added line #L866 was not covered by tests

} else {
component := &whatsapp.Component{Type: "body"}
for _, p := range v {
component.Params = append(component.Params, &whatsapp.Param{Type: p.Type, Text: p.Value})
}
template.Components = append(template.Components, component)

}

}
} else {
if i < (len(msgParts) + len(msg.Attachments()) - 1) {
// this is still a msg part
Expand Down
6 changes: 3 additions & 3 deletions handlers/meta/whataspp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{
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"]}}`),
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
MockResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`,
Expand All @@ -386,7 +386,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng-US",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "variables": ["Chef", "tomorrow"]}}`),
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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_US"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
Expand All @@ -399,7 +399,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "bnt",
MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "variables": ["Chef", "tomorrow"]}}`),
MsgMetadata: json.RawMessage(`{"templating": { "template": { "name": "revive_issue", "uuid": "8ca114b4-bee2-4d3b-aaf1-9aa6b48d41e8" }, "params": {"body": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}}}`),
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"}]}]}}`,
Expand Down
13 changes: 11 additions & 2 deletions handlers/meta/whatsapp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,17 @@ type Button struct {
}

type Param struct {
Type string `json:"type"`
Text string `json:"text"`
Type string `json:"type"`
Text string `json:"text"`
Image *struct {
Link string `json:"link,omitempty"`
} `json:"image,omitempty"`
Video *struct {
Link string `json:"link,omitempty"`
} `json:"video,omitempty"`
Document *struct {
Link string `json:"link,omitempty"`
} `json:"document,omitempty"`
}

type Component struct {
Expand Down
7 changes: 5 additions & 2 deletions handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ 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"`
Namespace string `json:"namespace"`
Params map[string][]struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"params"`
}

func GetTemplating(msg courier.MsgOut) (*MsgTemplating, error) {
Expand Down

0 comments on commit c6cc2ef

Please sign in to comment.