1

I wrote a simple telegram bot that after inputting a command it displays some stats. It works flawlessly except when it's being fed the same command more than once (different users using the command). Right now looks something like this:

me: /hello
bot: "hey"
me: /hello
user2: /hello
user3: /hello
bot: "hey"
bot: "hey"
bot: "hey"

The appropiate would be to just display one message if it's the same as the previous one.

me: /hello
bot: "hey"
me: /hello
user2: /hello
user3: /hello
bot: "hey"

I'm using node-telegram-bot-api with a polling approach (I'll move it later to use webhooks).

I'm only using the bot.onText/bot.sendMessage functions from the telegram api.

bot.onText(/^\/test$/, async function onTestText(msg) {
    bot.sendMessage(msg.chat.id, 'hey', {parse_mode: 'Markdown'});
});

TLDR: if bot receives the same command more than once, it will reply with a message X amount of times. I need to avoid this.

1 Answers1

0

maybe you need to implement a global lock inside your code, which will lock executing the code until. unlocking the code

let lock= false ;
bot.onText(/^\/test$/, async function onTestText(msg) {
 if(!lock){
  lock = true ; 
  await bot.sendMessage(msg.chat.id, 'hey', {parse_mode: 'Markdown'});
  lock = false ;
 }
});
Mohammed naji
  • 983
  • 1
  • 11
  • 23