From ce8ffabba72f7e6bbf6f432aa9db5506e358f904 Mon Sep 17 00:00:00 2001 From: ShukeBta Date: Sat, 30 May 2026 03:04:30 +0800 Subject: [PATCH] fix: ignore non-command telegram group messages --- README.md | 24 +++++++++--------- README_EN.md | 22 ++++++++-------- docker-compose.yml | 2 +- internal/service/telegram_api_test.go | 15 +++++++++++ internal/service/telegram_bot.go | 36 ++++++++++++++++++++++++++- 5 files changed, 74 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 617b3eb..97988d9 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ mkdir -p data cache media downloads ```bash cat > .env <<'EOF' # 固定版本;需要升级时改成新的 MediaStationGo-vX.Y.Z 后执行 docker compose pull && docker compose up -d -MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.25 +MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.26 MEDIASTATION_HTTP_PORT=18080 # 程序数据和缓存建议放在 MediaStationGo 部署目录下,便于备份和迁移。 @@ -307,7 +307,7 @@ vim docker-compose.yml # # 镜像版本: # 默认拉取 latest;如需固定版本,创建 .env 并写入: -# MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.25 +# MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.26 # # 路径映射总览: # /data 程序数据目录。保存 SQLite 数据库、JWT secret、系统配置等,必须持久化。 @@ -516,7 +516,7 @@ docker compose up -d ```bash cat > .env <<'EOF' -MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.25 +MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.26 MEDIASTATION_HTTP_PORT=18080 MEDIASTATION_DATA_DIR=./data MEDIASTATION_CACHE_DIR=./cache @@ -779,26 +779,26 @@ cd MediaStationGo | 平台 | 包名示例 | | --- | --- | -| Linux x86_64 | `MediaStationGo-v0.0.25-linux-amd64.tar.gz` | -| Linux ARM64 | `MediaStationGo-v0.0.25-linux-arm64.tar.gz` | -| Windows x86_64 | `MediaStationGo-v0.0.25-windows-amd64.zip` | -| macOS Intel | `MediaStationGo-v0.0.25-darwin-amd64.tar.gz` | -| macOS Apple Silicon | `MediaStationGo-v0.0.25-darwin-arm64.tar.gz` | +| Linux x86_64 | `MediaStationGo-v0.0.26-linux-amd64.tar.gz` | +| Linux ARM64 | `MediaStationGo-v0.0.26-linux-arm64.tar.gz` | +| Windows x86_64 | `MediaStationGo-v0.0.26-windows-amd64.zip` | +| macOS Intel | `MediaStationGo-v0.0.26-darwin-amd64.tar.gz` | +| macOS Apple Silicon | `MediaStationGo-v0.0.26-darwin-arm64.tar.gz` | 部署步骤: ```bash # Linux 示例 -tar -xzf MediaStationGo-v0.0.25-linux-amd64.tar.gz -cd MediaStationGo-v0.0.25-linux-amd64 +tar -xzf MediaStationGo-v0.0.26-linux-amd64.tar.gz +cd MediaStationGo-v0.0.26-linux-amd64 MEDIASTATION_APP_PORT=18080 ./mediastation-go ``` Windows: ```powershell -Expand-Archive .\MediaStationGo-v0.0.25-windows-amd64.zip -cd .\MediaStationGo-v0.0.25-windows-amd64 +Expand-Archive .\MediaStationGo-v0.0.26-windows-amd64.zip +cd .\MediaStationGo-v0.0.26-windows-amd64 $env:MEDIASTATION_APP_PORT = "18080" .\mediastation-go.exe ``` diff --git a/README_EN.md b/README_EN.md index 8a1862e..4733c98 100644 --- a/README_EN.md +++ b/README_EN.md @@ -235,7 +235,7 @@ mkdir -p data cache media downloads ```bash cat > .env <<'EOF' -MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.25 +MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.26 MEDIASTATION_HTTP_PORT=18080 MEDIASTATION_DATA_DIR=./data MEDIASTATION_CACHE_DIR=./cache @@ -344,7 +344,7 @@ For production, pin a specific release tag instead of using `latest`. Recommende ```bash cat > .env <<'EOF' -MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.25 +MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.26 MEDIASTATION_HTTP_PORT=18080 MEDIASTATION_DATA_DIR=./data MEDIASTATION_CACHE_DIR=./cache @@ -593,25 +593,25 @@ Each release provides multi-platform archives: | Platform | Package example | | --- | --- | -| Linux x86_64 | `MediaStationGo-v0.0.25-linux-amd64.tar.gz` | -| Linux ARM64 | `MediaStationGo-v0.0.25-linux-arm64.tar.gz` | -| Windows x86_64 | `MediaStationGo-v0.0.25-windows-amd64.zip` | -| macOS Intel | `MediaStationGo-v0.0.25-darwin-amd64.tar.gz` | -| macOS Apple Silicon | `MediaStationGo-v0.0.25-darwin-arm64.tar.gz` | +| Linux x86_64 | `MediaStationGo-v0.0.26-linux-amd64.tar.gz` | +| Linux ARM64 | `MediaStationGo-v0.0.26-linux-arm64.tar.gz` | +| Windows x86_64 | `MediaStationGo-v0.0.26-windows-amd64.zip` | +| macOS Intel | `MediaStationGo-v0.0.26-darwin-amd64.tar.gz` | +| macOS Apple Silicon | `MediaStationGo-v0.0.26-darwin-arm64.tar.gz` | Linux example: ```bash -tar -xzf MediaStationGo-v0.0.25-linux-amd64.tar.gz -cd MediaStationGo-v0.0.25-linux-amd64 +tar -xzf MediaStationGo-v0.0.26-linux-amd64.tar.gz +cd MediaStationGo-v0.0.26-linux-amd64 MEDIASTATION_APP_PORT=18080 ./mediastation-go ``` Windows example: ```powershell -Expand-Archive .\MediaStationGo-v0.0.25-windows-amd64.zip -cd .\MediaStationGo-v0.0.25-windows-amd64 +Expand-Archive .\MediaStationGo-v0.0.26-windows-amd64.zip +cd .\MediaStationGo-v0.0.26-windows-amd64 $env:MEDIASTATION_APP_PORT = "18080" .\mediastation-go.exe ``` diff --git a/docker-compose.yml b/docker-compose.yml index 1e4f85c..90c0cc7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,7 +17,7 @@ # # 镜像版本: # 默认拉取 latest;如需固定版本,创建 .env 并写入: -# MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.25 +# MEDIASTATION_IMAGE_TAG=MediaStationGo-v0.0.26 # # 路径映射总览: # /data 程序数据目录。保存 SQLite 数据库、JWT secret、系统配置等,必须持久化。 diff --git a/internal/service/telegram_api_test.go b/internal/service/telegram_api_test.go index cb8966f..8e838ee 100644 --- a/internal/service/telegram_api_test.go +++ b/internal/service/telegram_api_test.go @@ -96,3 +96,18 @@ func TestTelegramProxyCandidatesDefaultLocalFallbacks(t *testing.T) { } } } + +func TestTelegramCommandFiltering(t *testing.T) { + if telegramIsCommandText("今天看什么") { + t.Fatal("plain chat message should not be treated as command") + } + if !telegramIsCommandText("/start user pass") { + t.Fatal("/start should be treated as command") + } + if got := telegramCommandName("/hideadult@MediaStationGoBot on"); got != "/hideadult" { + t.Fatalf("telegramCommandName = %q, want /hideadult", got) + } + if telegramSupportedCommand("/签到") { + t.Fatal("unrelated group bot command should not be handled") + } +} diff --git a/internal/service/telegram_bot.go b/internal/service/telegram_bot.go index 859fff6..52bf8f2 100644 --- a/internal/service/telegram_bot.go +++ b/internal/service/telegram_bot.go @@ -107,6 +107,12 @@ func (s *TelegramBotService) HandleWebhook(ctx context.Context, body []byte) err msg := update.Message text := strings.TrimSpace(msg.Text) + if !telegramIsCommandText(text) { + return nil + } + if msg.Chat.Type != "" && msg.Chat.Type != "private" && !telegramSupportedCommand(telegramCommandName(text)) { + return nil + } s.log.Info("telegram command received", zap.Int("chat_id", msg.Chat.ID), @@ -150,7 +156,7 @@ func (s *TelegramBotService) executeCommand(ctx context.Context, channel *model. return telegramCommandReply{}, nil } - cmd := strings.ToLower(parts[0]) + cmd := telegramCommandName(parts[0]) args := parts[1:] if msg.Chat.Type != "" && msg.Chat.Type != "private" && !s.telegramChatAllowed(channel, msg.Chat.ID) { return telegramCommandReply{Text: "此群组/频道未绑定到 Bot 管理入口,请在通知渠道里填写「绑定群组 ID」或「绑定频道 ID」。"}, nil @@ -188,6 +194,34 @@ func (s *TelegramBotService) executeCommand(ctx context.Context, channel *model. } } +func telegramIsCommandText(text string) bool { + return strings.HasPrefix(strings.TrimSpace(text), "/") && telegramCommandName(text) != "" +} + +func telegramCommandName(text string) string { + parts := strings.Fields(strings.TrimSpace(text)) + if len(parts) == 0 { + return "" + } + cmd := strings.ToLower(strings.TrimSpace(parts[0])) + if !strings.HasPrefix(cmd, "/") { + return "" + } + if at := strings.Index(cmd, "@"); at > 0 { + cmd = cmd[:at] + } + return cmd +} + +func telegramSupportedCommand(cmd string) bool { + switch cmd { + case "/start", "/help", "/hideadult", "/hide_adult", "/adult", "/status", "/search", "/downloads", "/stats": + return true + default: + return false + } +} + // cmdStart 处理 /start 命令。 func (s *TelegramBotService) cmdStart(ctx context.Context, msg *TelegramMessage, args []string) telegramCommandReply { name := msg.From.FirstName