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

Add support for sending contact language in messages for WAC and WA #101

Merged
merged 1 commit into from
Dec 23, 2022
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
43 changes: 43 additions & 0 deletions core/handlers/msg_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package handlers

import (
"context"
"fmt"

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/core/hooks"
Expand Down Expand Up @@ -90,6 +92,18 @@ func handleMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa
channel = oa.ChannelByUUID(event.Msg.Channel().UUID)
if channel == nil {
return errors.Errorf("unable to load channel with uuid: %s", event.Msg.Channel().UUID)
} else {
if fmt.Sprint(channel.Type()) == "WAC" || fmt.Sprint(channel.Type()) == "WA" {
country := envs.DeriveCountryFromTel("+" + event.Msg.URN().Path())
locale := envs.NewLocale(scene.Contact().Language(), country)
languageCode := locale.ToBCP47()

if _, valid := validLanguageCodes[languageCode]; !valid {
languageCode = ""
}

event.Msg.TextLanguage = envs.Language(languageCode)
}
}
}

Expand All @@ -108,3 +122,32 @@ func handleMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa

return nil
}

var validLanguageCodes = map[string]bool{
"da-DK": true,
"de-DE": true,
"en-AU": true,
"en-CA": true,
"en-GB": true,
"en-IN": true,
"en-US": true,
"ca-ES": true,
"es-ES": true,
"es-MX": true,
"fi-FI": true,
"fr-CA": true,
"fr-FR": true,
"it-IT": true,
"ja-JP": true,
"ko-KR": true,
"nb-NO": true,
"nl-NL": true,
"pl-PL": true,
"pt-BR": true,
"ru-RU": true,
"sv-SE": true,
"zh-CN": true,
"zh-HK": true,
"zh-TW": true,
"ar-JO": true,
}
5 changes: 4 additions & 1 deletion core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func newOutgoingMsg(rt *runtime.Runtime, org *Org, channel *Channel, contactID C
}

// populate metadata if we have any
if len(out.QuickReplies()) > 0 || out.Templating() != nil || out.Topic() != flows.NilMsgTopic {
if len(out.QuickReplies()) > 0 || out.Templating() != nil || out.Topic() != flows.NilMsgTopic || out.TextLanguage != "" {
metadata := make(map[string]interface{})
if len(out.QuickReplies()) > 0 {
metadata["quick_replies"] = out.QuickReplies()
Expand All @@ -409,6 +409,9 @@ func newOutgoingMsg(rt *runtime.Runtime, org *Org, channel *Channel, contactID C
if out.Topic() != flows.NilMsgTopic {
metadata["topic"] = string(out.Topic())
}
if out.TextLanguage != "" {
metadata["text_language"] = out.TextLanguage
}
m.Metadata = null.NewMap(metadata)
}

Expand Down