diff --git a/src/auto-reply/types.ts b/src/auto-reply/types.ts index fce285175..6f7c9ba87 100644 --- a/src/auto-reply/types.ts +++ b/src/auto-reply/types.ts @@ -57,4 +57,6 @@ export type ReplyPayload = { isStatusMessage?: boolean; isError?: boolean; editMessageId?: string; + /** Inline keyboard buttons (reply markup). */ + buttons?: Array>; }; diff --git a/src/telegram/bot/delivery.ts b/src/telegram/bot/delivery.ts index 3c6c4adbf..788a48f62 100644 --- a/src/telegram/bot/delivery.ts +++ b/src/telegram/bot/delivery.ts @@ -7,7 +7,7 @@ import { } from "../format.js"; import { chunkMarkdownTextWithMode, type ChunkMode } from "../../auto-reply/chunk.js"; import { splitTelegramCaption } from "../caption.js"; -import { recordSentMessage } from "../sent-message-cache.js"; +import { getLastSentMessage, recordSentMessage } from "../sent-message-cache.js"; import type { ReplyPayload } from "../../auto-reply/types.js"; import type { ReplyToMode } from "../../config/config.js"; import type { MarkdownTableMode } from "../../config/types.base.js"; @@ -83,20 +83,26 @@ export async function deliverReplies(params: { ? [reply.mediaUrl] : []; if (mediaList.length === 0) { - let sentMessageId: string | undefined; - const explicitEditId = reply.editMessageId; + // Check if the previous message was a status message. + // If so, we should edit it instead of sending a new message. + const lastSent = getLastSentMessage(chatId); + const shouldEditLast = + lastSent?.isStatus && lastSent.messageId && Date.now() - lastSent.timestamp < 120000; // 2 min threshold + + let sentMessageId: string | undefined; + + if (shouldEditLast && reply.text) { + const editId = String(lastSent!.messageId); - if (explicitEditId && reply.text) { - // Explicit edit requested by StatusUpdateController/agent-runner try { - await editMessageTelegram(chatId, explicitEditId, reply.text, { + await editMessageTelegram(chatId, editId, reply.text, { token: params.token, accountId: params.accountId, api: bot.api, }); - sentMessageId = explicitEditId; + sentMessageId = editId; } catch (err) { - logVerbose(`Telegram explicit edit failed, falling back to new message: ${String(err)}`); + logVerbose(`Telegram edit failed, falling back to new message: ${String(err)}`); // Fall through to send new message } }