From 9c83ed98c602418b3262d91fd38be2a2c9064387 Mon Sep 17 00:00:00 2001 From: db123 Date: Wed, 24 Jun 2026 09:52:28 +0330 Subject: [PATCH] fix: keep keyboard update message alive instead of send-delete pattern Previous approach (send \u200C then immediately delete it) lost the reply keyboard because Telegram may not persist the keyboard from a deleted message. Now we delete the OLD keyboard message and keep the new one, ensuring the reply keyboard is always present and correct. --- internal/bot/handlers.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/bot/handlers.go b/internal/bot/handlers.go index 7d03d04..5fa3f4a 100644 --- a/internal/bot/handlers.go +++ b/internal/bot/handlers.go @@ -21,6 +21,7 @@ type Handler struct { DB *db.Store AllowedUsers map[int64]bool lastMsgTime map[int64]time.Time + keyboardMsg map[int64]int mu sync.Mutex } @@ -30,6 +31,7 @@ func NewHandler(bot *tgbotapi.BotAPI, store *db.Store, allowed map[int64]bool) * DB: store, AllowedUsers: allowed, lastMsgTime: make(map[int64]time.Time), + keyboardMsg: make(map[int64]int), } } @@ -359,11 +361,19 @@ func (h *Handler) handleStart(msg *tgbotapi.Message) { } func (h *Handler) updateReplyKeyboard(chatID int64) { + h.mu.Lock() + oldID := h.keyboardMsg[chatID] + h.mu.Unlock() + if oldID != 0 { + h.Bot.Request(tgbotapi.NewDeleteMessage(chatID, oldID)) + } msg := tgbotapi.NewMessage(chatID, "\u200C") msg.ReplyMarkup = h.smartKeyboard(chatID) sent, err := h.Bot.Send(msg) if err == nil { - h.Bot.Request(tgbotapi.NewDeleteMessage(chatID, sent.MessageID)) + h.mu.Lock() + h.keyboardMsg[chatID] = sent.MessageID + h.mu.Unlock() } }