-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_generic.go
91 lines (79 loc) · 2.08 KB
/
component_generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package got
import "github.com/ramin0/messenger"
// ComponentGeneric type
type ComponentGeneric struct {
ComponentBase
Dynamic bool
Prefix string
Actions []*ComponentGenericAction
Elements []*SubComponentElement
}
// ComponentGenericAction type
type ComponentGenericAction struct {
Name string
BlockID string
}
// Execute func
func (c *ComponentGeneric) Execute(ctx *BotContext) error {
elements := c.Elements
if c.Dynamic {
elements = c.buildElements(ctx)
}
elms := []*messenger.ElmElement{}
for _, e := range elements {
elms = append(elms, e.Execute(ctx))
}
return c.client().SendGenericTemplate(ctx.userID,
elms)
}
func (c *ComponentGeneric) buildElements(ctx *BotContext) []*SubComponentElement {
var elements []*SubComponentElement
elementsData := ctx.Interpolations()[c.Prefix].([]map[string]interface{})
for _, data := range elementsData {
e := &SubComponentElement{}
elements = append(elements, e)
if title, ok := data["title"].(string); ok {
e.Title = title
}
if subtitle, ok := data["subtitle"].(string); ok {
e.Subtitle = subtitle
}
if imageURL, ok := data["image_url"].(string); ok {
e.ImageURL = imageURL
}
if buttons, ok := data["buttons"].([]map[string]interface{}); ok {
for _, b := range buttons {
button := &SubComponentButton{}
e.Buttons = append(e.Buttons, button)
if t, ok := b["type"].(string); ok {
button.Type = t
switch t {
case SubComponentButtonTypeWebURL:
button.URL = b["url"].(string)
case SubComponentButtonTypePostback:
if actionData, ok := b["action"].(map[string]interface{}); ok {
var action *ComponentGenericAction
for _, a := range c.Actions {
if actionData["name"] == a.Name {
action = a
break
}
}
if action == nil {
continue
}
button.Payload = &BotPayload{
BlockID: action.BlockID,
Content: actionData["params"].(map[string]interface{}),
}
}
}
}
if title, ok := b["title"].(string); ok {
button.Title = title
}
}
}
}
return elements
}