-
Notifications
You must be signed in to change notification settings - Fork 3
/
template.go
141 lines (121 loc) · 3.98 KB
/
template.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package instabot
import "encoding/json"
// Template defines template.
type Template interface {
TemplateType() TemplateType
}
// TemplateType defines available template type.
type TemplateType string
// all template type.
// generic, product template are available on instagram.
const (
TemplateTypeProduct TemplateType = TemplateType("product")
TemplateTypeGeneric TemplateType = TemplateType("generic")
)
// TemplateDefaultAction template default action.
type TemplateDefaultAction struct {
buttonType ButtonType
URL string
}
// NewTemplateDefaultAction returns new TemplateDefaultAction.
func NewTemplateDefaultAction(URL string) *TemplateDefaultAction {
return &TemplateDefaultAction{
buttonType: ButtonTypeURL,
URL: URL,
}
}
// MarshalJSON returns json of the button.
func (d *TemplateDefaultAction) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string `json:"type"`
URL string `json:"url"`
}{
Type: string(d.buttonType),
URL: d.URL,
})
}
// GenericTemplateElement defines generic template element.
// https://developers.facebook.com/docs/messenger-platform/instagram/features/generic-template#elements
type GenericTemplateElement struct {
Title string
Subtitle string
ImageURL string
DefaultAction *TemplateDefaultAction
Buttons []Button
}
// GenericTemplateElementOption defines new GenericTemplateElement
// instantiation optional argument.
type GenericTemplateElementOption func(*GenericTemplateElement)
// WithTemplateSubtitle sets subtitle of a GenericTemplateElement.
func WithTemplateSubtitle(subtitle string) GenericTemplateElementOption {
return func(gte *GenericTemplateElement) {
gte.Subtitle = subtitle
}
}
// WithTemplateImageURL sets image url of a GenericTemplateElement.
func WithTemplateImageURL(imageURL string) GenericTemplateElementOption {
return func(gte *GenericTemplateElement) {
gte.ImageURL = imageURL
}
}
// WithTemplateDefaultAction sets default action of a GenericTemplateElement.
func WithTemplateDefaultAction(URL string) GenericTemplateElementOption {
return func(gte *GenericTemplateElement) {
gte.DefaultAction = &TemplateDefaultAction{
buttonType: ButtonTypeURL,
URL: URL,
}
}
}
// WithTemplateButtons sets buttons of a GenericTemplateElement,
// a maximum of 3 buttons per element is supported.
func WithTemplateButtons(buttons []Button) GenericTemplateElementOption {
return func(gte *GenericTemplateElement) {
gte.Buttons = buttons
}
}
// NewGenericTemplateElement returns new GenericTemplateElement.
func NewGenericTemplateElement(title string, opts ...GenericTemplateElementOption) *GenericTemplateElement {
gte := &GenericTemplateElement{
Title: title,
}
for _, opt := range opts {
opt(gte)
}
return gte
}
// MarshalJSON marshal generic template element to JSON.
func (e *GenericTemplateElement) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
ImageURL string `json:"image_url,omitempty"`
DefaultAction *TemplateDefaultAction `json:"default_action,omitempty"`
Buttons []Button `json:"buttons,omitempty"`
}{
Title: e.Title,
Subtitle: e.Subtitle,
ImageURL: e.ImageURL,
DefaultAction: e.DefaultAction,
Buttons: e.Buttons,
})
}
// ProductTemplateElement defines product template element.
// https://developers.facebook.com/docs/messenger-platform/send-messages/template/product
type ProductTemplateElement struct {
ProductID string
}
// NewProductTemplateElement returns a new product template element.
func NewProductTemplateElement(productID string) *ProductTemplateElement {
return &ProductTemplateElement{
ProductID: productID,
}
}
// MarshalJSON marshal product template element to JSON.
func (e *ProductTemplateElement) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
ProductID string `json:"id"`
}{
ProductID: e.ProductID,
})
}