fix(whitelist): Fix whitelist middleware
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Matthieu 'JP' DERASSE 2023-06-05 19:01:52 +00:00
parent 444d9b1563
commit 1af7f0a506
Signed by: mderasse
GPG Key ID: 55141C777B16A705
2 changed files with 15 additions and 3 deletions

View File

@ -8,7 +8,6 @@ import (
"git.dev.m-and-m.ovh/mderasse/tesla/alert"
"git.dev.m-and-m.ovh/mderasse/tesla/api"
"gopkg.in/telebot.v3/middleware"
log "github.com/sirupsen/logrus"
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) {
log.Infof("Trusting the following chats: %v", config.WhiteListChatIds)
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) {
bot.Handle(tele.OnText, func(c tele.Context) error {

View File

@ -85,7 +85,7 @@ func initBotConfig() (*botConfig, error) {
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