fix(whitelist): Fix whitelist middleware
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
444d9b1563
commit
1af7f0a506
16
bot/bot.go
16
bot/bot.go
@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"git.dev.m-and-m.ovh/mderasse/tesla/alert"
|
"git.dev.m-and-m.ovh/mderasse/tesla/alert"
|
||||||
"git.dev.m-and-m.ovh/mderasse/tesla/api"
|
"git.dev.m-and-m.ovh/mderasse/tesla/api"
|
||||||
"gopkg.in/telebot.v3/middleware"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
tele "gopkg.in/telebot.v3"
|
tele "gopkg.in/telebot.v3"
|
||||||
@ -79,10 +78,23 @@ func handleAlert(bot *tele.Bot, config *botConfig, alertChan chan api.Availabili
|
|||||||
func initMiddlewares(bot *tele.Bot, config *botConfig) {
|
func initMiddlewares(bot *tele.Bot, config *botConfig) {
|
||||||
log.Infof("Trusting the following chats: %v", config.WhiteListChatIds)
|
log.Infof("Trusting the following chats: %v", config.WhiteListChatIds)
|
||||||
bot.Use(
|
bot.Use(
|
||||||
middleware.Whitelist(config.WhiteListChatIds...),
|
middlewareAllowChat(config.WhiteListChatIds...),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func middlewareAllowChat(chatIds ...int64) tele.MiddlewareFunc {
|
||||||
|
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||||
|
return func(c tele.Context) error {
|
||||||
|
for _, chat := range chatIds {
|
||||||
|
if chat == c.Chat().ID {
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func initCommands(bot *tele.Bot, config *botConfig) {
|
func initCommands(bot *tele.Bot, config *botConfig) {
|
||||||
|
|
||||||
bot.Handle(tele.OnText, func(c tele.Context) error {
|
bot.Handle(tele.OnText, func(c tele.Context) error {
|
||||||
|
@ -85,7 +85,7 @@ func initBotConfig() (*botConfig, error) {
|
|||||||
return nil, fmt.Errorf("impossible to use TELEGRAM_WHITELIST_CHAT_IDS. format expected: int64,int64")
|
return nil, fmt.Errorf("impossible to use TELEGRAM_WHITELIST_CHAT_IDS. format expected: int64,int64")
|
||||||
}
|
}
|
||||||
|
|
||||||
config.WhiteListChatIds = append(config.AlertChatIds, chatId)
|
config.WhiteListChatIds = append(config.WhiteListChatIds, chatId)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &config, nil
|
return &config, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user