Skip to content

Commit

Permalink
Remplace TemplateParam with ComponentsVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 13, 2023
1 parent 3497663 commit e8ad91a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 203 deletions.
56 changes: 25 additions & 31 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,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"`
Params map[string][]flows.TemplateParam `json:"params"`
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.ComponentVariables `json:"params"`
}

// LocalizationUUID gets the UUID which identifies this object for localization
Expand Down Expand Up @@ -142,40 +142,34 @@ func getTemplatingMsg(action *SendMsgAction, run flows.Run, urn urns.URN, channe
}
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.") {
evaluatedParams := make(map[string]flows.ComponentVariables)
for compKey, compVars := range action.Templating.Params {

qrIndex, err := strconv.Atoi(strings.TrimPrefix(compKey, "button."))
if err != nil {
logEvent(events.NewError(err))
}
paramValue = evaluatedQuickReplies[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
}
}
} else {
localizedParamVariables, _ := run.GetTextArray(uuids.UUID(templateParam.UUID()), "value", []string{templateParam.Value()}, nil)
paramValue, err = run.EvaluateTemplate(localizedParamVariables[0])
var evaluatedComponentVariables []string
if strings.HasPrefix(compKey, "button.") {
qrIndex, err := strconv.Atoi(strings.TrimPrefix(compKey, "button."))
if err != nil {
logEvent(events.NewError(err))

Check warning on line 152 in flows/actions/send_msg.go

View check run for this annotation

Codecov / codecov/patch

flows/actions/send_msg.go#L152

Added line #L152 was not covered by tests
}
paramValue := evaluatedQuickReplies[qrIndex]
evaluatedComponentVariables = []string{paramValue}

} else {

localizedComponentVariables, _ := run.GetTextArray(uuids.UUID(compVars.UUID), "variables", compVars.Variables, nil)
evaluatedComponentVariables = make([]string, len(localizedComponentVariables))
for i, variable := range localizedComponentVariables {
sub, err := run.EvaluateTemplate(variable)
if err != nil {
logEvent(events.NewError(err))
}
evaluatedComponentVariables[i] = sub
}
evaluatedParam := flows.NewTemplateParam(templateParam.Type(), templateParam.UUID(), paramValue)
compVariables[i] = evaluatedParam
}
evaluatedParams[compKey] = compVariables
evaluatedParams[compKey] = flows.ComponentVariables{UUID: compVars.UUID, Variables: evaluatedComponentVariables}

}

templating := flows.NewMsgTemplating(action.Templating.Template, evaluatedVariables, templateTranslation.Namespace(), evaluatedParams)
locale := templateTranslation.Locale()

Expand Down
175 changes: 55 additions & 120 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,10 @@
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "@contact.name"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "boy"
}
]
"body": {
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"variables": ["@contact.name", "boy"]
}
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
Expand Down Expand Up @@ -460,18 +452,10 @@
"text": "Hi Ryan Lewis, who's an excellent boy?",
"templating": {
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "Ryan Lewis"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "boy"
}
]
"body": {
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"variables": ["Ryan Lewis", "boy"]
}
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
Expand Down Expand Up @@ -521,18 +505,10 @@
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "@contact.name"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "boy"
}
]
"body": {
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"variables": ["@contact.name", "boy"]
}
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
Expand All @@ -552,11 +528,8 @@
"niño"
]
},
"f383e94d-0598-4fd9-81d7-f0aea397d63b": {
"value": ["niño"]
},
"74d59988-18c0-4919-9cb4-9f9e3847ed50": {
"value": ["@contact.name"]
"variables": ["@contact.name", "niño"]
}
}
},
Expand All @@ -575,18 +548,10 @@
"text": "Hola Ryan Lewis, quien es un niño excelente?",
"templating": {
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "Ryan Lewis"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "niño"
}
]
"body": {
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"variables": ["Ryan Lewis", "niño"]
}
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
Expand Down Expand Up @@ -854,39 +819,22 @@
"templating": {
"uuid": "9c4bf5b5-3aa4-48ec-9bb9-424a9cbc6785",
"params": {
"header": [
{
"type": "image",
"uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1",
"value": "no_used"
}
],
"body": [
{
"type": "text",
"header":{
"uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1",
"variables": ["http://example.com/red.jpg"]
},
"body": {
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "@contact.name"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "boy"
}
],
"button.0": [
{
"type": "text",
"uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1",
"value": "no_used"
}
],
"button.1": [
{
"type": "text",
"uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea",
"value": "no_used"
}
]
"variables": ["@contact.name", "boy"]
},
"button.0": {
"uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1",
"variables": ["no_used"]
},
"button.1": {
"uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea",
"variables": ["no_used"]
}
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
Expand Down Expand Up @@ -918,11 +866,16 @@
"niño"
]
},
"98788f6d-775e-4c6c-a11c-d6169c3551c1": {
"variables": [
"image:http://example.com/rojo.jpg"
]
},
"f383e94d-0598-4fd9-81d7-f0aea397d63b": {
"value": ["niño"]
"variables": ["niño"]
},
"74d59988-18c0-4919-9cb4-9f9e3847ed50": {
"value": ["@contact.name"]
"variables": ["@contact.name", "niño"]
}
}
},
Expand All @@ -948,40 +901,22 @@
],
"templating": {
"params": {
"body": [
{
"type": "text",
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"value": "Ryan Lewis"
},
{
"type": "text",
"uuid": "f383e94d-0598-4fd9-81d7-f0aea397d63b",
"value": "niño"
}

],
"header": [
{
"type": "image",
"uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1",
"value": "http://example.com/rojo.jpg"
}
],
"button.0": [
{
"type": "text",
"uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1",
"value": "Si"
}
],
"button.1": [
{
"type": "text",
"uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea",
"value": "No"
}
]
"body": {
"uuid": "74d59988-18c0-4919-9cb4-9f9e3847ed50",
"variables": ["Ryan Lewis", "niño"]
},
"header": {
"uuid": "98788f6d-775e-4c6c-a11c-d6169c3551c1",
"variables": ["image:http://example.com/rojo.jpg"]
},
"button.0": {
"uuid": "31cd12f7-5a8d-4421-a289-d47cdfb7cff1",
"variables": ["Si"]
},
"button.1": {
"uuid": "67ed8f42-4fce-4e8a-b788-8a7afc9b02ea",
"variables": ["No"]
}
},
"template": {
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
Expand Down
41 changes: 9 additions & 32 deletions flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,40 +168,17 @@ func (m *MsgOut) Locale() i18n.Locale { return m.Locale_ }
// UnsendableReason returns the reason this message can't be sent (if any)
func (m *MsgOut) UnsendableReason() UnsendableReason { return m.UnsendableReason_ }

// TemplateParamUUID is the UUID of a param
type TemplateParamUUID uuids.UUID

// TemplateParam represents a single parameter
type TemplateParam struct {
Type_ string `json:"type"`
UUID_ TemplateParamUUID `json:"uuid"`
Value_ string `json:"value"`
}

// Type returns the type for this parameter
func (t *TemplateParam) Type() string { return t.Type_ }

// UUID returns the UUID for this parameter
func (t *TemplateParam) UUID() TemplateParamUUID { return t.UUID_ }

// Value returns the value for this parameter
func (t *TemplateParam) Value() string { return t.Value_ }

// NewMsgTemplating creates and returns a new msg template
func NewTemplateParam(paramType string, uuid TemplateParamUUID, value string) TemplateParam {
return TemplateParam{
Type_: paramType,
UUID_: uuid,
Value_: value,
}
type ComponentVariables struct {
UUID uuids.UUID `json:"uuid"`
Variables []string `json:"variables"`
}

// MsgTemplating represents any substituted message template that should be applied when sending this message
type MsgTemplating struct {
Template_ *assets.TemplateReference `json:"template"`
Variables_ []string `json:"variables,omitempty"`
Namespace_ string `json:"namespace"`
Params_ map[string][]TemplateParam `json:"params"`
Template_ *assets.TemplateReference `json:"template"`
Variables_ []string `json:"variables,omitempty"`
Namespace_ string `json:"namespace"`
Params_ map[string]ComponentVariables `json:"params"`
}

// Template returns the template this msg template is for
Expand All @@ -214,10 +191,10 @@ func (t MsgTemplating) Variables() []string { return t.Variables_ }
func (t MsgTemplating) Namespace() string { return t.Namespace_ }

// Params returns the params that should be used for the template
func (t MsgTemplating) Params() map[string][]TemplateParam { return t.Params_ }
func (t MsgTemplating) Params() map[string]ComponentVariables { return t.Params_ }

// NewMsgTemplating creates and returns a new msg template
func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string, params map[string][]TemplateParam) *MsgTemplating {
func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string, params map[string]ComponentVariables) *MsgTemplating {
return &MsgTemplating{
Template_: template,
Variables_: variables,
Expand Down
Loading

0 comments on commit e8ad91a

Please sign in to comment.