diff --git a/core/handlers/msg_created.go b/core/handlers/msg_created.go index 6519ad4f3..771d888eb 100644 --- a/core/handlers/msg_created.go +++ b/core/handlers/msg_created.go @@ -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" @@ -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) + } } } @@ -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, +} diff --git a/core/models/msgs.go b/core/models/msgs.go index 002baa307..6351d69bc 100644 --- a/core/models/msgs.go +++ b/core/models/msgs.go @@ -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() @@ -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) }