-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware.go
140 lines (113 loc) · 3.44 KB
/
middleware.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
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
package main
import (
"fmt"
"github.com/CoreDumped-ETSISI/etsisi-telegram-bot/cmds/stub"
"math/rand"
"time"
"github.com/CoreDumped-ETSISI/etsisi-telegram-bot/cmds/janitor"
"github.com/CoreDumped-ETSISI/etsisi-telegram-bot/state"
tb "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/guad/commander"
log "github.com/sirupsen/logrus"
)
func (cfg config) ratelimitMiddleware(next commander.Handler) commander.Handler {
return func(ctx commander.Context) error {
update := ctx.Arg("update").(state.Update)
key := fmt.Sprintf("TIMEOUT_%v_%v", update.Message.Chat.ID, ctx.Name)
_, err := cfg.redis.Get(key).Result()
if err == nil { // Key was found
sendNag(update.State.Bot(), update.Message.Chat.ID)
return nil
}
err = cfg.redis.Set(key, true, cfg.commandTimeout).Err()
if err != nil {
// If redis is unavailable, don't execute the command to prevent spam.
return err
}
return next(ctx)
}
}
func sendNag(bot *tb.BotAPI, chatID int64) {
frases := []string{
"Deja de hacer spam, prostifruto!",
"El spam a tu casa, campeón.",
"Stop spamming, idiota. ¿Tanta prisa tienes?",
}
msg := tb.NewMessage(chatID, frases[rand.Intn(len(frases))])
bot.Send(msg)
}
func loggerMiddleware(next commander.Handler) commander.Handler {
return func(ctx commander.Context) error {
update := ctx.Arg("update").(state.Update)
pre := time.Now()
// TODO: Recover from panic
err := next(ctx)
elapsed := time.Now().Sub(pre)
if err != nil {
log.
WithError(err).
WithFields(log.Fields{
"command": ctx.Name,
"args": ctx.Args,
"chatid": update.Message.Chat.ID,
"chat": getChatTitle(update.Message),
"sender": getSenderName(update.Message.From),
"text": update.Message.Text,
"elapsed": elapsed,
}).Error("Error when executing command")
} else {
log.WithFields(log.Fields{
"command": ctx.Name,
"args": ctx.Args,
"chatid": update.Message.Chat.ID,
"chat": getChatTitle(update.Message),
"sender": getSenderName(update.Message.From),
"text": update.Message.Text,
"elapsed": elapsed,
}).Info("Successfuly sent command")
}
return err
}
}
func callbackLoggerMiddleware(next commander.Handler) commander.Handler {
return func(ctx commander.Context) error {
update := ctx.Arg("update").(state.Update)
pre := time.Now()
// TODO: Recover from panic
err := next(ctx)
elapsed := time.Now().Sub(pre)
if err != nil {
log.
WithError(err).
WithFields(log.Fields{
"command": ctx.Name,
"args": ctx.Args,
"chatid": update.CallbackQuery.Message.Chat.ID,
"chat": getChatTitle(update.CallbackQuery.Message),
"sender": getSenderName(update.CallbackQuery.Message.From),
"text": update.CallbackQuery.Data,
"elapsed": elapsed,
}).Error("Error when executing command")
}
return err
}
}
func errorResponseMiddleware(next commander.Handler) commander.Handler {
return func(ctx commander.Context) error {
err := next(ctx)
update := ctx.Arg("update").(state.Update)
bot := state.G.Bot()
if err == janitor.ErrNotAnAdmin {
m := tb.NewMessage(update.Message.Chat.ID, "Debes ser un administrador de este grupo!")
m.ReplyToMessageID = update.Message.MessageID
_, _ = bot.Send(m)
}
return err
}
}
func use(cmd *commander.CommandGroup, cfg config) {
cmd.Use(loggerMiddleware)
cmd.Use(cfg.ratelimitMiddleware)
cmd.Use(errorResponseMiddleware)
cmd.Use(stub.DynamicMiddleware())
}