Simplify config and add user white list
This commit is contained in:
@@ -1,36 +1,37 @@
|
|||||||
# Server configuration
|
# Server configuration
|
||||||
[server]
|
[server]
|
||||||
port = 8080
|
port = 8080
|
||||||
|
shutdown_timeout = 5
|
||||||
|
force_shutdown_timeout = 20
|
||||||
|
|
||||||
# Database configuration
|
# Database configuration
|
||||||
[database]
|
[database]
|
||||||
path = "data/transcriber.db"
|
path = "data/transcriber.db"
|
||||||
|
|
||||||
# AWS S3 Configuration
|
# Yandex Cloud Configuration
|
||||||
[aws]
|
|
||||||
# Регион AWS (например: us-east-1, eu-west-1)
|
|
||||||
region = "us-east-1"
|
|
||||||
|
|
||||||
# AWS Access Key ID (получить в AWS Console)
|
|
||||||
access_key_id = "your_access_key_id"
|
|
||||||
|
|
||||||
# AWS Secret Access Key (получить в AWS Console)
|
|
||||||
secret_access_key = "your_secret_access_key"
|
|
||||||
|
|
||||||
# Имя S3 bucket для загрузки файлов
|
|
||||||
bucket_name = "your_bucket_name"
|
|
||||||
|
|
||||||
# Кастомный endpoint для S3 (оставить пустым для AWS S3, заполнить для MinIO или других S3-совместимых сервисов)
|
|
||||||
endpoint = ""
|
|
||||||
|
|
||||||
# Yandex Cloud Speech-to-Text Configuration
|
|
||||||
[yandex]
|
[yandex]
|
||||||
# API ключ для доступа к Yandex Cloud (получить в консоли Yandex Cloud)
|
|
||||||
api_key = "your_api_key_here"
|
|
||||||
|
|
||||||
# ID папки в Yandex Cloud (получить в консоли Yandex Cloud)
|
# ID папки в Yandex Cloud (получить в консоли Yandex Cloud)
|
||||||
folder_id = "your_folder_id_here"
|
folder_id = "your_folder_id_here"
|
||||||
|
|
||||||
|
# API ключ для доступа к Yandex SpeechKit (получить в консоли Yandex Cloud)
|
||||||
|
speech_kit_api_key = "your_speech_kit_api_key_here"
|
||||||
|
|
||||||
|
# Object Storage (S3) configuration
|
||||||
|
# Access Key ID для доступа к Object Storage (получить в консоли Yandex Cloud)
|
||||||
|
object_storage_access_key_id = "your_access_key_id"
|
||||||
|
|
||||||
|
# Secret Access Key для доступа к Object Storage (получить в консоли Yandex Cloud)
|
||||||
|
object_storage_secret_access_key = "your_secret_access_key"
|
||||||
|
|
||||||
|
# Имя бакета в Object Storage
|
||||||
|
object_storage_bucket_name = "your_bucket_name"
|
||||||
|
|
||||||
|
# Регион Object Storage
|
||||||
|
object_storage_region = "ru-central1"
|
||||||
|
|
||||||
|
# Endpoint Object Storage
|
||||||
|
object_storage_endpoint = "https://storage.yandexcloud.net/"
|
||||||
|
|
||||||
# Telegram Bot Configuration
|
# Telegram Bot Configuration
|
||||||
[telegram]
|
[telegram]
|
||||||
# Токен Telegram бота (получить у @BotFather в Telegram)
|
# Токен Telegram бота (получить у @BotFather в Telegram)
|
||||||
|
@@ -10,7 +10,6 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Server ServerConfig `toml:"server"`
|
Server ServerConfig `toml:"server"`
|
||||||
Database DatabaseConfig `toml:"database"`
|
Database DatabaseConfig `toml:"database"`
|
||||||
AWS AWSConfig `toml:"aws"`
|
|
||||||
Yandex YandexConfig `toml:"yandex"`
|
Yandex YandexConfig `toml:"yandex"`
|
||||||
Telegram TelegramConfig `toml:"telegram"`
|
Telegram TelegramConfig `toml:"telegram"`
|
||||||
}
|
}
|
||||||
@@ -19,23 +18,21 @@ type ServerConfig struct {
|
|||||||
Port int `toml:"port"`
|
Port int `toml:"port"`
|
||||||
ShutdownTimeout int `toml:"shutdown_timeout"`
|
ShutdownTimeout int `toml:"shutdown_timeout"`
|
||||||
ForceShutdownTimeout int `toml:"force_shutdown_timeout"`
|
ForceShutdownTimeout int `toml:"force_shutdown_timeout"`
|
||||||
|
UsersWhiteList []string `toml:"users_while_list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DatabaseConfig struct {
|
type DatabaseConfig struct {
|
||||||
Path string `toml:"path"`
|
Path string `toml:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AWSConfig struct {
|
|
||||||
Region string `toml:"region"`
|
|
||||||
AccessKey string `toml:"access_key_id"`
|
|
||||||
SecretKey string `toml:"secret_access_key"`
|
|
||||||
BucketName string `toml:"bucket_name"`
|
|
||||||
Endpoint string `toml:"endpoint"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type YandexConfig struct {
|
type YandexConfig struct {
|
||||||
APIKey string `toml:"api_key"`
|
|
||||||
FolderID string `toml:"folder_id"`
|
FolderID string `toml:"folder_id"`
|
||||||
|
SpeechKitAPIKey string `toml:"speech_kit_api_key"`
|
||||||
|
ObjStorageAccessKey string `toml:"object_storage_access_key_id"`
|
||||||
|
ObjStorageSecretKey string `toml:"object_storage_secret_access_key"`
|
||||||
|
ObjStorageBucketName string `toml:"object_storage_bucket_name"`
|
||||||
|
ObjStorageRegion string `toml:"object_storage_region"`
|
||||||
|
ObjStorageEndpoint string `toml:"object_storage_endpoint"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TelegramConfig struct {
|
type TelegramConfig struct {
|
||||||
@@ -54,16 +51,14 @@ func DefaultConfig() *Config {
|
|||||||
Database: DatabaseConfig{
|
Database: DatabaseConfig{
|
||||||
Path: "data/transcriber.db",
|
Path: "data/transcriber.db",
|
||||||
},
|
},
|
||||||
AWS: AWSConfig{
|
|
||||||
Region: "ru-central1",
|
|
||||||
AccessKey: "",
|
|
||||||
SecretKey: "",
|
|
||||||
BucketName: "",
|
|
||||||
Endpoint: "",
|
|
||||||
},
|
|
||||||
Yandex: YandexConfig{
|
Yandex: YandexConfig{
|
||||||
APIKey: "",
|
|
||||||
FolderID: "",
|
FolderID: "",
|
||||||
|
SpeechKitAPIKey: "",
|
||||||
|
ObjStorageAccessKey: "",
|
||||||
|
ObjStorageSecretKey: "",
|
||||||
|
ObjStorageBucketName: "",
|
||||||
|
ObjStorageRegion: "ru-central1",
|
||||||
|
ObjStorageEndpoint: "https://storage.yandexcloud.net/",
|
||||||
},
|
},
|
||||||
Telegram: TelegramConfig{
|
Telegram: TelegramConfig{
|
||||||
BotToken: "",
|
BotToken: "",
|
||||||
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -15,17 +16,19 @@ import (
|
|||||||
|
|
||||||
type TelegramController struct {
|
type TelegramController struct {
|
||||||
// deps
|
// deps
|
||||||
bot *tgbotapi.BotAPI
|
|
||||||
transcribeService *service.TranscribeService
|
transcribeService *service.TranscribeService
|
||||||
jobRepo contract.TranscriptJobRepository
|
jobRepo contract.TranscriptJobRepository
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
// params
|
// params
|
||||||
|
bot *tgbotapi.BotAPI
|
||||||
|
userWhiteList []string
|
||||||
updateTimeout int
|
updateTimeout int
|
||||||
}
|
}
|
||||||
|
|
||||||
type TelegramConfig struct {
|
type TelegramConfig struct {
|
||||||
BotToken string
|
BotToken string
|
||||||
UpdateTimeout int
|
UpdateTimeout int
|
||||||
|
UserWhiteList []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTelegramController(
|
func NewTelegramController(
|
||||||
@@ -50,6 +53,7 @@ func NewTelegramController(
|
|||||||
jobRepo: jobRepo,
|
jobRepo: jobRepo,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
updateTimeout: config.UpdateTimeout,
|
updateTimeout: config.UpdateTimeout,
|
||||||
|
userWhiteList: config.UserWhiteList,
|
||||||
}
|
}
|
||||||
|
|
||||||
return controller, nil
|
return controller, nil
|
||||||
@@ -71,6 +75,12 @@ func (c *TelegramController) Start() {
|
|||||||
author := update.Message.From.String()
|
author := update.Message.From.String()
|
||||||
c.logger.Info("New incoming message", "author", author)
|
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
|
// Handle commands
|
||||||
if update.Message.IsCommand() {
|
if update.Message.IsCommand() {
|
||||||
// Extract the command from the Message
|
// Extract the command from the Message
|
||||||
@@ -105,6 +115,13 @@ func (c *TelegramController) handleStartCommand(message *tgbotapi.Message) {
|
|||||||
c.bot.Send(msg)
|
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) {
|
func (c *TelegramController) handleHelpCommand(message *tgbotapi.Message) {
|
||||||
helpText := `Я бот для расшифровки аудиосообщений и аудиофайлов.
|
helpText := `Я бот для расшифровки аудиосообщений и аудиофайлов.
|
||||||
|
|
||||||
|
13
main.go
13
main.go
@@ -100,12 +100,12 @@ func main() {
|
|||||||
converter := ffmpegconv.NewFfmpegConverter()
|
converter := ffmpegconv.NewFfmpegConverter()
|
||||||
|
|
||||||
recognizer, err := yandex.NewYandexAudioRecognizerService(yandex.YandexAudioRecognizerConfig{
|
recognizer, err := yandex.NewYandexAudioRecognizerService(yandex.YandexAudioRecognizerConfig{
|
||||||
Region: cfg.AWS.Region,
|
Region: cfg.Yandex.ObjStorageRegion,
|
||||||
AccessKey: cfg.AWS.AccessKey,
|
AccessKey: cfg.Yandex.ObjStorageAccessKey,
|
||||||
SecretKey: cfg.AWS.SecretKey,
|
SecretKey: cfg.Yandex.ObjStorageSecretKey,
|
||||||
BucketName: cfg.AWS.BucketName,
|
BucketName: cfg.Yandex.ObjStorageBucketName,
|
||||||
Endpoint: cfg.AWS.Endpoint,
|
Endpoint: cfg.Yandex.ObjStorageEndpoint,
|
||||||
ApiKey: cfg.Yandex.APIKey,
|
ApiKey: cfg.Yandex.SpeechKitAPIKey,
|
||||||
FolderID: cfg.Yandex.FolderID,
|
FolderID: cfg.Yandex.FolderID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -127,6 +127,7 @@ func main() {
|
|||||||
tgConfig := tgcontroller.TelegramConfig{
|
tgConfig := tgcontroller.TelegramConfig{
|
||||||
BotToken: cfg.Telegram.BotToken,
|
BotToken: cfg.Telegram.BotToken,
|
||||||
UpdateTimeout: cfg.Telegram.UpdateTimeout,
|
UpdateTimeout: cfg.Telegram.UpdateTimeout,
|
||||||
|
UserWhiteList: cfg.Server.UsersWhiteList,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Создаем Telegram бот
|
// Создаем Telegram бот
|
||||||
|
Reference in New Issue
Block a user