G
G
gleendo2020-12-05 10:19:45
Node.js
gleendo, 2020-12-05 10:19:45

How to clear channel history using telegram bot?

Tell me how to clear, for example, every 10 minutes the history of the channel using a bot?
The bot will send messages to the channel every 30 seconds, and it is necessary that the channel be cleared over time. Can it be done?
I usenode-telegram-bot-api

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey, 2020-12-05
@Azperin

I would suggest something like this

let messagesToDelete = [];
telegramBot.sendMessage('some').then((m) => {
  messagesToDelete.push({
    messageId: m.message_id,
    chatId: m.chat.id
  });
}).catch(console.error);

function wipeHistory() {
  messagesToDelete.forEach(m => {
    telegramBot.deleteMessage(m.chatId, m.messageId).then((success) => {
      if (success) {
        messagesToDelete = messagesToDelete.filter((v) => (v.messageId !== m.message_id && v.chatId !== m.chat.id));
      };
    }).catch(console.error);
  });
};

setInterval(wipeHistory, 60 * 10 * 1000);

A
Alex K, 2020-12-05
@alexk111

I will not give out the finished code, but the algorithm will be as follows:
- when adding a message using the `sendMessage` method - remember its `message_id`
- delete messages by the timer using the `deleteMessage` method for each `message_id`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question