Skip to content

Commit

Permalink
Merge pull request #1144 from nyaruka/single_msg_lang
Browse files Browse the repository at this point in the history
Reduce msg/ivr_created localization blobs to a single language
  • Loading branch information
rowanseymour authored Dec 15, 2022
2 parents 3f2eff6 + 23825d1 commit 2b056d2
Show file tree
Hide file tree
Showing 46 changed files with 552 additions and 1,880 deletions.
15 changes: 13 additions & 2 deletions flows/actions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (a *baseAction) Validate() error { return nil }
func (a *baseAction) LocalizationUUID() uuids.UUID { return uuids.UUID(a.UUID_) }

// helper function for actions that send a message (text + attachments) that must be localized and evalulated
func (a *baseAction) evaluateMessage(run flows.Run, languages []envs.Language, actionText string, actionAttachments []string, actionQuickReplies []string, logEvent flows.EventCallback) (string, []utils.Attachment, []string, map[string]envs.Language) {
func (a *baseAction) evaluateMessage(run flows.Run, languages []envs.Language, actionText string, actionAttachments []string, actionQuickReplies []string, logEvent flows.EventCallback) (string, []utils.Attachment, []string, envs.Language) {
// localize and evaluate the message text
localizedText, txtLang := run.GetTextArray(uuids.UUID(a.UUID()), "text", []string{actionText}, languages)
evaluatedText, err := run.EvaluateTemplate(localizedText[0])
Expand Down Expand Up @@ -122,7 +122,18 @@ func (a *baseAction) evaluateMessage(run flows.Run, languages []envs.Language, a
evaluatedQuickReplies = append(evaluatedQuickReplies, evaluatedQuickReply)
}

return evaluatedText, evaluatedAttachments, evaluatedQuickReplies, map[string]envs.Language{"text": txtLang, "attachments": attLang, "quick_replies": qrsLang}
// although it's possible for the different parts of the message to have different languages, we want to resolve
// a single language based on what the user actually provided for this message
var lang envs.Language
if localizedText[0] != "" {
lang = txtLang
} else if len(translatedAttachments) > 0 {
lang = attLang
} else if len(translatedQuickReplies) > 0 {
lang = qrsLang
}

return evaluatedText, evaluatedAttachments, evaluatedQuickReplies, lang
}

// helper to save a run result and log it as an event
Expand Down
5 changes: 2 additions & 3 deletions flows/actions/play_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"

"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
)
Expand Down Expand Up @@ -62,8 +61,8 @@ func (a *PlayAudioAction) Execute(run flows.Run, step flows.Step, logModifier fl
call := run.Session().Trigger().Call()

// if we have an audio URL, turn it into a message
msg := flows.NewIVRMsgOut(call.URN(), call.Channel(), "", evaluatedAudioURL)
logEvent(events.NewIVRCreated(msg, map[string]envs.Language{"audio_url": urlLang}))
msg := flows.NewIVRMsgOut(call.URN(), call.Channel(), "", evaluatedAudioURL, urlLang)
logEvent(events.NewIVRCreated(msg))

return nil
}
7 changes: 3 additions & 4 deletions flows/actions/say_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"

"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ func NewSayMsg(uuid flows.ActionUUID, text string, audioURL string) *SayMsgActio
// Execute runs this action
func (a *SayMsgAction) Execute(run flows.Run, step flows.Step, logModifier flows.ModifierCallback, logEvent flows.EventCallback) error {
// localize and evaluate the message text
localizedText, textLanguage := run.GetText(uuids.UUID(a.UUID()), "text", a.Text)
localizedText, textLang := run.GetText(uuids.UUID(a.UUID()), "text", a.Text)
evaluatedText, err := run.EvaluateTemplate(localizedText)
if err != nil {
logEvent(events.NewError(err))
Expand All @@ -69,8 +68,8 @@ func (a *SayMsgAction) Execute(run flows.Run, step flows.Step, logModifier flows
// an IVR flow must have been started with a call
call := run.Session().Trigger().Call()

msg := flows.NewIVRMsgOut(call.URN(), call.Channel(), evaluatedText, localizedAudioURL)
logEvent(events.NewIVRCreated(msg, map[string]envs.Language{"text": textLanguage}))
msg := flows.NewIVRMsgOut(call.URN(), call.Channel(), evaluatedText, localizedAudioURL, textLang)
logEvent(events.NewIVRCreated(msg))

return nil
}
10 changes: 5 additions & 5 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
unsendableReason = flows.UnsendableReasonContactStatus
}

evaluatedText, evaluatedAttachments, evaluatedQuickReplies, l10n := a.evaluateMessage(run, nil, a.Text, a.Attachments, a.QuickReplies, logEvent)
evaluatedText, evaluatedAttachments, evaluatedQuickReplies, lang := a.evaluateMessage(run, nil, a.Text, a.Attachments, a.QuickReplies, logEvent)

destinations := run.Contact().ResolveDestinations(a.AllURNs)

Expand Down Expand Up @@ -125,15 +125,15 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
}
}

msg := flows.NewMsgOut(urn, channelRef, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, templating, a.Topic, unsendableReason)
logEvent(events.NewMsgCreated(msg, l10n))
msg := flows.NewMsgOut(urn, channelRef, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, templating, a.Topic, lang, unsendableReason)
logEvent(events.NewMsgCreated(msg))
}

// if we couldn't find a destination, create a msg without a URN or channel and it's up to the caller
// to handle that as they want
if len(destinations) == 0 {
msg := flows.NewMsgOut(urns.NilURN, nil, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, nil, a.Topic, flows.UnsendableReasonNoDestination)
logEvent(events.NewMsgCreated(msg, nil))
msg := flows.NewMsgOut(urns.NilURN, nil, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, nil, a.Topic, lang, flows.UnsendableReasonNoDestination)
logEvent(events.NewMsgCreated(msg))
}

return nil
Expand Down
12 changes: 4 additions & 8 deletions flows/actions/testdata/play_audio.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@
"text": "",
"attachments": [
"audio:http://uploads.temba.io/welcome/5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f.m4a"
]
},
"localization": {
"audio_url": "eng"
],
"language": "eng"
}
}
],
Expand Down Expand Up @@ -136,10 +134,8 @@
"text": "",
"attachments": [
"audio:http://uploads.temba.io/bienvenido/5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f.m4a"
]
},
"localization": {
"audio_url": "spa"
],
"language": "spa"
}
}
],
Expand Down
18 changes: 6 additions & 12 deletions flows/actions/testdata/say_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@
"text": "Hi there Ryan Lewis",
"attachments": [
"audio:http://uploads.temba.io/welcome.m4a"
]
},
"localization": {
"text": "eng"
],
"language": "eng"
}
}
],
Expand Down Expand Up @@ -84,10 +82,8 @@
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"text": "Hi there Ryan Lewis"
},
"localization": {
"text": "eng"
"text": "Hi there Ryan Lewis",
"language": "eng"
}
}
]
Expand Down Expand Up @@ -129,10 +125,8 @@
"text": "Hola Ryan Lewis",
"attachments": [
"audio:http://uploads.temba.io/bienvenido.m4a"
]
},
"localization": {
"text": "spa"
],
"language": "spa"
}
}
],
Expand Down
97 changes: 25 additions & 72 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,8 @@
"quick_replies": [
"Red",
"Blue"
]
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
],
"language": "eng"
}
}
]
Expand Down Expand Up @@ -189,12 +185,8 @@
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"text": "Hi there"
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
"text": "Hi there",
"language": "eng"
}
}
]
Expand Down Expand Up @@ -231,12 +223,8 @@
"text": "Hi there",
"attachments": [
"image/jpeg:http://exacmple.com/test.jpg"
]
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
],
"language": "eng"
}
}
]
Expand All @@ -260,12 +248,8 @@
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"text": "Hi there Ryan Lewis welcome to U-Report, the secret password is Chef"
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
"text": "Hi there Ryan Lewis welcome to U-Report, the secret password is Chef",
"language": "eng"
}
}
],
Expand Down Expand Up @@ -314,12 +298,8 @@
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"text": "Hi there"
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
"text": "Hi there",
"language": "eng"
}
},
{
Expand All @@ -333,12 +313,8 @@
"uuid": "8e21f093-99aa-413b-b55b-758b54308fcb",
"name": "Twitter Channel"
},
"text": "Hi there"
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
"text": "Hi there",
"language": "eng"
}
}
]
Expand All @@ -359,6 +335,7 @@
"msg": {
"uuid": "9688d21d-95aa-4bed-afc7-f31b35731a3d",
"text": "Hi there",
"language": "eng",
"unsendable_reason": "no_destination"
}
}
Expand Down Expand Up @@ -394,12 +371,8 @@
"uuid": "57f1078f-88aa-46f4-a59a-948a5739c03d",
"name": "My Android Phone"
},
"text": "Hi Ryan Lewis, who's a good boy?"
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
"text": "Hi Ryan Lewis, who's a good boy?",
"language": "eng"
}
}
],
Expand Down Expand Up @@ -485,12 +458,8 @@
],
"namespace": ""
},
"topic": "account"
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
"topic": "account",
"language": "eng"
}
}
],
Expand Down Expand Up @@ -571,12 +540,8 @@
"niño"
],
"namespace": ""
}
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
},
"language": "eng"
}
}
],
Expand Down Expand Up @@ -642,12 +607,8 @@
"language": "eng",
"country": "",
"namespace": ""
}
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "eng"
},
"language": "eng"
}
}
],
Expand Down Expand Up @@ -716,12 +677,8 @@
"language": "eng",
"country": "",
"namespace": ""
}
},
"localization": {
"attachments": "eng",
"quick_replies": "eng",
"text": "spa"
},
"language": "spa"
}
}
],
Expand Down Expand Up @@ -795,12 +752,8 @@
"quick_replies": [
"Si",
"No"
]
},
"localization": {
"attachments": "spa",
"quick_replies": "spa",
"text": "spa"
],
"language": "spa"
}
}
],
Expand Down
Loading

0 comments on commit 2b056d2

Please sign in to comment.