Skip to content

Commit

Permalink
Merge pull request #1246 from nyaruka/13.5
Browse files Browse the repository at this point in the history
13.5
  • Loading branch information
rowanseymour authored Apr 25, 2024
2 parents 7650434 + 8e6abe0 commit b56e274
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 172 deletions.
6 changes: 3 additions & 3 deletions flows/actions/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ func testActionType(t *testing.T, assetsJSON json.RawMessage, typeName string) {
// inject the action into a suitable node's actions in that flow
actionsPath := []string{"flows", fmt.Sprintf("[%d]", flowIndex), "nodes", "[0]", "actions"}
actionsJson := []byte(fmt.Sprintf("[%s]", string(tc.Action)))
assetsJSON = test.JSONReplace(assetsJSON, actionsPath, actionsJson)
testAssetsJSON := test.JSONReplace(assetsJSON, actionsPath, actionsJson)

// if we have a localization section, inject that too
if tc.Localization != nil {
localizationPath := []string{"flows", fmt.Sprintf("[%d]", flowIndex), "localization"}
assetsJSON = test.JSONReplace(assetsJSON, localizationPath, tc.Localization)
testAssetsJSON = test.JSONReplace(testAssetsJSON, localizationPath, tc.Localization)
}

// create session assets
sa, err := test.CreateSessionAssets(assetsJSON, "")
sa, err := test.CreateSessionAssets(testAssetsJSON, "")
require.NoError(t, err, "unable to create session assets in %s", testName)

// now try to read the flow, and if we expect a read error, check that
Expand Down
66 changes: 15 additions & 51 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ const TypeSendMsg string = "send_msg"
// "text": "Hi @contact.name, are you ready to complete today's survey?",
// "attachments": [],
// "all_urns": false,
// "templating": {
// "template": {
// "uuid": "3ce100b7-a734-4b4e-891b-350b1279ade2",
// "name": "revive_issue"
// },
// "components": [
// {
// "uuid": "2131d674-9afb-41e8-bcb8-5910c2faec2f",
// "name": "body",
// "params": ["@contact.name"]
// }
// ]
// "template": {
// "uuid": "3ce100b7-a734-4b4e-891b-350b1279ade2",
// "name": "revive_issue"
// },
// "template_variables": ["@contact.name"],
// "topic": "event"
// }
//
Expand All @@ -54,24 +46,10 @@ type SendMsgAction struct {
universalAction
createMsgAction

AllURNs bool `json:"all_urns,omitempty"`
Templating *Templating `json:"templating,omitempty" validate:"omitempty"`
Topic flows.MsgTopic `json:"topic,omitempty" validate:"omitempty,msg_topic"`
}

type TemplatingComponent struct {
UUID uuids.UUID `json:"uuid" validate:"required,uuid4"`
Name string `json:"name" validate:"required"`
Params []string `json:"params" engine:"localized,evaluated"`
}

// LocalizationUUID gets the UUID which identifies this object for localization
func (c *TemplatingComponent) LocalizationUUID() uuids.UUID { return c.UUID }

// Templating represents the templating that should be used if possible
type Templating struct {
Template *assets.TemplateReference `json:"template" validate:"required"`
Components []*TemplatingComponent `json:"components,omitempty"`
AllURNs bool `json:"all_urns,omitempty"`
Template *assets.TemplateReference `json:"template,omitempty"`
TemplateVariables []string `json:"template_variables,omitempty" engine:"localized,evaluated"`
Topic flows.MsgTopic `json:"topic,omitempty" validate:"omitempty,msg_topic"`
}

// NewSendMsg creates a new send msg action
Expand Down Expand Up @@ -103,8 +81,8 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
sa := run.Session().Assets()

var template *flows.Template
if a.Templating != nil {
template = sa.Templates().Get(a.Templating.Template.UUID)
if a.Template != nil {
template = sa.Templates().Get(a.Template.UUID)
}

// create a new message for each URN+channel destination
Expand Down Expand Up @@ -141,12 +119,10 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
// for message actions that specify a template, this generates a mesage with templating information and content that can
// be used as a preview
func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef *assets.ChannelReference, translation *flows.TemplateTranslation, unsendableReason flows.UnsendableReason, logEvent flows.EventCallback) *flows.MsgOut {
// start by localizing and transforming the current per-component lists into a single list
variableExpressions := getTemplateVariables(run, a.Templating)

// evaluate the variables
evaluatedVariables := make([]string, len(variableExpressions))
for i, varExp := range variableExpressions {
// localize and evaluate the variables
localizedVariables, _ := run.GetTextArray(uuids.UUID(a.UUID()), "template_variables", a.TemplateVariables, nil)
evaluatedVariables := make([]string, len(localizedVariables))
for i, varExp := range localizedVariables {
v, _ := run.EvaluateTemplate(varExp, logEvent)
evaluatedVariables[i] = v
}
Expand Down Expand Up @@ -194,19 +170,7 @@ func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef *

previewText := strings.Join(previewParts, "\n\n")
locale := translation.Locale()
templating := flows.NewMsgTemplating(a.Templating.Template, translation.Namespace(), components, variables)
templating := flows.NewMsgTemplating(a.Template, translation.Namespace(), components, variables)

return flows.NewMsgOut(urn, channelRef, previewText, nil, previewQRs, templating, flows.NilMsgTopic, locale, unsendableReason)
}

func getTemplateVariables(run flows.Run, t *Templating) []string {
variables := make([]string, 0, 5)

for _, comp := range t.Components {
localized, _ := run.GetTextArray(comp.UUID, "params", comp.Params, nil)

variables = append(variables, localized...)
}

return variables
}
6 changes: 3 additions & 3 deletions flows/actions/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"uuid": "bead76f5-dac4-4c9d-996c-c62b326e8c0a",
"name": "Action Tester",
"spec_version": "13.4.0",
"spec_version": "13.5.0",
"language": "eng",
"type": "messaging",
"revision": 123,
Expand All @@ -23,7 +23,7 @@
{
"uuid": "7a84463d-d209-4d3e-a0ff-79f977cd7bd0",
"name": "Voice Action Tester",
"spec_version": "13.4.0",
"spec_version": "13.5.0",
"language": "eng",
"type": "voice",
"revision": 123,
Expand All @@ -43,7 +43,7 @@
{
"uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
"name": "Collect Age",
"spec_version": "13.4.0",
"spec_version": "13.5.0",
"revision": 123,
"language": "eng",
"type": "messaging",
Expand Down
159 changes: 52 additions & 107 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,14 @@
"type": "send_msg",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"text": "Hi @contact.name, who's a good boy?",
"templating": {
"template": {
"uuid": "b620b463-8d15-427f-b2e3-4f44f9f071ec",
"name": "missing"
},
"components": [
{
"uuid": "aa304c85-c14f-4f94-b97c-ec59c07a3f39",
"name": "body",
"params": [
"@contact.name",
"boy"
]
}
]
}
"template": {
"uuid": "b620b463-8d15-427f-b2e3-4f44f9f071ec",
"name": "missing"
},
"template_variables": [
"@contact.name",
"boy"
]
},
"events": [
{
Expand Down Expand Up @@ -422,22 +414,14 @@
"type": "send_msg",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"text": "Hi Ryan Lewis, who's a good boy?",
"templating": {
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
},
"components": [
{
"uuid": "1067f8e2-82f0-4378-9214-0f019365ddb7",
"name": "body",
"params": [
"@contact.name",
"boy"
]
}
]
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
},
"template_variables": [
"@contact.name",
"boy"
],
"topic": "account"
},
"events": [
Expand Down Expand Up @@ -509,32 +493,24 @@
}
},
{
"description": "Msg with a matching template with localized component params",
"description": "Msg with a matching template with localized variables",
"action": {
"type": "send_msg",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"text": "Hi Ryan Lewis, who's a good boy?",
"templating": {
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
},
"components": [
{
"uuid": "1067f8e2-82f0-4378-9214-0f019365ddb7",
"name": "body",
"params": [
"@contact.name",
"boy"
]
}
]
}
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
},
"template_variables": [
"@contact.name",
"boy"
]
},
"localization": {
"spa": {
"1067f8e2-82f0-4378-9214-0f019365ddb7": {
"params": [
"ad154980-7bf7-4ab8-8728-545fd6378912": {
"template_variables": [
"@contact.name",
"niño"
]
Expand Down Expand Up @@ -612,16 +588,14 @@
}
},
{
"description": "Msg with template but no params or variables",
"description": "Msg with template but no variables",
"action": {
"type": "send_msg",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"text": "Hi there, it's time to get up!",
"templating": {
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"name": "wakeup"
}
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"name": "wakeup"
}
},
"events": [
Expand Down Expand Up @@ -674,11 +648,9 @@
"type": "send_msg",
"uuid": "4f0510e9-7e41-4f83-aaa3-59b6e9a17e70",
"text": "Hi Ryan, wakeup!",
"templating": {
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"name": "wakeup"
}
"template": {
"uuid": "2edc8dfd-aef0-41cf-a900-8a71bdb00900",
"name": "wakeup"
}
},
"localization": {
Expand Down Expand Up @@ -814,7 +786,7 @@
}
},
{
"description": "Use template translation with non body component params",
"description": "Use template translation with non body component params and too many variables",
"action": {
"type": "send_msg",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
Expand All @@ -826,53 +798,25 @@
"Yes",
"No"
],
"templating": {
"template": {
"uuid": "ce00c80e-991a-4c03-b373-3273c23ee042",
"name": "gender_update"
},
"components": [
{
"uuid": "1067f8e2-82f0-4378-9214-0f019365ddb7",
"name": "body",
"params": [
"@contact.name",
"boy"
]
},
{
"uuid": "5fb16175-4aef-4ee3-ae59-53cd55452bd8",
"name": "button.0",
"params": [
"Yeah"
]
},
{
"uuid": "1be525d9-7bbd-478e-8922-38a75ad95c36",
"name": "button.1",
"params": [
"Nope"
]
}
]
}
"template": {
"uuid": "ce00c80e-991a-4c03-b373-3273c23ee042",
"name": "gender_update"
},
"template_variables": [
"@contact.name",
"boy",
"Yeah",
"Nope"
]
},
"localization": {
"spa": {
"128b97ff-a530-4ec8-87dd-04a9a778c3e0": {
"params": [
"http://templates.com/rojo.jpg"
]
},
"1067f8e2-82f0-4378-9214-0f019365ddb7": {
"params": [
"ad154980-7bf7-4ab8-8728-545fd6378912": {
"template_variables": [
"@contact.name",
"niño"
]
},
"5fb16175-4aef-4ee3-ae59-53cd55452bd8": {
"params": [
"Sip"
"niño",
"Sip",
"Nop"
]
}
}
Expand Down Expand Up @@ -943,11 +887,12 @@
"No",
"@contact.name",
"boy",
"Yeah",
"Nope",
"@contact.name",
"niño",
"Yeah",
"Sip",
"Nope"
"Nop"
],
"localizables": [
"Hey Ryan Lewis, your gender is saved as boy.",
Expand Down
2 changes: 1 addition & 1 deletion flows/definition/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

// CurrentSpecVersion is the flow spec version supported by this library
var CurrentSpecVersion = semver.MustParse("13.4.0")
var CurrentSpecVersion = semver.MustParse("13.5.0")

// IsVersionSupported checks the given version is supported
func IsVersionSupported(v *semver.Version) bool {
Expand Down
Loading

0 comments on commit b56e274

Please sign in to comment.