forked from go-telegram/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_update.go
59 lines (48 loc) · 1.08 KB
/
process_update.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
package bot
import (
"context"
"github.com/gokhanaltun/go-telegram-bot/models"
)
func applyMiddlewares(h HandlerFunc, m ...Middleware) HandlerFunc {
if len(m) < 1 {
return h
}
wrapped := h
for i := len(m) - 1; i >= 0; i-- {
wrapped = m[i](wrapped)
}
return wrapped
}
// ProcessUpdate allows you to process update
func (b *Bot) ProcessUpdate(ctx context.Context, upd *models.Update) {
h := b.defaultHandlerFunc
defer func() {
applyMiddlewares(h, b.middlewares...)(ctx, b, upd)
}()
if upd.Message != nil {
h = b.findHandler(HandlerTypeMessageText, upd)
return
}
if upd.CallbackQuery != nil {
h = b.findHandler(HandlerTypeCallbackQueryData, upd)
return
}
}
func (b *Bot) findHandler(handlerType HandlerType, upd *models.Update) HandlerFunc {
b.handlersMx.RLock()
defer b.handlersMx.RUnlock()
for _, h := range b.handlers {
if h.handlerType == handlerType {
if h.match(upd) {
return h.handler
}
}
}
if b.conversationHandler != nil {
hf := b.conversationHandler.getStageFunction(upd)
if hf != nil {
return hf
}
}
return b.defaultHandlerFunc
}