editMessageCaption
Use this method to edit captions of messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time they were sent.
Parameters
business_connection_idStringOptionalUnique identifier of the business connection on behalf of which the message to be edited was sent
chat_idIntegerStringOptionalRequired if inline\message\id is not specified. Unique identifier for the target chat or username of the target channel (in the format
@channelusername)message_idIntegerOptionalRequired if inline\message\id is not specified. Identifier of the message to edit
inline_message_idStringOptionalRequired if chat\id and message\id are not specified. Identifier of the inline message
New caption of the message, 0-1024 characters after entities parsing
parse_modeStringOptionalMode for parsing entities in the message caption. See formatting options for more details.
A JSON-serialized list of special entities that appear in the caption, which can be specified instead of parse\_mode
show_caption_above_mediaBooleanOptionalPass True, if the caption must be shown above the message media. Supported only for animation, photo and video messages.
A JSON-serialized object for an inline keyboard.
Returns
On success, Message | True is returned.
GramIO Usage
ts
// Update a photo/video caption when a user presses a button
bot.on("callback_query", async (ctx) => {
await ctx.editCaption("Caption updated!");
await ctx.answer();
});ts
// Edit caption with rich formatting — entities are built automatically
bot.on("callback_query", async (ctx) => {
await ctx.editCaption(
format`${bold("Updated:")} new caption text`,
);
await ctx.answer();
});ts
// Remove a caption by passing an empty string
bot.on("callback_query", async (ctx) => {
await ctx.editCaption("");
await ctx.answer();
});ts
// Direct API call — edit caption and show it above the media
await bot.api.editMessageCaption({
chat_id: "@mychannel",
message_id: 123,
caption: "Updated caption for this photo",
show_caption_above_media: true,
});Errors
| Code | Error | Cause |
|---|---|---|
| 400 | Bad Request: message is not modified | New caption is identical to current — check before calling to avoid unnecessary requests |
| 400 | Bad Request: message can't be edited | Message too old, sent by another bot, or is not a media message with a caption |
| 400 | Bad Request: CAPTION_TOO_LONG | caption exceeds 1024 characters |
| 400 | Bad Request: can't parse entities | Malformed HTML/Markdown markup — use GramIO's format helper to build caption_entities safely |
| 400 | Bad Request: chat not found | Invalid or inaccessible chat_id |
| 400 | Bad Request: message not found | message_id doesn't exist in the chat |
| 403 | Forbidden: bot was blocked by the user | User blocked the bot — catch and mark user as inactive |
| 403 | Forbidden: not enough rights | Bot lacks edit permissions in a channel |
| 429 | Too Many Requests: retry after N | Flood control — check retry_after, use auto-retry plugin |
TIP
Use GramIO's auto-retry plugin to handle 429 errors automatically.
Tips & Gotchas
- Caption limit is 1024 characters — half the 4096-character limit for text messages. Plan your content layout accordingly.
- Caption can be cleared by passing
""— unlikeeditMessageText, the caption is optional and can be set to zero characters to remove it entirely. parse_modeandcaption_entitiesare mutually exclusive. GramIO'sformathelper always producescaption_entities, so never passparse_modealongside a formatted string.show_caption_above_mediaonly works for animation, photo, and video — it has no effect on documents or audio messages.- Business messages have a 48-hour edit window. Messages sent via a business connection by another user without an inline keyboard can only be edited within 48 hours of sending.
- Inline messages return
true, notMessage. When editing viainline_message_id, the method returnstrueon success instead of the updatedMessageobject.
See Also
- editMessageText — edit the text of a text message
- editMessageMedia — replace the media file itself
- editMessageReplyMarkup — update only the inline keyboard
- Formatting guide — using
format,bold,italic, and more - Keyboards overview — building inline keyboards with
InlineKeyboard - Message — the type returned on success for non-inline messages
- auto-retry plugin — automatic
429retry handling