Skip to content

Commit

Permalink
Merge pull request #752 from nyaruka/split-template-attachments
Browse files Browse the repository at this point in the history
Use split attachments for templates media attachment for consistency …
  • Loading branch information
rowanseymour authored Jun 6, 2024
2 parents 6c87908 + 08e87fc commit 36c7a42
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 28 deletions.
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

0 comments on commit 36c7a42

Please sign in to comment.