1
votes

I have a telegram bot written in Python. It sends message on specific commands as per mentioned in code. I want to delete the replies sent by this bot suppose after X seconds. There is a telegram bot API that deletes the message

https://api.telegram.org/botBOTID/deleteMessage?chat_id=?&message_id=?

To delete the message we need chat id and message id. To get the chat id and message id of the replied message by the bot, I need to keep reading all the messages (even from users) and find these id's. This will increase a lot of overhead on the bot.

Is there any other way to find these id's without reading all the messages?

2
Can't you just get the last messages or? - user7111497
@AfloroaieRobert If the group is having many members, it will have many messages incoming.. - rock321987
Hey Did You Find Any Solution To Delete Bot Messages In Supergroup? - shailu

2 Answers

1
votes

In nodeJs I use these codes to delete the replies sent by bot after 10 seconds:

let TelegramBot = require('node-telegram-bot-api');
let bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
    let chatId = msg.chat.id;
    let botReply = "A response from the bot that will removed after 10 seconds"
    bot.sendMessage(chatId ,botReply)
        .then((result) => { setTimeout(() => {
            bot.deleteMessage(chatId, result.message_id)
        }, 10 * 1000)})
        .catch(err => console.log(err))
}
0
votes

This is the Chat object. It contains the identifier of the chat.

partial screenshot of chat object

This is the Message object. It contains the identifier for that message and a Chat object representing the coversation where it resides.

partial screenshot of message object

The sendMessage REST function returns the Message you sent on success.

partial screenshot of sendMessage documentation

So your solution here is to store the Message object you get when sending a message, and then call the delete api with the parameters from the stored objects (Message.message_id and Message.chat.id).

Regarding Python you can use the pickle module to store objects in files.