skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  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
    ...
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search