Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use split attachments for templates media attachment for consistency … #752

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"golang.org/x/exp/maps"
)

Expand All @@ -30,18 +31,23 @@ func GetTemplatePayload(templating *courier.Templating) *Template {
component = &Component{Type: comp.Type}

for _, p := range compParams {
if p.Type == "image" {
component.Params = append(component.Params, &Param{Type: p.Type, Image: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else if p.Type == "video" {
component.Params = append(component.Params, &Param{Type: p.Type, Video: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
} else if p.Type == "document" {
component.Params = append(component.Params, &Param{Type: p.Type, Document: &struct {
Link string "json:\"link,omitempty\""
}{Link: p.Value}})
if p.Type != "text" {
attType, attURL := handlers.SplitAttachment(p.Value)
attType = strings.Split(attType, "/")[0]

if attType == "image" {
component.Params = append(component.Params, &Param{Type: "image", Image: &struct {
Link string "json:\"link,omitempty\""
}{Link: attURL}})
} else if attType == "video" {
component.Params = append(component.Params, &Param{Type: "video", Video: &struct {
Link string "json:\"link,omitempty\""
}{Link: attURL}})
} else if attType == "application" {
component.Params = append(component.Params, &Param{Type: "document", Document: &struct {
Link string "json:\"link,omitempty\""
}{Link: attURL}})
}
} else {
component.Params = append(component.Params, &Param{Type: p.Type, Text: p.Value})
}
Expand Down
93 changes: 77 additions & 16 deletions handlers/meta/whatsapp/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,96 @@ func TestGetTemplatePayload(t *testing.T) {
"language": "en",
"components": [
{
"type": "button/quick_reply",
"name": "button.0",
"variables": {"1": 0, "2": 1}
"type": "header",
"name": "header",
"variables": {"1": 0}
},
{
"type": "button/quick_reply",
"name": "button.1",
"variables": {"1": 2}
"type": "body",
"name": "body",
"variables": {"1": 1, "2": 2}
}
],
"variables": [
{"type": "image", "value": "image/jpeg:http://example.com/cat2.jpg"},
{"type": "text", "value": "Hello"},
{"type": "text", "value": "Bob"}
]
}`,
expected: &whatsapp.Template{
Name: "Update",
Language: &whatsapp.Language{Policy: "deterministic", Code: "en"},
Components: []*whatsapp.Component{
{Type: "header", Params: []*whatsapp.Param{{Type: "image", Image: &struct {
Link string "json:\"link,omitempty\""
}{Link: "http://example.com/cat2.jpg"}}}},
{Type: "body", Params: []*whatsapp.Param{{Type: "text", Text: "Hello"}, {Type: "text", Text: "Bob"}}},
},
},
},
{
templating: `{
"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"},
"language": "en",
"components": [
{
"type": "header",
"name": "header",
"variables": {"1": 0}
},
{
"type": "button/url",
"name": "button.2",
"variables": {"1": 3}
"type": "body",
"name": "body",
"variables": {"1": 1, "2": 2}
}
],
"variables": [
{"type": "text", "value": "Yes"},
{"type": "text", "value": "Bob"},
{"type": "text", "value": "No"},
{"type": "text", "value": "id0023"}
{"type": "video", "value": "video/mp4:http://example.com/video.mp4"},
{"type": "text", "value": "Hello"},
{"type": "text", "value": "Bob"}
]
}`,
expected: &whatsapp.Template{
Name: "Update",
Language: &whatsapp.Language{Policy: "deterministic", Code: "en"},
Components: []*whatsapp.Component{
{Type: "header", Params: []*whatsapp.Param{{Type: "video", Video: &struct {
Link string "json:\"link,omitempty\""
}{Link: "http://example.com/video.mp4"}}}},
{Type: "body", Params: []*whatsapp.Param{{Type: "text", Text: "Hello"}, {Type: "text", Text: "Bob"}}},
},
},
},
{
templating: `{
"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"},
"language": "en",
"components": [
{
"type": "header",
"name": "header",
"variables": {"1": 0}
},
{
"type": "body",
"name": "body",
"variables": {"1": 1, "2": 2}
}
],
"variables": [
{"type": "document", "value": "application/pdf:http://example.com/doc.pdf"},
{"type": "text", "value": "Hello"},
{"type": "text", "value": "Bob"}
]
}`,
expected: &whatsapp.Template{
Name: "Update",
Language: &whatsapp.Language{Policy: "deterministic", Code: "en"},
Components: []*whatsapp.Component{
{Type: "button", SubType: "quick_reply", Index: "0", Params: []*whatsapp.Param{{Type: "payload", Payload: "Yes"}, {Type: "payload", Payload: "Bob"}}},
{Type: "button", SubType: "quick_reply", Index: "1", Params: []*whatsapp.Param{{Type: "payload", Payload: "No"}}},
{Type: "button", SubType: "url", Index: "2", Params: []*whatsapp.Param{{Type: "text", Text: "id0023"}}},
{Type: "header", Params: []*whatsapp.Param{{Type: "document", Document: &struct {
Link string "json:\"link,omitempty\""
}{Link: "http://example.com/doc.pdf"}}}},
{Type: "body", Params: []*whatsapp.Param{{Type: "text", Text: "Hello"}, {Type: "text", Text: "Bob"}}},
},
},
},
Expand Down
Loading