Search code examples
slack-apislackbotkit

Slack + Botkit: Automatic reconnect after RTM closure


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!


Solution

  • 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