Skip to content

Commit

Permalink
Merge pull request #1263 from nyaruka/tweak_component_types
Browse files Browse the repository at this point in the history
Support template component types like `header/*` and `body/*`
  • Loading branch information
rowanseymour authored Jun 10, 2024
2 parents 2598778 + 5bfeda9 commit cbb0c8d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions assets/static/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ func (t *TemplateTranslation) Locale() i18n.Locale { return t.Locale_ }
func (t *TemplateTranslation) Channel() *assets.ChannelReference { return t.Channel_ }

type TemplateComponent struct {
Type_ string `json:"type"`
Name_ string `json:"name"`
Type_ string `json:"type"`
Content_ string `json:"content"`
Display_ string `json:"display"`
Variables_ map[string]int `json:"variables"`
}

// Type returns the type for this template component
func (t *TemplateComponent) Type() string { return t.Type_ }

// Name returns the name for this template component
func (t *TemplateComponent) Name() string { return t.Name_ }

// Type returns the type for this template component
func (t *TemplateComponent) Type() string { return t.Type_ }

// Content returns the content for this template component
func (t *TemplateComponent) Content() string { return t.Content_ }

Expand All @@ -107,7 +107,7 @@ func (t *TemplateComponent) Display() string { return t.Display_ }
func (t *TemplateComponent) Variables() map[string]int { return t.Variables_ }

// NewTemplateComponent creates a new template param
func NewTemplateComponent(type_, name, content, display string, variables map[string]int) *TemplateComponent {
func NewTemplateComponent(name, type_, content, display string, variables map[string]int) *TemplateComponent {
return &TemplateComponent{Type_: type_, Name_: name, Content_: content, Display_: display, Variables_: variables}
}

Expand Down
6 changes: 3 additions & 3 deletions assets/static/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func TestTemplate(t *testing.T) {
assert.Equal(t, "text", v1.Type())
assert.Equal(t, "text", v2.Type())

c1 := static.NewTemplateComponent("body", "body", "Hello {{1}}", "", map[string]int{"1": 0})
c2 := static.NewTemplateComponent("button/url", "button.0", "http://google.com?q={{1}}", "Go", map[string]int{"1": 1})
c1 := static.NewTemplateComponent("body", "body/text", "Hello {{1}}", "", map[string]int{"1": 0})
c2 := static.NewTemplateComponent("button.0", "button/url", "http://google.com?q={{1}}", "Go", map[string]int{"1": 1})

assert.Equal(t, "body", c1.Type())
assert.Equal(t, "body", c1.Name())
assert.Equal(t, "body/text", c1.Type())
assert.Equal(t, "Hello {{1}}", c1.Content())
assert.Equal(t, "", c1.Display())
assert.Equal(t, map[string]int{"1": 0}, c1.Variables())
Expand Down
2 changes: 1 addition & 1 deletion assets/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ type TemplateVariable interface {
}

type TemplateComponent interface {
Type() string
Name() string
Type() string
Content() string
Display() string
Variables() map[string]int
Expand Down
2 changes: 1 addition & 1 deletion flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef *
}

if previewContent != "" {
if comp.Type() == "header" || comp.Type() == "body" || comp.Type() == "footer" {
if comp.Type() == "header" || comp.Type() == "header/text" || comp.Type() == "body" || comp.Type() == "body/text" || comp.Type() == "footer" || comp.Type() == "footer/text" {
previewText = append(previewText, previewContent)
} else if strings.HasPrefix(comp.Type(), "button/") {
previewQRs = append(previewQRs, stringsx.TruncateEllipsis(previewContent, maxQuickReplyLength))
Expand Down
18 changes: 9 additions & 9 deletions flows/actions/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -339,23 +339,23 @@
"locale": "eng-US",
"components": [
{
"type": "header",
"name": "header",
"type": "header/text",
"content": "Update",
"variables": {}
},
{
"type": "body",
"name": "body",
"type": "body/text",
"content": "Hey {{1}}, your gender is saved as {{2}}.",
"variables": {
"1": 0,
"2": 1
}
},
{
"type": "button/quick_reply",
"name": "button.1",
"type": "button/quick_reply",
"content": "{{1}}",
"variables": {
"1": 2
Expand All @@ -382,31 +382,31 @@
"locale": "spa",
"components": [
{
"type": "header",
"name": "header",
"type": "header/text",
"content": "Actualizar",
"variables": {}
},
{
"type": "body",
"name": "body",
"type": "body/text",
"content": "Hola, {{1}}, tu género está guardado como {{2}}.",
"variables": {
"1": 0,
"2": 1
}
},
{
"type": "button/quick_reply",
"name": "button.0",
"type": "button/quick_reply",
"content": "{{1}}",
"variables": {
"1": 2
}
},
{
"type": "button/quick_reply",
"name": "button.1",
"type": "button/quick_reply",
"content": "No",
"variables": {}
}
Expand Down Expand Up @@ -437,16 +437,16 @@
"locale": "eng-US",
"components": [
{
"type": "header",
"name": "header",
"type": "header/image",
"content": "",
"variables": {
"1": 0
}
},
{
"type": "body",
"name": "body",
"type": "body/text",
"content": "{{1}}",
"variables": {
"1": 1
Expand Down
6 changes: 3 additions & 3 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,8 @@
"namespace": "",
"components": [
{
"type": "body",
"name": "body",
"type": "body/text",
"variables": {
"1": 0,
"2": 1
Expand Down Expand Up @@ -955,15 +955,15 @@
"namespace": "",
"components": [
{
"type": "header",
"name": "header",
"type": "header/image",
"variables": {
"1": 0
}
},
{
"type": "body",
"name": "body",
"type": "body/text",
"variables": {
"1": 1
}
Expand Down

0 comments on commit cbb0c8d

Please sign in to comment.