-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
36 lines (30 loc) · 940 Bytes
/
handler.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
package telebot
type IHandler interface {
// CheckUpdate checks whether the update should handled by this handler.
CheckUpdate(ctx Context) bool
// HandleUpdate processes the update.
HandleUpdate(ctx Context) error
// Name gets the handler name; used to differentiate handlers programmatically. Names should be unique.
Name() string
}
// HandlerFunc represents a handler function, which is
// used to handle actual endpoints.
type HandlerFunc func(Context) error
func (h HandlerFunc) Name() string {
return "HandlerFunc"
}
func (h HandlerFunc) CheckUpdate(ctx Context) bool {
return true
}
func (h HandlerFunc) HandleUpdate(ctx Context) error {
return h(ctx)
}
// CheckHandlerList iterates over a list of handlers until a match is found; at which point it is returned.
func CheckHandlerList(handlers []IHandler, ctx Context) IHandler {
for _, h := range handlers {
if h.CheckUpdate(ctx) {
return h
}
}
return nil
}