Skip to content

Commit

Permalink
Merge pull request #139 from Ilhasoft/feat/language-metadata
Browse files Browse the repository at this point in the history
Add "Menu" word translation mapping to list messages in WAC and WA channels
  • Loading branch information
Robi9 authored Dec 23, 2022
2 parents 57e3df7 + 9a845e1 commit 41d43b6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 9 deletions.
13 changes: 13 additions & 0 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ type DBMsg struct {
workerToken queue.WorkerToken
alreadyWritten bool
quickReplies []string
textLanguage string
}

func (m *DBMsg) ID() courier.MsgID { return m.ID_ }
Expand Down Expand Up @@ -585,6 +586,18 @@ func (m *DBMsg) Topic() string {
return string(topic)
}

func (m *DBMsg) TextLanguage() string {
if m.textLanguage != "" {
return m.textLanguage
}
if m.Metadata_ == nil {
return ""
}

textLanguage, _, _, _ := jsonparser.Get(m.Metadata_, "text_language")
return string(textLanguage)
}

// Metadata returns the metadata for this message
func (m *DBMsg) Metadata() json.RawMessage {
return m.Metadata_
Expand Down
34 changes: 34 additions & 0 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,10 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
section,
}}

if msg.TextLanguage() != "" {
interactive.Action.Button = languageMenuMap[msg.TextLanguage()]
}

payload.Interactive = &interactive
} else {
return nil, fmt.Errorf("too many quick replies WAC supports only up to 10 quick replies")
Expand Down Expand Up @@ -1864,3 +1868,33 @@ var languageMap = map[string]string{
"vie": "vi", // Vietnamese
"zul": "zu", // Zulu
}

// iso language code mapping to respective "Menu" word translation
var languageMenuMap = map[string]string{
"da-DK": "Menu",
"de-DE": "Speisekarte",
"en-AU": "Menu",
"en-CA": "Menu",
"en-GB": "Menu",
"en-IN": "Menu",
"en-US": "Menu",
"ca-ES": "Menú",
"es-ES": "Menú",
"es-MX": "Menú",
"fi-FI": "Valikko",
"fr-CA": "Menu",
"fr-FR": "Menu",
"it-IT": "Menù",
"ja-JP": "メニュー",
"ko-KR": "메뉴",
"nb-NO": "Meny",
"nl-NL": "Menu",
"pl-PL": "Menu",
"pt-BR": "Menu",
"ru-RU": "Меню",
"sv-SE": "Meny",
"zh-CN": "菜单",
"zh-HK": "菜單",
"zh-TW": "菜單",
"ar-JO": "قائمة الطعام",
}
4 changes: 2 additions & 2 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ var SendTestCasesWAC = []ChannelSendTestCase{
SendPrep: setSendURL},
{Label: "Interactive List Message Send",
Text: "Interactive List Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"ROW1", "ROW2", "ROW3", "ROW4"},
Status: "W", ExternalID: "157b5e14568e8",
Status: "W", ExternalID: "157b5e14568e8", TextLanguage: "en-US",
ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201,
RequestBody: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"list","body":{"text":"Interactive List Msg"},"action":{"button":"Menu","sections":[{"rows":[{"id":"0","title":"ROW1"},{"id":"1","title":"ROW2"},{"id":"2","title":"ROW3"},{"id":"3","title":"ROW4"}]}]}}}`,
SendPrep: setSendURL},
Expand All @@ -624,7 +624,7 @@ var SendTestCasesWAC = []ChannelSendTestCase{
SendPrep: setSendURL},
{Label: "Interactive List Message Send with attachment",
Text: "Interactive List Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"ROW1", "ROW2", "ROW3", "ROW4"},
Status: "W", ExternalID: "157b5e14568e8",
Status: "W", ExternalID: "157b5e14568e8", TextLanguage: "en-US",
Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
Responses: map[MockedRequest]MockedResponse{
MockedRequest{
Expand Down
7 changes: 4 additions & 3 deletions handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ type ChannelSendTestCase struct {

ContactURNs map[string]bool

SendPrep SendPrepFunc
NewURN string
SendPrep SendPrepFunc
NewURN string
TextLanguage string
}

// Sp is a utility method to get the pointer to the passed in string
Expand Down Expand Up @@ -225,7 +226,7 @@ func RunChannelSendTestCases(t *testing.T, channel courier.Channel, handler cour
t.Run(testCase.Label, func(t *testing.T) {
require := require.New(t)

msg := mb.NewOutgoingMsg(channel, courier.NewMsgID(10), urns.URN(testCase.URN), testCase.Text, testCase.HighPriority, testCase.QuickReplies, testCase.Topic, testCase.ResponseToID, testCase.ResponseToExternalID)
msg := mb.NewOutgoingMsg(channel, courier.NewMsgID(10), urns.URN(testCase.URN), testCase.Text, testCase.HighPriority, testCase.QuickReplies, testCase.Topic, testCase.ResponseToID, testCase.ResponseToExternalID, testCase.TextLanguage)

for _, a := range testCase.Attachments {
msg.WithAttachment(a)
Expand Down
36 changes: 35 additions & 1 deletion handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,11 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
} else {
payload.Interactive.Type = "list"
payload.Interactive.Body.Text = part
payload.Interactive.Action.Button = "Menu"
if msg.TextLanguage() != "" {
payload.Interactive.Action.Button = languageMenuMap[msg.TextLanguage()]
} else {
payload.Interactive.Action.Button = "Menu"
}
section := mtSection{
Rows: make([]mtSectionRow, len(qrs)),
}
Expand Down Expand Up @@ -1291,3 +1295,33 @@ var languageMap = map[string]string{
"vie": "vi", // Vietnamese
"zul": "zu", // Zulu
}

// iso language code mapping to respective "Menu" word translation
var languageMenuMap = map[string]string{
"da-DK": "Menu",
"de-DE": "Speisekarte",
"en-AU": "Menu",
"en-CA": "Menu",
"en-GB": "Menu",
"en-IN": "Menu",
"en-US": "Menu",
"ca-ES": "Menú",
"es-ES": "Menú",
"es-MX": "Menú",
"fi-FI": "Valikko",
"fr-CA": "Menu",
"fr-FR": "Menu",
"it-IT": "Menù",
"ja-JP": "メニュー",
"ko-KR": "메뉴",
"nb-NO": "Meny",
"nl-NL": "Menu",
"pl-PL": "Menu",
"pt-BR": "Menu",
"ru-RU": "Меню",
"sv-SE": "Meny",
"zh-CN": "菜单",
"zh-HK": "菜單",
"zh-TW": "菜單",
"ar-JO": "قائمة الطعام",
}
2 changes: 1 addition & 1 deletion handlers/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ var defaultSendTestCases = []ChannelSendTestCase{
SendPrep: setSendURL},
{Label: "Interactive List Message Send",
Text: "Interactive List Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"ROW1", "ROW2", "ROW3", "ROW4"},
Status: "W", ExternalID: "157b5e14568e8",
Status: "W", ExternalID: "157b5e14568e8", TextLanguage: "pt-BR",
ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201,
RequestBody: `{"to":"250788123123","type":"interactive","interactive":{"type":"list","body":{"text":"Interactive List Msg"},"action":{"button":"Menu","sections":[{"rows":[{"id":"0","title":"ROW1"},{"id":"1","title":"ROW2"},{"id":"2","title":"ROW3"},{"id":"3","title":"ROW4"}]}]}}}`,
SendPrep: setSendURL},
Expand Down
2 changes: 2 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@ type Msg interface {

EventID() int64
SessionStatus() string

TextLanguage() string
}
6 changes: 4 additions & 2 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ func (mb *MockBackend) NewIncomingMsg(channel Channel, urn urns.URN, text string
}

// NewOutgoingMsg creates a new outgoing message from the given params
func (mb *MockBackend) NewOutgoingMsg(channel Channel, id MsgID, urn urns.URN, text string, highPriority bool, quickReplies []string, topic string, responseToID int64, responseToExternalID string) Msg {
func (mb *MockBackend) NewOutgoingMsg(channel Channel, id MsgID, urn urns.URN, text string, highPriority bool, quickReplies []string, topic string, responseToID int64, responseToExternalID string, textLanguage string) Msg {
msgResponseToID := NilMsgID
if responseToID != 0 {
msgResponseToID = NewMsgID(responseToID)
}

return &mockMsg{channel: channel, id: id, urn: urn, text: text, highPriority: highPriority, quickReplies: quickReplies, topic: topic, responseToID: msgResponseToID, responseToExternalID: responseToExternalID}
return &mockMsg{channel: channel, id: id, urn: urn, text: text, highPriority: highPriority, quickReplies: quickReplies, topic: topic, responseToID: msgResponseToID, responseToExternalID: responseToExternalID, textLanguage: textLanguage}
}

// PushOutgoingMsg is a test method to add a message to our queue of messages to send
Expand Down Expand Up @@ -584,6 +584,7 @@ type mockMsg struct {
metadata json.RawMessage
alreadyWritten bool
isResend bool
textLanguage string

receivedOn *time.Time
sentOn *time.Time
Expand All @@ -609,6 +610,7 @@ func (m *mockMsg) ResponseToID() MsgID { return m.responseToID }
func (m *mockMsg) ResponseToExternalID() string { return m.responseToExternalID }
func (m *mockMsg) Metadata() json.RawMessage { return m.metadata }
func (m *mockMsg) IsResend() bool { return m.isResend }
func (m *mockMsg) TextLanguage() string { return m.textLanguage }

func (m *mockMsg) ReceivedOn() *time.Time { return m.receivedOn }
func (m *mockMsg) SentOn() *time.Time { return m.sentOn }
Expand Down

0 comments on commit 41d43b6

Please sign in to comment.