Editing Messages¶
Messages don't have to be permanent. Telegram lets bots edit text, captions, and keyboards on messages they sent — and Api exposes all of it.
You'll edit messages constantly in menu-driven bots: swap button labels, replace a status line, remove buttons after the user picks an option.
Edit text¶
Api.editMessageText({
chat_id: chat.id,
message_id: request.message.message_id,
text: "Updated content.",
parse_mode: "Markdown"
})
You need the message_id of the message you're changing. In a callback handler that's usually update.callback_query.message.message_id. If you just sent the message yourself, await Api.sendMessage(...) and chain .editText() instead — see Method Chaining.
Edit caption on media¶
Photos and documents use captions, not message text:
Api.editMessageCaption({
chat_id: chat.id,
message_id: request.message.message_id,
caption: "Revised caption."
})
Swap the inline keyboard¶
Remove buttons after a choice, or replace the whole menu:
Api.editMessageReplyMarkup({
chat_id: chat.id,
message_id: request.message.message_id,
reply_markup: {
inline_keyboard: [[{ text: "Done", callback_data: "done" }]]
}
})
Pass an empty inline_keyboard: [] to strip buttons entirely.
Delete instead of edit¶
Sometimes deletion is cleaner:
Or, if you have the chained response object:
What you can't edit¶
Bots can't edit messages they didn't send (with the usual Telegram exceptions around admins and channels). They also can't turn a text message into a photo — edit the text or send new media.
Telegram returns an error if the message is too old or the content is identical to what's already there. Check res.ok when using await.
Typical callback flow¶
- User taps inline button
Api.answerCallbackQuery(...)— dismiss the spinnerApi.editMessageText(...)— update the same message- Optionally
Bot.runCommand(...)— start a new flow
That pattern keeps chats tidy. One message morphs instead of five new ones stacking up.
Reference¶
Full parameter lists live in the Telegram docs: