I’m trying to restart my bot every 10 seconds(for testing I want to do it every 10 minutes or so) or so and I can’t seem to be able to do it.
I’ve tried:
const TeleBot = require('telebot');
const bot = new TeleBot({
token:'mytoken',
polling:true
});
setInterval(function(){ bot.start()},9000);
setInterval(function(){ bot.stop()},10000);
It says that the bot starts and stops, however when it does it the second time it says:
[bot.error.update] {
ok: false,
error_code: 409,
description: ‘Conflict: terminated by other getUpdates request; make sure that only one bot instance is running’
}
But if the bot is stopped there should only be no instance of the bot running.
Is there a way to completely stop the bot?
3
Answers
Thanks everyone who tried to help.
But I've came up with a hacky solution.
I moved my files from heroku to a vps, then I installed pm2 and set it to run node index.js(my main js file), then I installed cron and set it to restart pm2 every 10 minutes by typing crontab -e and typed the following:
And now my bot is restarted every 10 minutes.
Very interesting question, I guess the issue is caused by
setInterval
‘s inaccuracy wich will cause thosesetInterval
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;Using this approach, my bot has been turning itself off and on for about
2560 minutes without any issues:To ensure that the bot has stopped and nothing is lingering.
You can put it in its own node script and then run it with:
From your master or "cron job" script".
Keep in mind, that this will kill the process so there won’t be any chance for it to do any cleanups so if you want to kill the process softly first. Pipe a message to the child process first so that it can try to terminate on its own.