Skip to content

Commit 1d6ca83

Browse files
committed
feat(telegram): add DeleteMessage API method
Adds Bot.DeleteMessage(chatID, messageID) for deleting bot messages. Useful for cleanup — removing 'queued' notices, trace messages, and stale responses. Wired into testServer for handler tests.
1 parent 8fbb590 commit 1d6ca83

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

internal/telegram/bot.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ func (b *Bot) EditMessageText(chatID int64, messageID int, text string, opts *Se
293293
return b.doJSON("editMessageText", params, nil)
294294
}
295295

296+
// DeleteMessage deletes a message previously sent by the bot.
297+
// Requires the bot to have can_delete_messages permission in groups/supergroups.
298+
func (b *Bot) DeleteMessage(chatID int64, messageID int) error {
299+
params := map[string]any{
300+
"chat_id": chatID,
301+
"message_id": messageID,
302+
}
303+
return b.doJSON("deleteMessage", params, nil)
304+
}
305+
296306
// SendPhoto sends a photo from a file path to the specified chat.
297307
// opts may contain ReplyToMessageID to reply to a specific message.
298308
func (b *Bot) SendPhoto(chatID int64, path string, caption string, opts *SendOpts) (*Message, error) {

internal/telegram/bot_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,56 @@ func TestBot_EditMessageText_Error(t *testing.T) {
10901090
}
10911091
}
10921092

1093+
// ---------------------------------------------------------------------------
1094+
// DeleteMessage
1095+
// ---------------------------------------------------------------------------
1096+
1097+
func TestBot_DeleteMessage_Success(t *testing.T) {
1098+
var gotBody map[string]any
1099+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1100+
path := strings.TrimSuffix(r.URL.Path, "/")
1101+
if path != "/deleteMessage" {
1102+
t.Errorf("unexpected path: %s", path)
1103+
}
1104+
requireJSONBody(t, r, "application/json", &gotBody)
1105+
okResponse(w, true)
1106+
}))
1107+
defer ts.Close()
1108+
1109+
bot := NewBot("x")
1110+
bot.BaseURL = ts.URL
1111+
1112+
err := bot.DeleteMessage(123, 456)
1113+
if err != nil {
1114+
t.Fatalf("DeleteMessage: %v", err)
1115+
}
1116+
1117+
if gotBody["chat_id"] != float64(123) {
1118+
t.Errorf("chat_id = %v, want 123", gotBody["chat_id"])
1119+
}
1120+
if gotBody["message_id"] != float64(456) {
1121+
t.Errorf("message_id = %v, want 456", gotBody["message_id"])
1122+
}
1123+
}
1124+
1125+
func TestBot_DeleteMessage_Error(t *testing.T) {
1126+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1127+
failResponse(w, 400, "Bad Request: message to delete not found")
1128+
}))
1129+
defer ts.Close()
1130+
1131+
bot := NewBot("x")
1132+
bot.BaseURL = ts.URL
1133+
1134+
err := bot.DeleteMessage(1, 999)
1135+
if err == nil {
1136+
t.Fatal("expected error, got nil")
1137+
}
1138+
if !strings.Contains(err.Error(), "message to delete not found") {
1139+
t.Errorf("error = %q, want substring %q", err, "message to delete not found")
1140+
}
1141+
}
1142+
10931143
// ---------------------------------------------------------------------------
10941144
// SetFallbackURLs
10951145
// ---------------------------------------------------------------------------

internal/telegram/handler_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func testServer(t *testing.T, recorder *requestRecorder) *httptest.Server {
4545
switch {
4646
case strings.HasSuffix(r.URL.Path, "/answerCallbackQuery"):
4747
resp = map[string]any{"ok": true}
48+
case strings.HasSuffix(r.URL.Path, "/deleteMessage"):
49+
resp = map[string]any{"ok": true}
4850
case strings.HasSuffix(r.URL.Path, "/sendMessage"):
4951
resp = map[string]any{
5052
"ok": true,

0 commit comments

Comments
 (0)