Very interesting question, I guess the issue is caused by setInterval
's inaccuracy wich will cause those setInterval
to be out of sync. (So, for example, you'll try to start the bot, while it's already active)
The Telebot documentation shows us there are some TeleBot Events we can use to ensure we're only calling start()
/ stop()
when we want;
const TeleBot = require('telebot');
const bot = new TeleBot({
token:'********:AAEibBwftjTEKuYd9d2X0ACeyyzTk4DLe60',
polling:true
});
// Create event listener for bot-stop
bot.on('stop', (data) => {
// After 5 seconds, START the bot
setTimeout(function(){
bot.start()
}, 5 * 1000);
});
// Create event listener for bot-start
bot.on('start', (data) => {
// After 10 seconds, STOP the bot
setTimeout(function(){
bot.stop('Interval stop()')
}, 10 * 1000);
});
// Start for first time
bot.start()
Using this approach, my bot has been turning itself off and on for about 25 60 minutes without any issues:
MBP ➜ telebot node startEnStop.js
[bot.plugin] loaded 'regExpMessage' plugin
[bot.plugin] loaded 'shortReply' plugin
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
[bot.info] bot stopped : Interval stop()
[bot.info] bot started
...