-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionbuilder.go
272 lines (206 loc) · 6 KB
/
actionbuilder.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package telejoon
import (
"sync"
"github.com/aliforever/go-telegram-bot-api/structs"
"github.com/aliforever/go-telegram-bot-api/tools"
)
type (
ActionKind string
)
const (
ActionKindText ActionKind = "TEXT"
ActionKindInlineMenu ActionKind = "INLINE_MENU"
ActionKindState ActionKind = "STATE"
ActionKindRaw ActionKind = "RAW"
)
type Action interface {
Name(update *StateUpdate) string
}
type ActionBuilderKind interface {
build(update *StateUpdate) *ActionBuilder
}
type DeferredActionBuilder func(update *StateUpdate) *ActionBuilder
// Build builds the deferred action builder.
func (d DeferredActionBuilder) build(update *StateUpdate) *ActionBuilder {
return d(update)
}
// NewDeferredActionBuilder creates a new DeferredActionBuilder.
func NewDeferredActionBuilder(
builder func(update *StateUpdate) *ActionBuilder,
) DeferredActionBuilder {
return builder
}
type conditionalButtonFormation struct {
cond func(update *StateUpdate) bool
formation []int
}
type ActionBuilder struct {
locker sync.Mutex
definedConditions map[string]func(update *StateUpdate) bool
definedConditionResults map[string]bool
conditionalButtons []conditionalButtons
buttons []Action
commands []Action
conditionalButtonFormations []conditionalButtonFormation
buttonFormation []int
maxButtonPerRow int
}
// NewStaticActionBuilder creates a new ActionBuilder.
func NewStaticActionBuilder() *ActionBuilder {
return &ActionBuilder{}
}
// SetMaxButtonPerRow sets the maximum number of buttons per row.
func (b *ActionBuilder) SetMaxButtonPerRow(max int) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
b.maxButtonPerRow = max
return b
}
func (b *ActionBuilder) SetButtonFormation(formation ...int) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
b.buttonFormation = formation
return b
}
func (b *ActionBuilder) AddConditionalButtonFormation(
cond func(update *StateUpdate) bool,
formation ...int,
) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
b.conditionalButtonFormations = append(b.conditionalButtonFormations, conditionalButtonFormation{
cond: cond,
formation: formation,
})
return b
}
// AddCustomButton adds a custom action of button type to the ActionBuilder.
func (b *ActionBuilder) AddCustomButton(action Action) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
b.buttons = append(b.buttons, action)
return b
}
// AddCustomCommand adds a custom action of command type to the ActionBuilder.
func (b *ActionBuilder) AddCustomCommand(action Action) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
b.commands = append(b.commands, action)
return b
}
// DefineCondition defines a condition.
func (b *ActionBuilder) DefineCondition(name string, cond func(update *StateUpdate) bool) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
if b.definedConditions == nil {
b.definedConditions = make(map[string]func(update *StateUpdate) bool)
}
b.definedConditions[name] = cond
return b
}
// SetConditionValue sets the condition value.
func (b *ActionBuilder) SetConditionValue(name string, val bool) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
if b.definedConditionResults == nil {
b.definedConditionResults = make(map[string]bool)
}
b.definedConditionResults[name] = val
return b
}
func (b *ActionBuilder) build(update *StateUpdate) *ActionBuilder {
b.locker.Lock()
defer b.locker.Unlock()
if len(b.definedConditions) > 0 {
for name, cond := range b.definedConditions {
b.definedConditionResults[name] = cond(update)
}
}
return b
}
// getButtonByButton returns the action by the button.
func (b *ActionBuilder) getButtonByButton(update *StateUpdate, button string) Action {
if action := b.getConditionalButtonByName(update, button); action != nil {
return action
}
for _, action := range b.buttons {
if action.Name(update) == button {
if opts, ok := action.(baseButtonOptions); ok {
if opts.CanBeShown(update, b.definedConditionResults) {
return action
}
} else {
return action
}
}
}
return nil
}
// buildButtons builds the buttons.
func (b *ActionBuilder) buildButtons(update *StateUpdate, reverseButtonOrderInRows bool) *structs.ReplyKeyboardMarkup {
b.locker.Lock()
defer b.locker.Unlock()
if len(b.buttons) == 0 && len(b.conditionalButtons) == 0 {
return nil
}
var newButtons []string
buttonFormation := b.buttonFormation
if len(b.conditionalButtonFormations) > 0 {
for _, formation := range b.conditionalButtonFormations {
if formation.cond(update) {
buttonFormation = formation.formation
break
}
}
}
if len(b.conditionalButtons) > 0 {
for _, cdbs := range b.conditionalButtons {
if cdbs.canBeShown(update, b.definedConditionResults) {
availableButtons := b.makeButtonsFromActions(update, cdbs.buttons)
if len(availableButtons) > 0 {
newButtons = append(newButtons, availableButtons...)
if len(cdbs.formation) > 0 {
buttonFormation = append(buttonFormation, cdbs.formation...)
}
}
}
}
}
mainButtons := b.makeButtonsFromActions(update, b.buttons)
newButtons = append(newButtons, mainButtons...)
buttonFormation = append(buttonFormation, b.buttonFormation...)
return tools.Keyboards{}.NewReplyKeyboardFromSliceOfStringsWithFormation(
newButtons,
b.maxButtonPerRow,
buttonFormation,
reverseButtonOrderInRows,
)
}
func (b *ActionBuilder) makeButtonsFromActions(
update *StateUpdate,
actions []Action,
) []string {
var newButtons []string
for _, button := range actions {
name := button.Name(update)
shouldBreakAfter := false
if opts, ok := button.(baseButtonOptions); ok {
if !opts.CanBeShown(update, b.definedConditionResults) {
continue
}
if btnOpts := opts.Options(); btnOpts != nil {
if btnOpts.breakBefore {
newButtons = append(newButtons, "")
}
if btnOpts.breakAfter {
shouldBreakAfter = true
}
}
}
newButtons = append(newButtons, name)
if shouldBreakAfter {
newButtons = append(newButtons, "")
}
}
return newButtons
}