Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-8450 fix(telegram): preserve literal bot mentions in private messages (#9769)

Zhang Yifancde4175ce340
diff --git a/server/internal/integrations/telegram/inbound.go b/server/internal/integrations/telegram/inbound.go
index 20e63f7852..47d02e45c9 100644
--- a/server/internal/integrations/telegram/inbound.go
+++ b/server/internal/integrations/telegram/inbound.go
@@ -150,6 +150,9 @@ func inboundFromUpdateWithContext(u Update, botID int64, botUsername string, rec
addressed := chatType == channel.ChatTypeP2P || mentioned || repliedToBot
cleaned := normalizeText(text, botUsername)
+ if chatType == channel.ChatTypeP2P {
+ cleaned = normalizePrivateText(text, botUsername)
+ }
commandText := cleaned
forceFresh := false
startChat := false
@@ -342,6 +345,25 @@ func normalizeText(text, botUsername string) string {
return strings.TrimSpace(cleaned)
}
+// Private messages already address the bot. Only a leading slash command's
+// routing suffix is transport syntax; mentions in the body are user content.
+func normalizePrivateText(text, botUsername string) string {
+ trimmed := strings.TrimLeft(text, " \t\r\n")
+ if !strings.HasPrefix(trimmed, "/") || botUsername == "" {
+ return text
+ }
+ end := strings.IndexAny(trimmed, " \t\r\n")
+ if end < 0 {
+ end = len(trimmed)
+ }
+ command := trimmed[:end]
+ if at := strings.LastIndexByte(command, '@'); at >= 0 && strings.EqualFold(command[at+1:], botUsername) {
+ start := len(text) - len(trimmed)
+ return text[:start+at] + trimmed[end:]
+ }
+ return text
+}
+
// enrichWithQuotedMessage prepends the one message the sender explicitly
// selected by replying; ambient group history never enters the agent context
// this way. A quoted file leads its caption as the placeholder the resolver
diff --git a/server/internal/integrations/telegram/mention_preservation_test.go b/server/internal/integrations/telegram/mention_preservation_test.go
new file mode 100644
index 0000000000..6723c81fdb
--- /dev/null
+++ b/server/internal/integrations/telegram/mention_preservation_test.go
@@ -0,0 +1,46 @@
+package telegram
+
+import "testing"
+
+func TestPrivateMessagesPreserveLiteralBotMentions(t *testing.T) {
+ for _, text := range []string{
+ "当然是 @example_bot 可以继续",
+ "当然是\n@example_bot\n可以继续",
+ "😀 @example_bot and @another_bot",
+ "@example_bot",
+ } {
+ for _, caption := range []bool{false, true} {
+ m := &Message{MessageID: 7, From: &User{ID: 111}, Chat: Chat{ID: 111, Type: "private"}}
+ want := text
+ if caption {
+ m.Caption = text
+ m.Photo = []PhotoSize{{FileID: "fake-photo"}}
+ want = "[Image]\n" + text
+ } else {
+ m.Text = text
+ }
+ msg, ok := inboundFromUpdate(Update{Message: m}, 999, "example_bot")
+ if !ok || msg.Text != want || msg.CommandText != text {
+ t.Errorf("caption=%v: got text=%q command=%q; want text=%q command=%q", caption, msg.Text, msg.CommandText, want, text)
+ }
+ }
+ }
+}
+
+func TestPrivateCommandsStripOnlyRoutingSuffix(t *testing.T) {
+ for _, tc := range []struct {
+ input, command, body string
+ fresh bool
+ }{
+ {"/issue@my_bot fix @my_bot", "/issue fix @my_bot", "/issue fix @my_bot", false},
+ {"/clear@MY_BOT continue @my_bot", "/clear continue @my_bot", "continue @my_bot", true},
+ {"/issue@another_bot mention @my_bot", "/issue@another_bot mention @my_bot", "/issue@another_bot mention @my_bot", false},
+ } {
+ msg, ok := inboundFromUpdate(Update{Message: &Message{
+ MessageID: 8, From: &User{ID: 111}, Chat: Chat{ID: 111, Type: "private"}, Text: tc.input,
+ }}, 999, "my_bot")
+ if !ok || msg.CommandText != tc.command || msg.Text != tc.body || msg.ForceFresh != tc.fresh {
+ t.Errorf("%q: command=%q text=%q fresh=%v", tc.input, msg.CommandText, msg.Text, msg.ForceFresh)
+ }
+ }
+}