Simplify config and add user white list
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -15,17 +16,19 @@ import (
|
||||
|
||||
type TelegramController struct {
|
||||
// deps
|
||||
bot *tgbotapi.BotAPI
|
||||
transcribeService *service.TranscribeService
|
||||
jobRepo contract.TranscriptJobRepository
|
||||
logger *slog.Logger
|
||||
// params
|
||||
bot *tgbotapi.BotAPI
|
||||
userWhiteList []string
|
||||
updateTimeout int
|
||||
}
|
||||
|
||||
type TelegramConfig struct {
|
||||
BotToken string
|
||||
UpdateTimeout int
|
||||
UserWhiteList []string
|
||||
}
|
||||
|
||||
func NewTelegramController(
|
||||
@@ -50,6 +53,7 @@ func NewTelegramController(
|
||||
jobRepo: jobRepo,
|
||||
logger: logger,
|
||||
updateTimeout: config.UpdateTimeout,
|
||||
userWhiteList: config.UserWhiteList,
|
||||
}
|
||||
|
||||
return controller, nil
|
||||
@@ -71,6 +75,12 @@ func (c *TelegramController) Start() {
|
||||
author := update.Message.From.String()
|
||||
c.logger.Info("New incoming message", "author", author)
|
||||
|
||||
if !slices.Contains(c.userWhiteList, author) {
|
||||
c.logger.Info("User is not in white list, reject", "author", author)
|
||||
c.handleForbiddenUser(update.Message)
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle commands
|
||||
if update.Message.IsCommand() {
|
||||
// Extract the command from the Message
|
||||
@@ -105,6 +115,13 @@ func (c *TelegramController) handleStartCommand(message *tgbotapi.Message) {
|
||||
c.bot.Send(msg)
|
||||
}
|
||||
|
||||
func (c *TelegramController) handleForbiddenUser(message *tgbotapi.Message) {
|
||||
msg := tgbotapi.NewMessage(message.Chat.ID, "Извини, тебе нельзя пользоваться этим ботом. Обратись к владельцу бота.")
|
||||
msg.ReplyToMessageID = message.MessageID
|
||||
|
||||
c.bot.Send(msg)
|
||||
}
|
||||
|
||||
func (c *TelegramController) handleHelpCommand(message *tgbotapi.Message) {
|
||||
helpText := `Я бот для расшифровки аудиосообщений и аудиофайлов.
|
||||
|
||||
|
Reference in New Issue
Block a user