skip to Main Content

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

Answers


  1. 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.
    Login or Signup to reply.
  2. 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)
    }
    
    Login or Signup to reply.
  3. bot.command('delete', async (ctx) => {
        let i = 0;
        while(true) {
            try {
                await ctx.deleteMessage(ctx.message.message_id - i++);
            } catch(e) {
                break;
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search