mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 12:06:38 +08:00
fix: ignore non-command telegram group messages
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user