S
S
szjyakgf2022-02-11 19:42:11
Python
szjyakgf, 2022-02-11 19:42:11

How can a bot change a message?

How to make a telegram bot change messages

@dp.message_handler(commands=['start'])
async def start(message: types.Message):
  await bot.send_message(message.chat.id, 'Ждем: 2сек')
  await sleep(1)
  await bot.edit_message_text(chat_id=message.chat.id, message_id=message.message_id, text='Ждем: 1сек')
  await sleep(1)
  await bot.edit_message_text(chat_id=message.chat.id, message_id=message.message_id, text='Готово')

But it gives an error aiogram.utils.exceptions.MessageCantBeEdited: Message can't be edited

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shurshur, 2022-02-11
@szjyakgf

message in this function is the message of the user who wrote /start. Then the bot tries to edit this message... and it doesn't work! Of course, because he can only edit his own messages, but not others.
He should just edit his own post, not the user's post:

my_new_message = await bot.send_message(message.chat.id, 'Ждем: 2сек')
await sleep(1)
await bot.edit_message_text(chat_id=message.chat.id, message_id=my_new_message.message_id, text='Ждем: 1сек')
...

G
Georgy Kharitonov, 2022-02-11
@NoGrisha

First you need to take your first message and shove it into a variable and manage to send it using this line:

m = await bot.send_message(message.chat.id, 'Ждем: 2сек')

Then you also change it asynchronously: Maybe this is not true, test it, because without an error it's hard to say something right off the bat... await m.edit(content = "Ждем: 1сек")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question