Node-telegram-bot-api delay
-
I'm working on a telegram with node-telegram-bot-api. I made two buttons with keyboard. But when there's a lot of pressure on them, the bot will be spam sooner or later, he's jealous. Is there any way to delay the user on the message?
-
You can just create a stack of the mass and write a function for adding to the id-user. Check whether the text corresponds to the text of the button, if so, to put id in the stack and to start a timeout with the removal of the first elevent of the mass, as well as at the beginning of the message listener, to check id in the stack if it is not yet cleared to send the user restriction notice. ♪
let stopEpta = [];//стэк с id
function add(chatId){// функция принимает id пользователя
stopEpta.push(chatId);// добавляет его в стэк
return setTimeout(function(){stopEpta.shift()}, 7000);// через 7000 миллисекунд id пользователя будет удалён из стэка
}bot.on('message', function (msg){// бот слушает все сообщения
let chatId = msg.from.id, text = msg.text;// берём id пользователя и текст из сообщения
if(stopEpta.includes(chatId)) {// проверяем есть ли id пользователя в стэке
return bot.sendMessage(chatId,"Сейчас вы не можете обратиться к боту.");// если есть, возвращаем ему сообщение об ограничении
}if(text === "Текст кнопки") add(chatId);//Сверяем текст от пользователя и "Текст кнопки", если совпадает заносим пользователя в стэк
})