Skip to content

Commit 8cf3ba4

Browse files
committed
fix(telegram): route edited messages to same handler as new messages
Telegram sends edited_message in updates when users edit their messages. Previously, edited messages were silently dropped because the Update struct lacked the EditedMessage field. Now they are routed through handleMessage like regular messages — supporting both text and commands. 2 new tests: edited message routing, edited command routing
1 parent d75ac64 commit 8cf3ba4

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

internal/telegram/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ func (h *Handler) HandleUpdate(upd Update) {
165165
switch {
166166
case upd.Message != nil:
167167
h.handleMessage(upd.Message)
168+
case upd.EditedMessage != nil:
169+
h.handleMessage(upd.EditedMessage)
168170
case upd.CallbackQuery != nil:
169171
h.handleCallback(upd.CallbackQuery)
170172
default:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package telegram
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestHandleUpdate_EditedMessage verifies that edited messages are routed
8+
// to the same handler as regular messages.
9+
func TestHandleUpdate_EditedMessage(t *testing.T) {
10+
var (
11+
capturedChatID int64
12+
capturedText string
13+
capturedEdit bool
14+
)
15+
ts := testServer(t, nil)
16+
defer ts.Close()
17+
bot := testBot(t, ts)
18+
h := NewHandler(bot)
19+
h.OnTextMessage = func(chatID int64, messageID int, text string) (string, error) {
20+
capturedChatID = chatID
21+
capturedText = text
22+
// messageID should be set for edited messages too
23+
capturedEdit = (messageID > 0)
24+
return "ok", nil
25+
}
26+
27+
// Simulate an edited_message update from Telegram.
28+
upd := Update{
29+
ID: 1,
30+
EditedMessage: &Message{
31+
ID: 42,
32+
Chat: &Chat{ID: 123},
33+
From: &User{ID: 456},
34+
Text: "edited content",
35+
},
36+
}
37+
38+
h.HandleUpdate(upd)
39+
40+
if capturedChatID != 123 {
41+
t.Errorf("chatID = %d, want 123", capturedChatID)
42+
}
43+
if capturedText != "edited content" {
44+
t.Errorf("text = %q, want %q", capturedText, "edited content")
45+
}
46+
if !capturedEdit {
47+
t.Error("edited message should be routed to OnTextMessage")
48+
}
49+
}
50+
51+
// TestHandleUpdate_EditedMessageWithCommand verifies that edited commands
52+
// are also routed through the command handler.
53+
func TestHandleUpdate_EditedMessageWithCommand(t *testing.T) {
54+
var (
55+
capturedCmd string
56+
capturedArgs string
57+
)
58+
ts := testServer(t, nil)
59+
defer ts.Close()
60+
bot := testBot(t, ts)
61+
h := NewHandler(bot)
62+
h.OnCommand = func(chatID int64, messageID int, cmd string, args string) (string, error) {
63+
capturedCmd = cmd
64+
capturedArgs = args
65+
return "ok", nil
66+
}
67+
68+
upd := Update{
69+
ID: 2,
70+
EditedMessage: &Message{
71+
ID: 99,
72+
Chat: &Chat{ID: 111},
73+
From: &User{ID: 222},
74+
Text: "/start arg1",
75+
Entities: []MessageEntity{
76+
{Type: "bot_command", Offset: 0, Length: 6},
77+
},
78+
},
79+
}
80+
81+
h.HandleUpdate(upd)
82+
83+
if capturedCmd != "start" {
84+
t.Errorf("cmd = %q, want %q", capturedCmd, "start")
85+
}
86+
if capturedArgs != "arg1" {
87+
t.Errorf("args = %q, want %q", capturedArgs, "arg1")
88+
}
89+
}

internal/telegram/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
type Update struct {
1313
ID int `json:"update_id"`
1414
Message *Message `json:"message,omitempty"`
15+
EditedMessage *Message `json:"edited_message,omitempty"`
1516
CallbackQuery *CallbackQuery `json:"callback_query,omitempty"`
1617
}
1718

0 commit comments

Comments
 (0)