mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
fix: send message in group failed
This commit is contained in:
@@ -146,15 +146,26 @@ func normalizeTarget(raw string) string {
|
||||
if strings.HasPrefix(value, "@") {
|
||||
return value
|
||||
}
|
||||
isNumeric := true
|
||||
for _, r := range value {
|
||||
if r < '0' || r > '9' {
|
||||
isNumeric = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isNumeric {
|
||||
if isTelegramChatID(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" {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user