You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

aiogram中MessageCantBeEdited错误:Telegram机器人无法编辑消息求助

解决Telegram机器人编辑消息报错问题

错误原因

你当前的代码存在两个关键问题:

  1. 调用message.edit_text()是尝试编辑用户发送的原始消息,但Telegram规则不允许机器人编辑非自身发送的消息,这直接触发MessageCantBeEdited错误。
  2. 使用同步的time.sleep(0.5)会阻塞异步事件循环,可能导致后续操作异常。

修正后的代码

保存机器人发送的回复消息对象,再对该对象执行编辑操作,同时替换为异步休眠方法:

import asyncio
from aiogram import types, Dispatcher

@dp.message_handler()
async def echo_msg(message: types.Message):
    # 保存机器人发送的回复消息实例
    bot_sent_msg = await message.reply("Hi")
    # 异步休眠,避免阻塞事件循环
    await asyncio.sleep(0.5)
    # 编辑机器人自身发送的消息
    await bot_sent_msg.edit_text("hello")

额外注意事项

  • 若通过聊天ID+消息ID编辑,需确保消息ID属于机器人发送的消息,示例代码:
    await dp.bot.edit_message_text(
        chat_id=message.chat.id,
        message_id=bot_sent_msg.message_id,
        text="hello"
    )
    
  • 群组/频道环境下,需确保机器人拥有「编辑消息」的权限,私聊场景默认具备该权限。

内容的提问来源于stack exchange,提问作者AYUSH Pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 00:37:06