Skip to content

Commit

Permalink
Merge pull request #101 from Ilhasoft/feat/language-metadata
Browse files Browse the repository at this point in the history
Add support for sending contact language in messages for WAC and WA
  • Loading branch information
Robi9 authored Dec 23, 2022
2 parents 9c87fdb + 70f3bb9 commit d73f99e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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

0 comments on commit d73f99e

Please sign in to comment.