-1

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

JrBenito
  • 973
  • 8
  • 30
Matt
  • 127
  • 1
  • 13
  • Also, Just for confirmation that `delete_history` isn't available for bots even when using direct mtproto (telegram protocol) based libs, check out [this documentation in telethon](https://tl.telethon.dev/methods/messages/delete_history.html) for it – Tibebes. M Oct 25 '20 at 19:34

3 Answers3

3

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)
}
0

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.
Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • EHmmm..but i see sometimes BOTS that after done all things (also after 2 days) delete all the chat. Or other that when i play starts deletes all the messages and then they send a message. I utilized one almost 2 weeks ago and deletes all (also sent from more then 2 days). Then i uses wizardscenes so how can i delete a message sent by the bot? – Matt Oct 25 '20 at 19:07
  • Well, that's what the official doc. states and note that we're speaking of Telegraf here (which essentially operates via HTTP Bot API). – Tibebes. M Oct 25 '20 at 19:15
  • These bots most probably are running on some mtproto libraries such as [telethon](https://docs.telethon.dev/en/latest/), [pyrogram](https://docs.pyrogram.org/), etc.. (since it's allowed to [delete messages without the 48hrs restrictions as of 24 March, 2019](https://telegram.org/blog/unsend-privacy-emoji) in telegram clients). But you would have to use mtproto protocol and not the HTTP api I believe (as stated in the documentation I've attached in the answer) – Tibebes. M Oct 25 '20 at 19:18
  • ah ok thank you, so there isn't any app like this for javascript? I do not know PHP – Matt Oct 25 '20 at 20:59
  • These are in python but there are a couple of alternatives in JavaScript as well like [mtproto-core](https://github.com/alik0211/mtproto-core) and [GramJs](https://github.com/gram-js/gramjs) – Tibebes. M Oct 25 '20 at 21:17
0
bot.command('delete', async (ctx) => {
    let i = 0;
    while(true) {
        try {
            await ctx.deleteMessage(ctx.message.message_id - i++);
        } catch(e) {
            break;
        }
    }
}
Аис
  • 1
  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Also, a very similar code was already provided by [these answer](https://stackoverflow.com/a/66155706/3641635), thus i suggest you to remove your answer and instead add a comment on Kayra's answer regarding the asynchronous approach – Zilog80 Dec 03 '22 at 17:31