简体   繁体   English

有一段时间重复发送消息的电报机器人?

[英]Telegram bot which repeats send message with some time?

ClearInterval don't work or work but I make a mistake. ClearInterval 不工作或工作,但我犯了一个错误。 I don't know but when I use /stop it continue write 'Sending'.我不知道,但是当我使用 /stop 时,它会继续写“发送”。 How to resolve such problem.如何解决这样的问题。

 bot.hears(/\/send|\/stop/, ctx=> { let sending = setInterval(() => { if (/\/send/.test(ctx.update.message.text)) { ctx.reply('Sending:'); } else if (/\/stop/.test(ctx.update.message.text)){ ctx.reply('stopping;'); clearInterval(sending), } }; 10000); });

The main problem is you're creating new intervals every time you send /send or /stop .主要问题是您每次发送/send/stop时都会创建新的间隔。 So, your intervals get created multiple times generating multiple intervals in parallel.因此,您的间隔被创建多次,并行生成多个间隔。

Something like this should work:像这样的东西应该工作:

let sendInterval;
bot.hears(/\/send|\/stop/, ctx => {
  if (sendInterval) {
    clearInterval(sendInterval);
  }

  if (/\/send/.test(ctx.update.message.text)) {
    sendInterval = setInterval(() => {
      ctx.reply('Sending');
    }, 10000);
  } else if (/\/stop/.test(ctx.update.message.text)) {
    ctx.reply('stopping!');
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 电报bot API,通过@username发送消息 - Telegram bot api, send message via @username 在链接到谷歌电子表格的电报机器人中发送预定消息 - Send a scheduled message in my telegram bot linked to a google spreadsheet 向电报机器人发送消息 - sending message to telegram bot 我们如何阅读某些用户发送给我们的机器人的消息? - How Do we read the message which is send to our bot by some user? 电报机器人不显示时间 - telegram bot not showing time 通过电报机器人创建和发送SVG - Creat and send SVG by telegram bot 如何使 discord js 机器人在某个时间在 discord 的确切文本通道中发送随机消息(我创建一个列表并发送它) - How to make an discord js bot to send an random message(i make an list and he sends it) at some time in an exact text channel from discord 如何使用 Telegraf.js Node.js 在 Telegram Bot 中发送私人消息 - How to send private message in Telegram Bot using Telegraf.js Node.js 如何只向与我的电报机器人互动的每个用户发送一次消息 - How to send message only once to every individual user who has engaged with my telegram bot 为什么电报机器人每次向频道发送照片的次数与按下上传照片按钮的次数一样多? - Why does a telegram bot every time send a photo to channel as many times as the Upload Photo button was pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM