1

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?

pete
  • 125
  • 2
  • 12

3 Answers3

2

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
...
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Are you sure the bot token you are using isn't running anywhere else? Try getting a new bot (token) from @botFather to check this. Since your code does show me the error, but my code doesn't I'm not sure where the problem may exist. – 0stone0 Mar 26 '21 at 15:10
  • I created a new bot called test888888bot to test it – pete Mar 26 '21 at 15:11
  • Are there any code blocks not included in the OP? Can't think of anything why this won't work. – 0stone0 Mar 26 '21 at 15:41
  • here is a link with the picture of the error:https://ibb.co/G9437MX – pete Mar 26 '21 at 16:34
1

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:

const { fork } = require('child_process');
const controller = new AbortController();
const child = fork("your_script.js", { controller });


setTimeout(function()
{
    controller.abort(); // Stops the child process
}, 10000);

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.

John
  • 5,942
  • 3
  • 42
  • 79
-1

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:

*/10 * * * * pm restart 0

And now my bot is restarted every 10 minutes.

pete
  • 125
  • 2
  • 12