/
commandbot.go
74 lines (66 loc) · 1.59 KB
/
commandbot.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
package tg
import (
"log"
"strings"
"unicode"
)
type CommandHandler func(b *CommandBot, args string, msg *Message)
type AnyCommandHandler func(b *CommandBot, cmd, args string, msg *Message)
// CommandBot only handles commands.
type CommandBot struct {
*Bot
Handlers map[string][]CommandHandler
AnyHandlers []AnyCommandHandler
}
func Split(text string, sep byte) (string, string) {
var i, j int
for i = 0; i < len(text) && text[i] != sep; i++ {
}
for j = i; j < len(text) && text[j] == sep; j++ {
}
return text[0:i], text[j:len(text)]
}
func (b *CommandBot) HandleUpdate(_ *Bot, up *Update) {
if up.Message == nil {
return
}
if up.Message.Text == nil {
return
}
text := *up.Message.Text
if len(text) == 0 || text[0] != '/' {
// Not a command
return
}
cmd, args := Split(text[1:len(text)], ' ')
cmd = strings.TrimRightFunc(cmd, unicode.IsSpace)
cmd, to := Split(cmd, '@')
if to != "" && to != b.Name {
log.Println("addressed not to me, but to", to)
// Not addressed to me
return
}
for _, ach := range b.AnyHandlers {
ach(b, cmd, args, up.Message)
}
chs, ok := b.Handlers[cmd]
if !ok {
log.Printf("unknown command %q", cmd)
return
}
for _, ch := range chs {
ch(b, args, up.Message)
}
}
func (b *CommandBot) OnCommand(cmd string, ch CommandHandler) {
b.Handlers[cmd] = append(b.Handlers[cmd], ch)
}
func (b *CommandBot) OnAnyCommand(ach AnyCommandHandler) {
b.AnyHandlers = append(b.AnyHandlers, ach)
}
func NewCommandBot(token string) *CommandBot {
b := &CommandBot{NewBot(token),
make(map[string][]CommandHandler), nil}
b.OnUpdate(b.HandleUpdate)
return b
}