Skip to main content
Glama

edit_message

Modify existing Telegram message text or inline keyboard by specifying chat ID, message ID, and new content. Update messages without deleting and reposting them.

Instructions

Edit the text and/or inline keyboard of an existing message.

Args: chat_id: Chat containing the message. message_id: ID of the message to edit. text: New message text. buttons: New inline keyboard (None to remove buttons). parse_mode: HTML, Markdown, MarkdownV2, or None.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
chat_idYes
message_idYes
textYes
buttonsNo
parse_modeNoHTML

Implementation Reference

  • The implementation of the `edit_message` tool.
    async def edit_message(
        chat_id: int,
        message_id: int,
        text: str,
        buttons: list[list[dict[str, str]]] | None = None,
        parse_mode: str | None = "HTML",
    ) -> EditMessageResult:
        """Edit the text and/or inline keyboard of an existing message.
    
        Args:
            chat_id: Chat containing the message.
            message_id: ID of the message to edit.
            text: New message text.
            buttons: New inline keyboard (None to remove buttons).
            parse_mode: HTML, Markdown, MarkdownV2, or None.
        """
        if not ctx.is_chat_allowed(chat_id):
            result = EditMessageResult(
                ok=False,
                error=f"Chat {chat_id} is not in allowed_chat_ids.",
            )
            if ctx.audit_logger:
                ctx.audit_logger.log(
                    "edit_message",
                    {"chat_id": chat_id, "message_id": message_id, "text": text},
                    result.ok,
                    result.error,
                )
            return result
    
        reply_markup = None
        if buttons is not None:
            keyboard = _build_keyboard(buttons)
            if isinstance(keyboard, str):
                result = EditMessageResult(ok=False, error=keyboard)
                if ctx.audit_logger:
                    ctx.audit_logger.log(
                        "edit_message",
                        {"chat_id": chat_id, "message_id": message_id, "text": text},
                        result.ok,
                        result.error,
                    )
                return result
            reply_markup = keyboard
    
        try:
            if ctx.rate_limiter:
                await ctx.rate_limiter.acquire()
            api_result = await ctx.bot.edit_message_text(
                chat_id=chat_id,
                message_id=message_id,
                text=text,
                parse_mode=normalize_parse_mode(parse_mode),
                reply_markup=reply_markup,
            )
            if isinstance(api_result, bool):
                result = EditMessageResult(ok=True, message_id=message_id, chat_id=chat_id)
            else:
                result = EditMessageResult(
                    ok=True,
                    message_id=api_result.message_id,
                    chat_id=api_result.chat.id,
                )
        except ValueError as exc:
            result = EditMessageResult(ok=False, error=str(exc))
        except (TelegramBadRequest, TelegramForbiddenError) as exc:
            result = EditMessageResult(ok=False, error=str(exc))
    
        if ctx.audit_logger:
            ctx.audit_logger.log(
                "edit_message",
                {"chat_id": chat_id, "message_id": message_id, "text": text},
                result.ok,
                result.error,
            )
        return result
  • The return type schema for `edit_message`.
    class EditMessageResult(ToolResponse):
        message_id: int | None = None
        chat_id: int | None = None
  • Conditional registration of the edit_message tool.
    if allowed_tools is None or "edit_message" in allowed_tools:

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Py2755/aiogram-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server