Skip to content

Commit

Permalink
Rename BroadcastTranslation to MsgContent and re-use in actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jun 27, 2024
1 parent 1f68a82 commit 2194de6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions flows/actions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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 []i18n.Language, actionText string, actionAttachments []string, actionQuickReplies []string, logEvent flows.EventCallback) (string, []utils.Attachment, []string, i18n.Language) {
func (a *baseAction) evaluateMessage(run flows.Run, languages []i18n.Language, actionText string, actionAttachments []string, actionQuickReplies []string, logEvent flows.EventCallback) (*flows.MsgContent, i18n.Language) {
// localize and evaluate the message text
localizedText, txtLang := run.GetTextArray(uuids.UUID(a.UUID()), "text", []string{actionText}, languages)
evaluatedText, _ := run.EvaluateTemplate(localizedText[0], logEvent)
Expand Down Expand Up @@ -126,7 +126,7 @@ func (a *baseAction) evaluateMessage(run flows.Run, languages []i18n.Language, a
lang = qrsLang
}

return evaluatedText, evaluatedAttachments, evaluatedQuickReplies, lang
return &flows.MsgContent{Text: evaluatedText, Attachments: evaluatedAttachments, QuickReplies: evaluatedQuickReplies}, lang
}

// helper to save a run result and log it as an event
Expand Down
8 changes: 2 additions & 6 deletions flows/actions/send_broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,8 @@ func (a *SendBroadcastAction) Execute(run flows.Run, step flows.Step, logModifie
for _, language := range languages {
languages := []i18n.Language{language, run.Flow().Language()}

evaluatedText, evaluatedAttachments, evaluatedQuickReplies, _ := a.evaluateMessage(run, languages, a.Text, a.Attachments, a.QuickReplies, logEvent)
translations[language] = &flows.BroadcastTranslation{
Text: evaluatedText,
Attachments: evaluatedAttachments,
QuickReplies: evaluatedQuickReplies,
}
content, _ := a.evaluateMessage(run, languages, a.Text, a.Attachments, a.QuickReplies, logEvent)
translations[language] = content
}

// if we don't have any recipients, noop
Expand Down
6 changes: 3 additions & 3 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
unsendableReason = flows.UnsendableReasonContactStatus
}

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

destinations := run.Contact().ResolveDestinations(a.AllURNs)
Expand All @@ -101,7 +101,7 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
}

if msg == nil {
msg = flows.NewMsgOut(urn, channelRef, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, nil, a.Topic, locale, unsendableReason)
msg = flows.NewMsgOut(urn, channelRef, content.Text, content.Attachments, content.QuickReplies, nil, a.Topic, locale, unsendableReason)
}

logEvent(events.NewMsgCreated(msg))
Expand All @@ -110,7 +110,7 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
// 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, locale, flows.UnsendableReasonNoDestination)
msg := flows.NewMsgOut(urns.NilURN, nil, content.Text, content.Attachments, content.QuickReplies, nil, a.Topic, locale, flows.UnsendableReasonNoDestination)
logEvent(events.NewMsgCreated(msg))
}

Expand Down
8 changes: 4 additions & 4 deletions flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,17 @@ func NewMsgTemplating(template *assets.TemplateReference, namespace string, comp
return &MsgTemplating{Template: template, Namespace: namespace, Components: components, Variables: variables}
}

// BroadcastTranslation is the broadcast content in a particular language
type BroadcastTranslation struct {
// MsgContent is message content in a particular language
type MsgContent struct {
Text string `json:"text"`
Attachments []utils.Attachment `json:"attachments,omitempty"`
QuickReplies []string `json:"quick_replies,omitempty"`
}

type BroadcastTranslations map[i18n.Language]*BroadcastTranslation
type BroadcastTranslations map[i18n.Language]*MsgContent

// ForContact is a utility to help callers select the translation for a contact
func (b BroadcastTranslations) ForContact(e envs.Environment, c *Contact, baseLanguage i18n.Language) (*BroadcastTranslation, i18n.Language) {
func (b BroadcastTranslations) ForContact(e envs.Environment, c *Contact, baseLanguage i18n.Language) (*MsgContent, i18n.Language) {
// first try the contact language if it is valid
if c.Language() != i18n.NilLanguage && slices.Contains(e.AllowedLanguages(), c.Language()) {
t := b[c.Language()]
Expand Down
6 changes: 3 additions & 3 deletions flows/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ func TestIVRMsgOut(t *testing.T) {

func TestBroadcastTranslations(t *testing.T) {
bcastTrans := flows.BroadcastTranslations{
"eng": &flows.BroadcastTranslation{Text: "Hello"},
"fra": &flows.BroadcastTranslation{Text: "Bonjour"},
"spa": &flows.BroadcastTranslation{Text: "Hola"},
"eng": &flows.MsgContent{Text: "Hello"},
"fra": &flows.MsgContent{Text: "Bonjour"},
"spa": &flows.MsgContent{Text: "Hola"},
}
baseLanguage := i18n.Language("eng")

Expand Down

0 comments on commit 2194de6

Please sign in to comment.