P
P
Pavel Tsarenkov2022-01-13 16:28:06
Node.js
Pavel Tsarenkov, 2022-01-13 16:28:06

How to end wiretapping bot.on?

I have the following code, in which I implement a check for the correctness of the answer in the quiz:

let proverka = function() {
       
        bot.on('message', msg => {
            if (msg.text === правильный ответ) {
                bot.sendMessage(msg.chat.id, 'Правильно')
            }
        })
        
    }
    proverka()


The question is this: if the answer is correct, bot.on listening should stop working. How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lord Legitov, 2022-01-13
@Jaguar1616

function proverka(msg) {
    if (msg.text == "правильный ответ") {
          bot.sendMessage(msg.chat.id, 'Правильно');
          bot.removeListener("message", proverka);
    };
};

bot.on('message', proverka);
// Дальше свои обработчики добавляй

D
Dmitry Belyaev, 2022-01-13
@bingo347

https://nodejs.org/dist/latest-v16.x/docs/api/even...
The function given in .off must be the same as given in .on

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question