/
dialogbuilder.go
176 lines (155 loc) · 3.83 KB
/
dialogbuilder.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
package botkit
import (
"fmt"
"io"
"strconv"
)
type DialogBuilder struct {
steps []dialogStep
finalizer dialogFinalizer
}
type dialogStep struct {
id int
query *Query
handler dialogStepHandler
}
type dialogStepHandler func(response any) error
type dialogFinalizer func(ctx *Context, responses []any)
func NewDialogBuilder() *DialogBuilder {
return new(DialogBuilder)
}
func (db *DialogBuilder) AddTextInputQuery(text string, validator func(resp string) error) *DialogBuilder {
h := func(resp any) error {
return validator(resp.(string))
}
if validator == nil {
h = dummyDialogStepHandler
}
db.addStep(TextInputQueryKind, text, h)
return db
}
func (db *DialogBuilder) AddSingleChoiceQuery(text string, validator func(choice int) error, choices ...string) *DialogBuilder {
h := func(resp any) error {
return validator(resp.([]int)[0])
}
if validator == nil {
h = dummyDialogStepHandler
}
db.addStep(SingleChoiceQueryKind, text, h).Choices = choices
return db
}
func (db *DialogBuilder) AddMultiChoiceQuery(text string, validator func(choices []int) error, choices ...string) *DialogBuilder {
h := func(resp any) error {
return validator(resp.([]int))
}
if validator == nil {
h = dummyDialogStepHandler
}
db.addStep(MultiChoiceQueryKind, text, h).Choices = choices
return db
}
func (db *DialogBuilder) AddFileInputQuery(text string, validator func(io.Reader) error) *DialogBuilder {
h := func(resp any) error {
reader := resp.(io.ReadCloser)
defer reader.Close()
return validator(reader)
}
if validator == nil {
h = func(resp any) error {
reader := resp.(io.ReadCloser)
reader.Close()
return nil
}
}
db.addStep(FileInputQueryKind, text, h)
return db
}
func (db *DialogBuilder) SetFinalizer(finalizer func(ctx *Context, responses []any)) *DialogBuilder {
db.finalizer = finalizer
return db
}
func (db *DialogBuilder) Build() DialogHandler {
if len(db.steps) == 0 {
return nil
}
return func(ctx *Context, dlg *Dialog) *Query {
q := dlg.LastQuery()
if q == nil {
return db.steps[0].query
}
id, _ := getDialogStepIDFromQueryName(q.Name)
resp := db.steps[id].getUserResponse(ctx, dlg)
handler := db.steps[id].handler
if err := handler(resp); err != nil {
ctx.SendReply("%v", err)
return RetryQuery
}
id++
if id >= len(db.steps) {
if db.finalizer != nil {
resps := make([]any, len(db.steps))
for i := range resps {
resps[i] = db.steps[i].getUserResponse(ctx, dlg)
}
defer closeReaders(resps)
db.finalizer(ctx, resps)
}
return nil
}
return db.steps[id].query
}
}
func (db *DialogBuilder) addStep(kind QueryKind, text string, handler dialogStepHandler) *Query {
id := len(db.steps)
query := &Query{
Name: getQueryNameFromDialogStepID(id),
Kind: kind,
Text: text,
}
step := dialogStep{
id: id,
query: query,
handler: handler,
}
db.steps = append(db.steps, step)
return query
}
func (ds *dialogStep) getUserResponse(ctx *Context, dlg *Dialog) any {
switch ds.query.Kind {
case TextInputQueryKind:
resp, _ := dlg.UserResponse(ds.query.Name)
return resp
case SingleChoiceQueryKind:
resp, _ := dlg.UserChoices(ds.query.Name)
return resp[0]
case MultiChoiceQueryKind:
resp, _ := dlg.UserChoices(ds.query.Name)
return resp
case FileInputQueryKind:
resp, _ := dlg.UserResponse(ds.query.Name)
file, _ := ctx.DownloadFile(resp)
return file
default:
return nil
}
}
func dummyDialogStepHandler(resp any) error {
return nil
}
func getQueryNameFromDialogStepID(id int) string {
return "Q" + strconv.Itoa(id)
}
func getDialogStepIDFromQueryName(queryName string) (int, bool) {
var id int
if _, err := fmt.Sscanf(queryName, "Q%d", &id); err != nil {
return 0, false
}
return id, true
}
func closeReaders(resps []any) {
for _, resp := range resps {
if r, ok := resp.(io.ReadCloser); ok {
r.Close()
}
}
}