Skip to content

Commit

Permalink
Move functionality to generate preview content for template message f…
Browse files Browse the repository at this point in the history
…rom send_msg action to TemplateTranslation
  • Loading branch information
rowanseymour committed Jul 3, 2024
1 parent 4348b2e commit 108b741
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
12 changes: 3 additions & 9 deletions flows/actions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ import (
// max number of bytes to be saved to extra on a result
const resultExtraMaxBytes = 10000

// max length of a message attachment (type:url)
const maxAttachmentLength = 2048

// max length of a quick reply
const maxQuickReplyLength = 64

// common category names
const (
CategorySuccess = "Success"
Expand Down Expand Up @@ -96,8 +90,8 @@ func (a *baseAction) evaluateMessage(run flows.Run, languages []i18n.Language, a
logEvent(events.NewErrorf("attachment text evaluated to empty string, skipping"))
continue
}
if len(evaluatedAttachment) > maxAttachmentLength {
logEvent(events.NewErrorf("evaluated attachment is longer than %d limit, skipping", maxAttachmentLength))
if len(evaluatedAttachment) > flows.MaxAttachmentLength {
logEvent(events.NewErrorf("evaluated attachment is longer than %d limit, skipping", flows.MaxAttachmentLength))
continue
}
evaluatedAttachments = append(evaluatedAttachments, utils.Attachment(evaluatedAttachment))
Expand All @@ -112,7 +106,7 @@ func (a *baseAction) evaluateMessage(run flows.Run, languages []i18n.Language, a
logEvent(events.NewErrorf("quick reply text evaluated to empty string, skipping"))
continue
}
evaluatedQuickReplies = append(evaluatedQuickReplies, stringsx.TruncateEllipsis(evaluatedQuickReply, maxQuickReplyLength))
evaluatedQuickReplies = append(evaluatedQuickReplies, stringsx.TruncateEllipsis(evaluatedQuickReply, flows.MaxQuickReplyLength))
}

// although it's possible for the different parts of the message to have different languages, we want to resolve
Expand Down
33 changes: 2 additions & 31 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package actions

import (
"fmt"
"strings"

"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
)

func init() {
Expand Down Expand Up @@ -153,33 +148,9 @@ func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef *
}

// the message we return is an approximate preview of what the channel will send using the template
var previewText []string
var previewAttachments []utils.Attachment
var previewQRs []string

for _, comp := range translation.Components() {
previewContent := comp.Content()
for key, index := range comp.Variables() {
variable := variables[index]

if variable.Type == "text" {
previewContent = strings.ReplaceAll(previewContent, fmt.Sprintf("{{%s}}", key), variable.Value)
} else if variable.Type == "image" || variable.Type == "video" || variable.Type == "document" {
previewAttachments = append(previewAttachments, utils.Attachment(variable.Value))
}
}

if previewContent != "" {
if comp.Type() == "header/text" || comp.Type() == "body/text" || comp.Type() == "footer/text" {
previewText = append(previewText, previewContent)
} else if strings.HasPrefix(comp.Type(), "button/") {
previewQRs = append(previewQRs, stringsx.TruncateEllipsis(previewContent, maxQuickReplyLength))
}
}
}

preview := translation.Preview(variables)
locale := translation.Locale()
templating := flows.NewMsgTemplating(a.Template, translation.Namespace(), components, variables)

return flows.NewMsgOut(urn, channelRef, strings.Join(previewText, "\n\n"), previewAttachments, previewQRs, templating, flows.NilMsgTopic, locale, unsendableReason)
return flows.NewMsgOut(urn, channelRef, preview.Text, preview.Attachments, preview.QuickReplies, templating, flows.NilMsgTopic, locale, unsendableReason)
}
6 changes: 6 additions & 0 deletions flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func init() {
type UnsendableReason string

const (
// max length of a message attachment (type:url)
MaxAttachmentLength = 2048

// max length of a quick reply
MaxQuickReplyLength = 64

NilUnsendableReason UnsendableReason = ""
UnsendableReasonNoDestination UnsendableReason = "no_destination" // no sendable channel+URN pair
UnsendableReasonContactStatus UnsendableReason = "contact_status" // contact is blocked or stopped or archived
Expand Down
35 changes: 35 additions & 0 deletions flows/template.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package flows

import (
"fmt"
"strings"

"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/utils"
)

// Template represents messaging templates used by channels types such as WhatsApp
Expand Down Expand Up @@ -58,6 +63,36 @@ func NewTemplateTranslation(t assets.TemplateTranslation) *TemplateTranslation {
// Asset returns the underlying asset
func (t *TemplateTranslation) Asset() assets.TemplateTranslation { return t.TemplateTranslation }

// Preview returns message content which will act as a preview of a message sent with this template
func (t *TemplateTranslation) Preview(vars []*TemplatingVariable) *MsgContent {
var text []string
var attachments []utils.Attachment
var quickReplies []string

Check warning on line 70 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L67-L70

Added lines #L67 - L70 were not covered by tests

for _, comp := range t.Components() {
content := comp.Content()
for key, index := range comp.Variables() {
variable := vars[index]

Check warning on line 75 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L72-L75

Added lines #L72 - L75 were not covered by tests

if variable.Type == "text" {
content = strings.ReplaceAll(content, fmt.Sprintf("{{%s}}", key), variable.Value)
} else if variable.Type == "image" || variable.Type == "video" || variable.Type == "document" {
attachments = append(attachments, utils.Attachment(variable.Value))

Check warning on line 80 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L77-L80

Added lines #L77 - L80 were not covered by tests
}
}

if content != "" {
if comp.Type() == "header/text" || comp.Type() == "body/text" || comp.Type() == "footer/text" {
text = append(text, content)
} else if strings.HasPrefix(comp.Type(), "button/") {
quickReplies = append(quickReplies, stringsx.TruncateEllipsis(content, MaxQuickReplyLength))

Check warning on line 88 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L84-L88

Added lines #L84 - L88 were not covered by tests
}
}
}

return &MsgContent{Text: strings.Join(text, "\n\n"), Attachments: attachments, QuickReplies: quickReplies}

Check warning on line 93 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L93

Added line #L93 was not covered by tests
}

// TemplateAssets is our type for all the templates in an environment
type TemplateAssets struct {
templates []*Template
Expand Down

0 comments on commit 108b741

Please sign in to comment.