Skip to content

Commit

Permalink
Use language value in templating metadata instead of trying to match
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 6, 2023
1 parent f58fa2a commit f7056ae
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 194 deletions.
5 changes: 2 additions & 3 deletions handlers/dialog360/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)
}
qrs := msg.QuickReplies()
lang := whatsapp.GetSupportedLanguage(msg.Locale())
menuButton := whatsapp.GetMenuButton(lang)
menuButton := handlers.GetText("Menu", msg.Locale())

var payloadAudio whatsapp.SendRequest

Expand All @@ -334,7 +333,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch

payload.Type = "template"

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

component := &whatsapp.Component{Type: "body"}
Expand Down
28 changes: 1 addition & 27 deletions handlers/dialog360/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,38 +409,12 @@ 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" }, "variables": ["Chef", "tomorrow"], "language": "en_US"}}`),
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"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
SendPrep: setSendURL,
},
{
Label: "Template Country Language",
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"]}}`),
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"}]}]}}`,
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
},
{
Label: "Template Invalid Language",
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"]}}`),
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"}]}]}}`,
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
},
{
Expand Down
25 changes: 25 additions & 0 deletions handlers/i18n.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package handlers

import "github.com/nyaruka/gocommon/i18n"

func GetText(text string, locale i18n.Locale) string {
if set, ok := translations[text]; ok {
lang, _ := locale.Split()
if trans := set[lang]; trans != "" {
return trans
}
}
return text
}

var translations = map[string]map[i18n.Language]string{
"Menu": {
"afr": "Kieslys",
"ara": "قائمة",
"zho": "菜单",
"heb": "תפריט",
"gle": "Roghchlár",
"spa": "Menú",
"swa": "Menyu",
},
}
16 changes: 16 additions & 0 deletions handlers/i18n_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package handlers_test

import (
"testing"

"github.com/nyaruka/courier/handlers"
"github.com/stretchr/testify/assert"
)

func TestGetText(t *testing.T) {
assert.Equal(t, "Menu", handlers.GetText("Menu", "eng"))
assert.Equal(t, "Menú", handlers.GetText("Menu", "spa"))
assert.Equal(t, "Menú", handlers.GetText("Menu", "spa-MX"))
assert.Equal(t, "Menyu", handlers.GetText("Menu", "swa"))
assert.Equal(t, "Foo", handlers.GetText("Foo", "eng"))
}
5 changes: 2 additions & 3 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,7 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, clog
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)
}
qrs := msg.QuickReplies()
lang := whatsapp.GetSupportedLanguage(msg.Locale())
menuButton := whatsapp.GetMenuButton(lang)
menuButton := handlers.GetText("Menu", msg.Locale())

var payloadAudio whatsapp.SendRequest

Expand All @@ -823,7 +822,7 @@ 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}}
template := whatsapp.Template{Name: templating.Template.Name, Language: &whatsapp.Language{Policy: "deterministic", Code: templating.Language}}
payload.Template = &template

component := &whatsapp.Component{Type: "body"}
Expand Down
28 changes: 1 addition & 27 deletions handlers/meta/whataspp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,38 +373,12 @@ 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" }, "variables": ["Chef", "tomorrow"], "language": "en_US"}}`),
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"},"components":[{"type":"body","sub_type":"","index":"","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]}]}}`,
SendPrep: setSendURL,
},
{
Label: "Template Country Language",
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"]}}`),
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"}]}]}}`,
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
},
{
Label: "Template Invalid Language",
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"]}}`),
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"}]}]}}`,
ExpectedMsgStatus: "W",
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
},
{
Expand Down
107 changes: 0 additions & 107 deletions handlers/meta/whatsapp/languages.go

This file was deleted.

27 changes: 0 additions & 27 deletions handlers/meta/whatsapp/languages_test.go

This file was deleted.

1 change: 1 addition & 0 deletions handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MsgTemplating struct {
} `json:"template" validate:"required,dive"`
Namespace string `json:"namespace"`
Variables []string `json:"variables"`
Language string `json:"language"`
}

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

0 comments on commit f7056ae

Please sign in to comment.