yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.31k stars 1.51k forks source link

delete Message #328

Closed mafia-007 closed 7 years ago

mafia-007 commented 7 years ago

hi How Reply a message is deleted? If the code is correct? What code is deleted with group messaging? If the code is correct?

bot.onText(/^[/!#]delete$/, msg => {

bot.deleteMessage(msg.chat.id, msg.reply_to_message.id, form = {}); });

please guide me

savely-krasovsky commented 7 years ago

@mafia-007 lib is not updated yet. So you can't use deleteMessage method. Wait an update :)

ghost commented 7 years ago

This was added in cda9d8d. I think this issue can be closed now.

telegram-api commented 7 years ago

How delete all messages in the group? Or limit == 100 messages ? Please A example you mention. Thanks

ghost commented 7 years ago

You cannot delete all messages, only the ones sent since the bot was added, maybe unless you manually tell the bot to delete each message before that separately, but then it's not any faster than deleting them manually yourself. Bots don't get the history of messages sent before they are first added, not even in supergroups. Why don't you just delete and re-create the group?

telegram-api commented 7 years ago

Here's an example to remove 100 message in the group

By loop

GochoMugo commented 7 years ago

@mafia-007:

From our test suite (test/telegram.js#L891):

it('should delete message', function test() {
    return bot.deleteMessage(USERID, messageId).then(resp => {
        assert.equal(resp, true);
    });
});

API Reference: TelegramBot#deleteMessage().


@telegram-api: you could follow https://github.com/GochoMugo/tgfancy/issues/13.


@nyuszika7h Thanks for the help handling this issue. :+1: :+1: :+1:

@Lord-Protector We going to release it soon. Please hold on. :wink:

telegram-api commented 7 years ago

Hello @GochoMugo Please send an example with this method

bot.deleteMessages(chatID, [msgID1, msgID2, msgID3, / ... msgIDN /]);

Help me

savely-krasovsky commented 7 years ago

@telegram-api

const arrayOfMessages = [msgID1, msgID2, msgID3, /* ... msgIDN */];
for (let i in arrayOfMessages) {
    await bot.deleteMessage(msg.chat.id, arrayOfMessages[i]);
}

But of course this construction should be in async function. And yes, you can just remove await word, but then if you will delete many messages (30+) Telegram will just ban you because of spaming. So you should do synchronous cycle to avoid via async/await it or anti-patterns with promises.

Also you can modify it and by using Promise.all resolve groups with for example 10 deleteMessage-promises and then do it with something like setInterval to speed up synchronous cycle and avoid bans from Bot API.

telegram-api commented 7 years ago

bot.onText(/^[\/!#]del$/, msg => {

var arrayOfMessages = [msg.message_id1, msg.message_id2, msg.message_id3, / ... msg.message_idN /];

for (let i in arrayOfMessages) { bot.deleteMessage(msg.chat.id, arrayOfMessages[i]); } });

Is true?

savely-krasovsky commented 7 years ago

@telegram-api as I said with many messages telegram will ban you. This code is better, but requires Node.js 7+.

bot.onText(/^[/!#]del$/, async function (msg) {
    const arrayOfMessages = [msgID1, msgID2, msgID3, /* ... msgIDN */];
    for (let i in arrayOfMessages) {
        await bot.deleteMessage(msg.chat.id, arrayOfMessages[i]);
    }
});
telegram-api commented 7 years ago

@Lord-Protector http://uupload.ir/files/9f9e_ftrt.jpg

savely-krasovsky commented 7 years ago

@telegram-api you should specify msg ids that you give in array. For example:

let arrayOfMessages = [];
for (let i = 0; i < 100; i++) {
    arrayOfMessages.push(msg.message_id - i);
}
riseremi commented 7 years ago

Any news on this?

savely-krasovsky commented 7 years ago

@riseremi use master-branch. I don't know why they don't update npm yet. I already switched on Telegraf.js lib -__-

riseremi commented 7 years ago

@Lord-Protector oh thank you!

GochoMugo commented 7 years ago

Sorry guys, we have been unavailable for some time now. We shall push a new release to npm this week. Will close this issue as soon as we do so. Sorry for the wait.

GochoMugo commented 7 years ago

New Release, v0.28.0 :confetti_ball:

riseremi commented 7 years ago

Nice, thank you!

kbryamlk commented 3 years ago

We suffer from this problem a lot. We kindly ask you to publish a code against banning Telegram so that we can fix the problems of our bots

PanditSiddharth commented 1 year ago

hi How Reply a message is deleted? If the code is correct? What code is deleted with group messaging? If the code is correct?

bot.onText(/^[/!#]delete$/, msg => {

bot.deleteMessage(msg.chat.id, msg.reply_to_message.id, form = {}); });

please guide me

in place of msg.reply_to_message.id use msg.reply_to_message.message_id