2

I use Howdy.ai's Botkit for a simple bot application and have it running on node.js on a VPS. Basically, I customized the example for a Slack App from here and am now struggling to keep the bot alive - after some undefined time, the RTM channel to the Slack API get's closed and I can't find a proper way to reconnect. So far I tried

controller.on('rtm_close',function(bot) {
   console.log('** The RTM api just closed. Trying reconnect...');
   // Try a reconnect
   bot.startRTM(function(err) {
      if (!err) {
          trackBot(bot);
      } else {
          console.log('** The RTM api couldn\'t be reopened. It\'s closed now.'); 
      }
   });
});

The trackBot function controls the logging:

function trackBot(bot) {
   _bots[bot.config.token] = bot;
}

It seems I'm missing how the whole approach works. Any help is warmly appreciated!

2 Answers2

7

To enable reconnecting, you need to set the retry config value to true

  // Launch bot
  bot = controller.spawn({
    retry: true,
    token: 'xxx'
  })

https://github.com/howdyai/botkit/blob/master/readme-slack.md#slack-controller

Drazisil
  • 3,070
  • 4
  • 33
  • 53
1

Have you tried using the forever module? https://www.npmjs.com/package/forever

Then run it with forever stop bot.js; forever start bot.js && forever logs bot.js -f

I hope that helps

Ushal Naidoo
  • 2,704
  • 1
  • 24
  • 37
  • Actually, I already had forever running but thought I misconfigured it. As I digged deeper, the issue seems to be related more to Slack's API itself - it closes RTM channels from time to time. But this is not considered as a program shutdown in my code, so forever does not restart. As reconnecting doesn't work either, I had to force program shutdown by using the `process.exit()` statement after a failed reconnect. – Joschka Sondhof Dec 06 '16 at 13:48