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 variables in templating instead of params #730

Merged
merged 1 commit into from
Apr 17, 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
15 changes: 10 additions & 5 deletions handlers/dialog360/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,16 @@ var SendTestCasesD3C = []OutgoingTestCase{
ExpectedExtIDs: []string{"157b5e14568e8"},
},
{
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}`,
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
"components": [{"type": "body", "name": "body", "variables": {"1": 0, "2": 1}}],
"variables": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}],
"language": "en_US"
}`,
MockResponses: map[string][]*httpx.MockResponse{
"https://waba-v2.360dialog.io/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand Down
83 changes: 37 additions & 46 deletions handlers/meta/whataspp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,21 @@ var whatsappOutgoingTests = []OutgoingTestCase{
ExpectedExtIDs: []string{"157b5e14568e8"},
},
{
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}`,
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
"components": [
{"type": "body", "name": "body", "variables": {"1": 0, "2": 1}}
],
"variables": [
{"type": "text", "value": "Chef"},
{"type": "text" , "value": "tomorrow"}
],
"language": "en_US"
}`,
MockResponses: map[string][]*httpx.MockResponse{
"*/12345_ID/messages": {
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -405,11 +415,14 @@ var whatsappOutgoingTests = []OutgoingTestCase{
ExpectedExtIDs: []string{"157b5e14568e8"},
},
{
Label: "Template Send, no variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [], "variables": [], "language": "en_US"}`,
Label: "Template Send, no variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
"language": "en_US"
}`,
MockResponses: map[string][]*httpx.MockResponse{
"*/12345_ID/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -425,43 +438,21 @@ var whatsappOutgoingTests = []OutgoingTestCase{
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" },
MsgTemplating: `{
"template": {"uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3", "name": "revive_issue"},
"components": [
{
"type": "body",
"name": "body",
"params": [
{
"type": "text",
"value": "Ryan Lewis"
},
{
"type": "text",
"value": "niño"
}
]
},
{
"type": "button/quick_reply",
"name": "button.0",
"params": [
{
"type": "text",
"value": "Sip"
}
]
},
{
"type": "button/url",
"name": "button.1",
"params": [
{
"type": "url",
"value": "id00231"
}
]
}
], "language": "en_US"}`,
{"type": "body", "name": "body", "variables": {"1": 0, "2": 1}},
{"type": "button/quick_reply", "name": "button.0", "variables": {"1": 2}},
{"type": "button/url", "name": "button.1", "variables": {"1": 3}}
],
"variables": [
{"type": "text", "value": "Ryan Lewis"},
{"type": "text", "value": "niño"},
{"type": "text", "value": "Sip"},
{"type": "text", "value": "id00231"}
],
"language": "en_US"
}`,
MockResponses: map[string][]*httpx.MockResponse{
"*/12345_ID/messages": {
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand Down
16 changes: 13 additions & 3 deletions handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package whatsapp

import (
"sort"
"strings"

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

func GetTemplatePayload(templating *courier.Templating) *Template {
Expand All @@ -14,12 +16,20 @@ func GetTemplatePayload(templating *courier.Templating) *Template {
}

for _, comp := range templating.Components {
// get the variables used by this component in order of their names 1, 2 etc
compParams := make([]courier.TemplatingVariable, 0, len(comp.Variables))
varNames := maps.Keys(comp.Variables)
sort.Strings(varNames)
for _, varName := range varNames {
compParams = append(compParams, templating.Variables[comp.Variables[varName]])
}

var component *Component

if comp.Type == "header" {
component = &Component{Type: comp.Type}

for _, p := range comp.Params {
for _, p := range compParams {
if p.Type == "image" {
component.Params = append(component.Params, &Param{Type: p.Type, Image: &struct {
Link string "json:\"link,omitempty\""
Expand All @@ -39,13 +49,13 @@ func GetTemplatePayload(templating *courier.Templating) *Template {
} else if comp.Type == "body" {
component = &Component{Type: comp.Type}

for _, p := range comp.Params {
for _, p := range compParams {
component.Params = append(component.Params, &Param{Type: p.Type, Text: p.Value})
}
} else if strings.HasPrefix(comp.Type, "button/") {
component = &Component{Type: "button", Index: strings.TrimPrefix(comp.Name, "button."), SubType: strings.TrimPrefix(comp.Type, "button/"), Params: []*Param{}}

for _, p := range comp.Params {
for _, p := range compParams {
if comp.Type == "button/url" {
component.Params = append(component.Params, &Param{Type: "text", Text: p.Value})
} else {
Expand Down
28 changes: 19 additions & 9 deletions handlers/meta/whatsapp/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ func TestGetTemplatePayload(t *testing.T) {
templating: `{
"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"},
"namespace": "12345",
"language": "en",
"params": {}
"language": "en"
}`,
expected: &whatsapp.Template{
Name: "Update",
Expand All @@ -36,13 +35,18 @@ func TestGetTemplatePayload(t *testing.T) {
{
"type": "header",
"name": "header",
"params": [{"type": "text", "value": "Welcome"}]
"variables": {"1": 0}
},
{
"type": "body",
"name": "body",
"params": [{"type": "text", "value": "Hello"}, {"type": "text", "value": "Bob"}]
"variables": {"1": 1, "2": 2}
}
],
"variables": [
{"type": "text", "value": "Welcome"},
{"type": "text", "value": "Hello"},
{"type": "text", "value": "Bob"}
]
}`,
expected: &whatsapp.Template{
Expand All @@ -62,18 +66,24 @@ func TestGetTemplatePayload(t *testing.T) {
{
"type": "button/quick_reply",
"name": "button.0",
"params": [{"type": "text", "value": "Yes"}, {"type": "text", "value": "Bob"}]
"variables": {"1": 0, "2": 1}
},
{
"type": "button/quick_reply",
"name": "button.1",
"params" : [{"type": "text", "value": "No"}]
"variables": {"1": 2}
},
{
"type": "button/url",
"name": "button.2",
"params": [{"type": "url", "value": "id0023"}]
"variables": {"1": 3}
}
],
"variables": [
{"type": "text", "value": "Yes"},
{"type": "text", "value": "Bob"},
{"type": "text", "value": "No"},
{"type": "text", "value": "id0023"}
]
}`,
expected: &whatsapp.Template{
Expand All @@ -88,13 +98,13 @@ func TestGetTemplatePayload(t *testing.T) {
},
}

for _, tc := range tcs {
for i, tc := range tcs {
templating := &courier.Templating{}
jsonx.MustUnmarshal([]byte(tc.templating), templating)

msg := test.NewMockMsg(1, "87995844-2017-4ba0-bc73-f3da75b32f9b", nil, "tel:+1234567890", "hi", nil).WithTemplating(templating)
actual := whatsapp.GetTemplatePayload(msg.Templating())

assert.Equal(t, tc.expected, actual)
assert.Equal(t, tc.expected, actual, "%d: template payload mismatch", i)
}
}
12 changes: 11 additions & 1 deletion handlers/whatsapp_legacy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/nyaruka/redisx"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"golang.org/x/mod/semver"
)

Expand Down Expand Up @@ -716,9 +718,17 @@ func buildPayloads(msg courier.MsgOut, h *handler, clog *courier.ChannelLog) ([]
payload.Template.Language.Code = langCode

for _, comp := range msg.Templating().Components {
// get the variables used by this component in order of their names 1, 2 etc
compParams := make([]courier.TemplatingVariable, 0, len(comp.Variables))
varNames := maps.Keys(comp.Variables)
sort.Strings(varNames)
for _, varName := range varNames {
compParams = append(compParams, msg.Templating().Variables[comp.Variables[varName]])
}

if comp.Type == "body" {
component := &Component{Type: "body"}
for _, p := range comp.Params {
for _, p := range compParams {
component.Parameters = append(component.Parameters, Param{Type: p.Type, Text: p.Value})
}
payload.Template.Components = append(payload.Template.Components, *component)
Expand Down
Loading
Loading