Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Antispam for telegram bot

I am working on creating a telegram bot, I want to make an anti-spam system, that is, when a person presses a button too many times, the bot will freeze for him for a certain number of seconds, it is possible to write a message about blocking. I just started learning JavaScript. I use 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');
    });
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create a user throttler using 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
  }
}

How to use: You'll get the user's chatId from telegram api. You can use that id as an identifier and stop the user for given specific time.

For instance I'm gonna stop the user for 10seconds once the user requests.

// 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
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!