多条消息发送nodejs的电报机器人链 - 崩溃

发布于 2025-02-07 08:33:05 字数 946 浏览 1 评论 0原文

我试图通过节点Backend中的Telegraf API发送多个消息到Telegram Bot。有多种条件,如果是真实的,则应发送电报警报。条件的示例是:

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

大约有100个条件在数据中以从API获取流程时在几秒钟内被检查。因此,当尝试在一秒钟内处理30多条消息时,电报机器人限制会击中,并且服务器崩溃。

用于从机器人发送消息的代码是:

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

我还尝试在上述代码中使用settimeout()在上面的代码中放置一个延迟:

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

上面的代码将延迟(我认为是这样),但由于大约30-50条件,立刻一秒钟。该函数被调用多次,大约在大约之后。 10-15秒,所有警报都会同时发送,再次击中电报机器人限制。

另外,尝试这样的尝试:

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

这会导致直接错误。

请帮助我解决这个问题。我必须在一段时间内发送多个警报。同时提醒的数量可能会增加。因此,向我推荐使用JavaScript最好的解决方案。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2025-02-14 08:33:05

您可以通过在队列/数组中添加消息来做到这一点,然后使用异步等待它来循环

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文