James Baxter anarak
James Baxter anarak

Reputation: 3

telebot delete message pytelegrambotapi

How can I delete the last N number of messages from any users. After writing the delete command, the bot must delete the latest messages by the given number. I could not find complete documentation on the pytelegrambotapi library in any way.

Upvotes: 0

Views: 6011

Answers (1)

Danilo Cacace
Danilo Cacace

Reputation: 522

In my experience you should save the message_id of the lasts N messagges and cycling with the delete_message function when you need to do it.

bot = telegram.Bot(token=TOKEN)


message_ids = {}

message_id = bot.send_message(chat_id, text).message_id

if chat_id in message_ids.keys():
  message_ids[chat_id].append(message_id)
else:
  message_ids[chat_id] = [message_id]

When you need to delete the messages from some chat you can do (remember that messages after 48h aren't possible to be deleted)

for message_id in message_ids[chat_id]:
   bot.delete_message(chat_id, message_id)

I'm sure it's perfectible, but I think it's a good basic idea

Upvotes: 0

Related Questions