Telegraf JS: how can i clear history of a chat?

6.8k views Asked by At

How can i Clear the Chat History, in my case the Bot Chat history with Telegraf.js? I saw in Telegram API that is there a way to clear History of a chat: https://core.telegram.org/method/messages.deleteHistory

WIth messages.deleteHistory(), but i cannot find nothing on Telegraf docs.

So how can i do with Telegraf to clear the history?

Thank you

3

There are 3 answers

5
Tibebes. M On

Telegraf.js uses the official Telegram BOT Api behind the scenes (accessed using HTTP).

And messages.deleteHistory() is a core API method (accessed using MTProto protocol). Also note that the method can only be invoked by Regular Users and not Bots (even if you consider using a mtproto based libs. to call it)

In other words, Bots can't do that and there isn't such method in the HTTP bot API. The best you can do is use deleteMessage.

Use this method to delete a message, including service messages, with the following limitations:

  • A message can only be deleted if it was sent less than 48 hours ago.
  • A dice message in a private chat can only be deleted if it was sent more than 24 hours ago.
  • Bots can delete outgoing messages in private chats, groups, and supergroups.
  • Bots can delete incoming messages in private chats.
  • Bots granted can_post_messages permissions can delete outgoing messages in channels.
  • If the bot is an administrator of a group, it can delete any message there.
  • If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there. Returns True on success.
1
Kayra Berk Tuncer On

I've tried such a way, but it won't be enough.

bot.command('delete', (ctx) =>{
let k = 0;
for(let i = 0; i <= 100; i++ ){
    k =  ctx.message.message_id-i;
    ctx.deleteMessage(k)
}
1
Аис On
bot.command('delete', async (ctx) => {
    let i = 0;
    while(true) {
        try {
            await ctx.deleteMessage(ctx.message.message_id - i++);
        } catch(e) {
            break;
        }
    }
}