-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
99 lines (80 loc) · 1.62 KB
/
bot.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
package plant
import (
"fmt"
"golang.org/x/exp/maps"
tele "gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/layout"
)
type Bot struct {
*layout.Layout
*tele.Bot
h map[any][]tele.HandlerFunc
m map[any][]tele.MiddlewareFunc
c map[any][]func()
}
func (p *Plant) composeBot() (*Bot, error) {
lt, err := layout.New(p.Config.Bot.File)
if err != nil {
return nil, fmt.Errorf("composeBot: %w", err)
}
b, err := tele.NewBot(lt.Settings())
if err != nil {
return nil, fmt.Errorf("composeBot: %w", err)
}
return &Bot{
Layout: lt,
Bot: b,
h: make(map[any][]tele.HandlerFunc),
m: make(map[any][]tele.MiddlewareFunc),
c: make(map[any][]func()),
}, nil
}
func (p *Plant) exposeBot(b *Bot) error {
for _, mod := range p.mods {
for _, end := range mod.Expose() {
b.handle(end)
}
}
return nil
}
func (b *Bot) Start() {
b.callOn(Startup)
b.Bot.Start()
}
func (b *Bot) handle(end any) {
b.Handle(end, func(c tele.Context) error {
for _, h := range b.h[end] {
if err := h(c); err != nil {
return err
}
}
return nil
}, b.m[end]...)
}
func (b *Bot) callOn(on On) {
for _, do := range b.c[on] {
do()
}
}
type On = string
const (
Startup On = "plant:startup"
)
type Handler struct {
b *Bot
}
func (h *Handler) On(on On, do func()) {
h.b.c[on] = append(h.b.c[on], do)
}
func (h *Handler) Use(middle ...tele.MiddlewareFunc) {
h.b.Use(middle...)
}
func (h *Handler) Handle(end any, handler tele.HandlerFunc, m ...tele.MiddlewareFunc) {
h.b.h[end] = append(h.b.h[end], handler)
if len(m) > 0 {
h.b.m[end] = append(h.b.m[end], m...)
}
}
func (h *Handler) Expose() []any {
return maps.Keys(h.b.h)
}