-
Notifications
You must be signed in to change notification settings - Fork 3
/
interfaces.go
167 lines (144 loc) · 5.21 KB
/
interfaces.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
package tbcomctl
import (
"context"
"errors"
tb "gopkg.in/telebot.v3"
)
// Controller is the interface that some common controls implement. Controllers
// can be chained together. Controller is everything that can interact with user,
// i.e. a message that waits for the user input, or a picklist that presents the
// user with a set of buttons. Caller may want to create a custom controller to
// include in the form.
type Controller interface {
// Handler is the controller's message handler.
Handler(c tb.Context) error
// Name returns the name of the control assigned to it on creation. When
// Controller is a part of a form, one can call Form.Controller(name) method
// to get the controller.
Name() string
// SetNext sets the next handler, when control is part of a form.
SetNext(Controller)
// SetPrev sets the previous handler.
SetPrev(Controller)
// SetForm assigns the form to the controller, this will allow controller to
// address other controls in a form by name.
SetForm(*Form)
// Form returns the form associated with the controller.
Form() *Form
// Value returns the value stored in the controller for the recipient.
Value(recipient string) (string, bool)
// OutgoingID should return the value of the outgoing message ID for the
// user and true if the message is present or false otherwise.
OutgoingID(recipient string) (int, bool)
}
// HandleContextFunc is the callback function that is being called within
// controller callbacks. It should return the errors specific to controller
// requirements for the input to be processed.
type HandleContextFunc func(ctx context.Context, c tb.Context) error
// Texter is the interface that contains only the method to return the Message
// Text.
type Texter interface {
// Text should return the message that is presented to the user and an error.
Text(ctx context.Context, c tb.Context) (string, error)
}
// NewTexter wraps the msg returning a Texter.
func NewTexter(msg string) Texter {
return NewStaticTVC(msg, nil, nil)
}
// Valuer is the interface
type Valuer interface {
// Values should return a list of strings to present as choices to the user and an error.
// TODO describe supported error return values.
Values(ctx context.Context, c tb.Context) ([]string, error)
}
// Callbacker defines the interface for the callback function.
type Callbacker interface {
// Callback should process the handler's callback.
// TODO: describe supported error return values.
Callback(ctx context.Context, c tb.Context) error
}
// ErrorHandler is an optional interface for some controls, that the caller might
// implement so that errors occurred while handling the Callback could be
// handled by the caller as well.
type ErrorHandler interface {
// OnError should process the error.
OnError(ctx context.Context, c tb.Context, err error)
}
// TextValuer combines Texter and Valuer interfaces.
type TextValuer interface {
Texter
Valuer
}
// TextCallbacker combines Texter and Callbacker interfaces.
type TextCallbacker interface {
Texter
Callbacker
}
// TextValueCallbacker combines Texter, Valuer and Callbacker interfaces.
type TextValueCallbacker interface {
Texter
Valuer
Callbacker
}
// TVC is an implementation of TextValuerCallbacker.
type TVC struct {
TextFn func(context.Context, tb.Context) (string, error)
ValuesFn func(context.Context, tb.Context) ([]string, error)
CBfn HandleContextFunc
ErrorFn func(ctx context.Context, c tb.Context, err error)
}
// NewStaticTVC is a convenience constructor for TVC (TextValueCallbacker) with
// static text and values.
func NewStaticTVC(text string, values []string, callbackFn HandleContextFunc) *TVC {
return &TVC{
TextFn: func(_ context.Context, _ tb.Context) (string, error) { return text, nil },
ValuesFn: func(_ context.Context, _ tb.Context) ([]string, error) { return values, nil },
CBfn: callbackFn,
}
}
// Text callse the TextFn with contexts.
// ctx is legacy from the times when there was not telebot.Context.
func (t *TVC) Text(ctx context.Context, c tb.Context) (string, error) {
if t.TextFn == nil {
return "", errors.New("text function is not defined")
}
return t.TextFn(ctx, c)
}
func (t *TVC) Values(ctx context.Context, c tb.Context) ([]string, error) {
if t.ValuesFn == nil {
return nil, errors.New("values function is not defined")
}
return t.ValuesFn(ctx, c)
}
func (t *TVC) Callback(ctx context.Context, c tb.Context) error {
if t.CBfn == nil {
return errors.New("callback function is not defined")
}
return t.CBfn(ctx, c)
}
func (t *TVC) OnError(ctx context.Context, c tb.Context, err error) error {
if t.ErrorFn == nil {
return nil
}
return t.OnError(ctx, c, err)
}
// WithTextFn sets the text function to fn.
func (t *TVC) WithTextFn(fn func(context.Context, tb.Context) (string, error)) *TVC {
t.TextFn = fn
return t
}
// WithValuesFn sets the value function to fn.
func (t *TVC) WithValuesFn(fn func(context.Context, tb.Context) ([]string, error)) *TVC {
t.ValuesFn = fn
return t
}
// WithCallbackFn sets the callback function to fn.
func (t *TVC) WithCallbackFn(fn HandleContextFunc) *TVC {
t.CBfn = fn
return t
}
// WithErrorFn sets the error handling function to fn.
func (t *TVC) WithErrorFn(fn func(ctx context.Context, c tb.Context, err error)) *TVC {
t.ErrorFn = fn
return t
}