-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi_manager.go
More file actions
150 lines (128 loc) · 3.36 KB
/
Copy pathapi_manager.go
File metadata and controls
150 lines (128 loc) · 3.36 KB
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
package ParsBale
import (
"context"
"log"
"regexp"
"strings"
)
type HandlerFunc func(bot *Bot, update Update)
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
type Dispatcher struct {
Bot *Bot
State StateStorage
handlers []handlerEntry
middlewares []MiddlewareFunc
}
type handlerEntry struct {
filter func(Update) bool
handle HandlerFunc
}
func NewDispatcher(bot *Bot) *Dispatcher {
state := NewMemoryState()
bot.State = state
return &Dispatcher{
Bot: bot,
State: state,
handlers: make([]handlerEntry, 0),
middlewares: make([]MiddlewareFunc, 0),
}
}
func (d *Dispatcher) Use(m MiddlewareFunc) {
d.middlewares = append(d.middlewares, m)
}
func (d *Dispatcher) Handle(filter func(Update) bool, handler HandlerFunc) {
h := handler
// Reverse order middleware execution
for i := len(d.middlewares) - 1; i >= 0; i-- {
h = d.middlewares[i](h)
}
d.handlers = append(d.handlers, handlerEntry{filter: filter, handle: h})
}
// --- Routing Helpers ---
func (d *Dispatcher) OnCommand(cmd string, handler HandlerFunc) {
d.Handle(func(u Update) bool {
if u.Message == nil || u.Message.Text == "" {
return false
}
text := u.Message.Text
if !strings.HasPrefix(text, "/") {
return false
}
parts := strings.Fields(text)
command := strings.ToLower(parts[0])
if strings.Contains(command, "@") {
command = strings.Split(command, "@")[0]
}
return command == "/"+strings.ToLower(cmd)
}, handler)
}
func (d *Dispatcher) OnText(regex string, handler HandlerFunc) {
re := regexp.MustCompile(regex)
d.Handle(func(u Update) bool {
if u.Message == nil || u.Message.Text == "" {
return false
}
return re.MatchString(u.Message.Text)
}, handler)
}
func (d *Dispatcher) OnCallback(prefix string, handler HandlerFunc) {
d.Handle(func(u Update) bool {
return u.CallbackQuery != nil && strings.HasPrefix(u.CallbackQuery.Data, prefix)
}, handler)
}
func (d *Dispatcher) OnState(state string, handler HandlerFunc) {
d.Handle(func(u Update) bool {
var userID int64
if u.Message != nil && u.Message.From != nil {
userID = u.Message.From.ID
} else if u.CallbackQuery != nil && u.CallbackQuery.From != nil {
userID = u.CallbackQuery.From.ID
} else {
return false
}
currentState, _ := d.State.Get(userID)
return currentState == state
}, handler)
}
// --- Execution ---
func (d *Dispatcher) StartPolling(ctx context.Context) error {
poller := NewPoller(d.Bot)
updates := make(chan Update, 100)
go func() {
if err := poller.Start(ctx, updates); err != nil {
log.Fatalf("Polling failed: %v", err)
}
}()
log.Println("ParsBale Bot v1.0.0 Started polling...")
for {
select {
case <-ctx.Done():
return nil
case u := <-updates:
go d.processUpdate(u)
}
}
}
func (d *Dispatcher) StartWebhook(ctx context.Context, addr, path string) error {
server := NewWebhookServer(d.Bot, addr)
go func() {
for {
select {
case u := <-server.Updates():
go d.processUpdate(u)
case <-ctx.Done():
return
}
}
}()
log.Printf("ParsBale Bot v1.0.0 Started webhook on %s", addr)
return server.Start(path)
}
func (d *Dispatcher) processUpdate(u Update) {
for _, h := range d.handlers {
if h.filter(u) {
h.handle(d.Bot, u)
return
}
}
}