fix: send message in group failed

This commit is contained in:
Acbox
2026-02-20 22:46:38 +08:00
parent 313c3b84f3
commit cb5d2c5fab
2 changed files with 25 additions and 8 deletions
+19 -8
View File
@@ -146,15 +146,26 @@ func normalizeTarget(raw string) string {
if strings.HasPrefix(value, "@") { if strings.HasPrefix(value, "@") {
return value return value
} }
isNumeric := true if isTelegramChatID(value) {
for _, r := range value {
if r < '0' || r > '9' {
isNumeric = false
break
}
}
if isNumeric {
return value return value
} }
return "@" + value return "@" + value
} }
// isTelegramChatID returns true when s looks like a Telegram numeric chat ID,
// which may be negative (e.g. supergroup IDs like -1002280927535).
func isTelegramChatID(s string) bool {
digits := s
if strings.HasPrefix(digits, "-") {
digits = digits[1:]
}
if len(digits) == 0 {
return false
}
for _, r := range digits {
if r < '0' || r > '9' {
return false
}
}
return true
}
@@ -85,4 +85,10 @@ func TestNormalizeTarget(t *testing.T) {
if got := normalizeTarget("@alice"); got != "@alice" { if got := normalizeTarget("@alice"); got != "@alice" {
t.Fatalf("unexpected normalized target: %s", got) t.Fatalf("unexpected normalized target: %s", got)
} }
if got := normalizeTarget("123456789"); got != "123456789" {
t.Fatalf("positive chat ID mangled: %s", got)
}
if got := normalizeTarget("-1002280927535"); got != "-1002280927535" {
t.Fatalf("negative supergroup chat ID mangled: %s", got)
}
} }