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

427
Views
Telegram Bot Chain of Multiple Messages Sending NodeJS - Crashing

I am trying to send multiple messages to telegram bot through telegraf API in node backend. There are various conditions which when true, should send an telegram alert. The example of condition is:

if(condition is true) {
  message = "Hey there";
  sendToTGBot(message)
} 

There are approximately 100 conditions which get checked within few seconds when the data is fetched as a stream from the API. So, the telegram bot limit gets hit when trying to process more than 30 messages in a second and the server crashes.

The code used to send messages from the bot is:

export const sendToTGBot = (alert) => {
   bot.telegram.sendMessage(chat_id, alert);
}

I have also tried putting a delay using setTimeout() in the above code as:

export const sendToTGBot = (alert) => {
   setTimeout(() => {
   bot.telegram.sendMessage(chat_id, alert);
   }, 500);
}

This code above puts the delay (I think so), but due to approximately 30-50 conditions getting true at once in a second. The function gets called that many times and after approx. 10-15 seconds all the alerts get sent simultaneously, again hitting the telegram bot limit.

Also, tried it like this:

export const sendToTGBot = 
   setTimeout((alert) => {
   bot.telegram.sendMessage(chat_id, alert);
   }, 500);

This results in a straight error.

Kindly help me with this problem. I have to send multiple alerts within a duration. The number of alerts simultaneously can increase. So, recommend me the best possible solution using JavaScript. Thanks.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can do this by adding the messages in a queue/array, then utilize async await to loop it

let queue = [];

if(condition == true) {
  message = "Hey there";
  queue.push(message);
} 

setInterval(async ()=>{
  if(queue.length>0) {
    await bot.telegram.sendMessage(chat_id, queue[0]);
    queue.shift();
  }
}, 1000)
about 4 years ago · Santiago Gelvez 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!