-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP support WA template params #1199
Closed
Closed
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
47a9985
WIp support WA template params
norkans7 e9d3824
Add TemplateParam struct and remove unused components field
norkans7 80a02c5
Update tests for params
norkans7 54710ac
Add UUID field templateParam so we can properly support localization …
norkans7 8e80ddc
Adjust templateParam type and struct
norkans7 5e8be07
Adjust templateParam type for static assets
norkans7 55cc248
Extract function to get message from templating
norkans7 5ed7d0d
More tests
norkans7 3f0580a
Use predictable quick replies indexes
norkans7 3497663
More tests
norkans7 b7208db
Replace TemplateParam with ComponentsVariables
norkans7 89da1a5
Make sure old template Variables are passed as body template params
norkans7 4aa514e
Merge pull request #1201 from nyaruka/compVariables
rowanseymour 3af0c28
Merge branch 'main' into WA-template-params
rowanseymour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package actions | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/nyaruka/gocommon/i18n" | ||
"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() { | ||
|
@@ -52,9 +55,10 @@ type SendMsgAction struct { | |
|
||
// Templating represents the templating that should be used if possible | ||
type Templating struct { | ||
UUID uuids.UUID `json:"uuid" validate:"required,uuid4"` | ||
Template *assets.TemplateReference `json:"template" validate:"required"` | ||
Variables []string `json:"variables" engine:"localized,evaluated"` | ||
UUID uuids.UUID `json:"uuid" validate:"required,uuid4"` | ||
Template *assets.TemplateReference `json:"template" validate:"required"` | ||
Variables []string `json:"variables" engine:"localized,evaluated"` | ||
Params map[string][]flows.TemplateParam `json:"params"` | ||
} | ||
|
||
// LocalizationUUID gets the UUID which identifies this object for localization | ||
|
@@ -99,35 +103,19 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow | |
channelRef := assets.NewChannelReference(dest.Channel.UUID(), dest.Channel.Name()) | ||
|
||
// do we have a template defined? | ||
var templating *flows.MsgTemplating | ||
var msg *flows.MsgOut | ||
if template != nil { | ||
// looks for a translation in the contact locale or environment default | ||
locales := []i18n.Locale{ | ||
run.Session().MergedEnvironment().DefaultLocale(), | ||
run.Session().Environment().DefaultLocale(), | ||
locales := []i18n.Locale{run.Session().MergedEnvironment().DefaultLocale(), run.Session().Environment().DefaultLocale()} | ||
templateTranslation := template.FindTranslation(dest.Channel, locales) | ||
if templateTranslation != nil { | ||
msg = getTemplatingMsg(a, run, urn, channelRef, templateTranslation, evaluatedAttachments, evaluatedQuickReplies, unsendableReason, logEvent) | ||
} | ||
} | ||
|
||
translation := template.FindTranslation(dest.Channel, locales) | ||
if translation != nil { | ||
localizedVariables, _ := run.GetTextArray(uuids.UUID(a.Templating.UUID), "variables", a.Templating.Variables, nil) | ||
|
||
// evaluate our variables | ||
evaluatedVariables := make([]string, len(localizedVariables)) | ||
for i, variable := range localizedVariables { | ||
sub, err := run.EvaluateTemplate(variable) | ||
if err != nil { | ||
logEvent(events.NewError(err)) | ||
} | ||
evaluatedVariables[i] = sub | ||
} | ||
|
||
evaluatedText = translation.Substitute(evaluatedVariables) | ||
templating = flows.NewMsgTemplating(a.Templating.Template, evaluatedVariables, translation.Namespace()) | ||
locale = translation.Locale() | ||
} | ||
if msg == nil { | ||
msg = flows.NewMsgOut(urn, channelRef, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, nil, a.Topic, locale, unsendableReason) | ||
} | ||
|
||
msg := flows.NewMsgOut(urn, channelRef, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, templating, a.Topic, locale, unsendableReason) | ||
logEvent(events.NewMsgCreated(msg)) | ||
} | ||
|
||
|
@@ -140,3 +128,53 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow | |
|
||
return nil | ||
} | ||
|
||
func getTemplatingMsg(action *SendMsgAction, run flows.Run, urn urns.URN, channelRef *assets.ChannelReference, templateTranslation *flows.TemplateTranslation, evaluatedAttachments []utils.Attachment, evaluatedQuickReplies []string, unsendableReason flows.UnsendableReason, logEvent flows.EventCallback) *flows.MsgOut { | ||
qrIndex := 0 | ||
|
||
localizedVariables, _ := run.GetTextArray(uuids.UUID(action.Templating.UUID), "variables", action.Templating.Variables, nil) | ||
evaluatedVariables := make([]string, len(localizedVariables)) | ||
for i, variable := range localizedVariables { | ||
sub, err := run.EvaluateTemplate(variable) | ||
if err != nil { | ||
logEvent(events.NewError(err)) | ||
} | ||
evaluatedVariables[i] = sub | ||
} | ||
evaluatedText := templateTranslation.Substitute(evaluatedVariables) | ||
|
||
evaluatedParams := make(map[string][]flows.TemplateParam) | ||
for compKey, compParams := range action.Templating.Params { | ||
compVariables := make([]flows.TemplateParam, len(compParams)) | ||
for i, templateParam := range compParams { | ||
var paramValue string | ||
var err error | ||
if strings.HasPrefix(compKey, "button.") { | ||
paramValue = evaluatedQuickReplies[qrIndex] | ||
qrIndex++ | ||
} else if templateParam.Type() != "text" { | ||
paramValue = "" | ||
for _, att := range evaluatedAttachments { | ||
attType := strings.Split(att.ContentType(), "/")[0] | ||
if templateParam.Type() == attType { | ||
paramValue = att.URL() | ||
break | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use first attachment matching the type we expect from the template params |
||
} | ||
} else { | ||
localizedParamVariables, _ := run.GetTextArray(uuids.UUID(templateParam.UUID()), "value", []string{templateParam.Value()}, nil) | ||
paramValue, err = run.EvaluateTemplate(localizedParamVariables[0]) | ||
if err != nil { | ||
logEvent(events.NewError(err)) | ||
} | ||
} | ||
evaluatedParam := flows.NewTemplateParam(templateParam.Type(), templateParam.UUID(), paramValue) | ||
compVariables[i] = evaluatedParam | ||
} | ||
evaluatedParams[compKey] = compVariables | ||
} | ||
templating := flows.NewMsgTemplating(action.Templating.Template, evaluatedVariables, templateTranslation.Namespace(), evaluatedParams) | ||
locale := templateTranslation.Locale() | ||
|
||
return flows.NewMsgOut(urn, channelRef, evaluatedText, evaluatedAttachments, evaluatedQuickReplies, templating, action.Topic, locale, unsendableReason) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for QRs we iterate through them and take the next available for the parameter button parameters