2

I have created a bot using Botkit and wit.ai. It runs perfectly fine. However, after a certain period of time, I start getting multiple messages from the bot. After I restart the node.js service, it starts working normally.

Here's the code I have written,

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

 const controller = Botkit.slackbot({
     json_file_store: './db_slackbutton_bot/',
     logger: new winston.Logger({
       transports: [
        new (winston.transports.Console)(),
        new (winston.transports.File)({ filename: './bot.log' })
       ]
      }),
     debug: true
 })
 .configureSlackApp({
   clientId: config.slack.clientId,
   clientSecret: config.slack.clientSecret,
   scopes: ['bot'],
 });

 // Handle events related to the websocket connection to Slack

controller.on('rtm_open',function(bot) {
  console.log('** The RTM api just connected!');
});

controller.on('rtm_close',function(bot) {
  // you may want to attempt to re-open
  console.log('** The RTM api just closed');
  bot.startRTM(function(err) {
    if (!err) {
      trackBot(bot);
    }
  });
});



/*
  code for starting RTM
*/
controller.storage.teams.all(function(err,teams) {
  if (err) {
    throw new Error(err);
  }

  // connect all teams   with bots up to slack!
  for (var t  in teams) {
    if (teams[t].bot) {
      controller.spawn(teams[t]).startRTM(function(err, bot) {
        if (err) {
          console.log('Error connecting bot to Slack:',err);
        } else {
          trackBot(bot);
        }
      });
    }
  }
});

I am not able to figure out what is the issue in the code. These are few errors in the error log,

{"level":"error","message":"** BOT ID: after5 ...reconnect failed after #4 attempts and 8050ms","timestamp":"2016-08-26T10:53:19.175Z"}
{"level":"error","message":"Abnormal websocket close event, attempting to reconnect","timestamp":"2016-08-27T03:08:30.972Z"}

and the screenshot of the messages,

enter image description here

Kishan Rajdev
  • 1,919
  • 3
  • 18
  • 24

0 Answers0