-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
85 lines (66 loc) · 1.93 KB
/
main.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
package main
import (
"github.com/CoreDumped-ETSISI/etsisi-telegram-bot/state"
"github.com/guad/commander"
tb "github.com/go-telegram-bot-api/telegram-bot-api"
log "github.com/sirupsen/logrus"
)
func main() {
config := newConfig()
log.SetLevel(log.AllLevels[config.logLevel])
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyLevel: "level_name",
log.FieldKeyMsg: "message",
},
})
bot, updates := startBot()
config.bot = bot
state.G = &config
cmd := commander.New()
callbacks := commander.New()
me := bot.Self
cmd.Preprocessor = &CustomTelegramPreprocessor{
BotName: me.UserName,
}
route(cmd, config, callbacks)
use(cmd, config)
callbacks.Use(callbackLoggerMiddleware)
for update := range updates {
go handleUpdate(bot, update, cmd, callbacks)
}
}
func handleUpdate(bot *tb.BotAPI, update tb.Update, cmd *commander.CommandGroup, callbacks *commander.CommandGroup) {
ctx := map[string]interface{}{
"update": state.Update{
Update: update,
State: state.G,
},
}
if update.Message != nil && update.Message.Text != "" {
ok, err := cmd.ExecuteWithContext(update.Message.Text, ctx)
// TODO: Maybe send the user a message with the error?
log.WithFields(log.Fields{
"chatid": update.Message.Chat.ID,
"chat": getChatTitle(update.Message),
"sender": getSenderName(update.Message.From),
"text": update.Message.Text,
"error": err,
"found": ok,
}).Debug("Got update")
// General logging is done by the logging middleware.
cmd.TriggerWithContext("text", ctx)
} else if update.CallbackQuery != nil {
cq := update.CallbackQuery
ok, err := callbacks.ExecuteWithContext(cq.Data, ctx)
log.WithFields(log.Fields{
"chatid": cq.Message.Chat.ID,
"chat": getChatTitle(cq.Message),
"sender": getSenderName(cq.Message.From),
"data": cq.Data,
"error": err,
"found": ok,
}).Debug("Got query callback")
}
cmd.TriggerWithContext("update", ctx)
}