Estoy trabajando en la creación de un bot de Telegram, quiero hacer un sistema antispam, es decir, cuando una persona presiona un botón demasiadas veces, el bot se congelará para él durante una cierta cantidad de segundos, es posible escribir un mensaje sobre el bloqueo. Acabo de empezar a aprender JavaScript. Yo uso node-telegram-bot-api.
import { bot } from '../token.js'; import { keyboardMain } from '../keyboards/keyboardsMain.js'; export function commands() { bot.on('message', msg => { const text = msg.text; const chatId = msg.chat.id; if (text === '/start') { return bot.sendMessage(chatId, 'hello', keyboardMain); } return bot.sendMessage(chatId, 'error'); }); }Puede crear un acelerador de usuario usando Javascript Map
/* * @param {number} waitTime Seconds to wait */ function throttler(waitTime) { const users = new Map() return (chatId) => { const now = parseInt(Date.now()/1000) const hitTime = users.get(chatId) if (hitTime) { const diff = now - hitTime if (diff < waitTime) { return false } users.set(chatId, now) return true } users.set(chatId, now) return true } }Cómo usarlo: Obtendrá el ID de chat del usuario de la API de Telegram. Puede usar esa identificación como identificador y detener al usuario por un tiempo específico.
Por ejemplo, voy a detener al usuario durante 10 segundos una vez que el usuario lo solicite.
// global 10 second throttler const throttle = throttler(10) // 10 seconds // in your code const allowReply = throttle(chatId) // chatId obtained from telegram if (allowReply) { // reply to user } else { // dont reply }