botkit icon indicating copy to clipboard operation
botkit copied to clipboard

bot can't reply after several hours

Open ringtail opened this issue 10 years ago • 16 comments

bot can't reply after several hours.but It works if I restart the bot.

ringtail avatar Jun 13 '16 14:06 ringtail

Could you post some example code?

peterswimm avatar Jun 13 '16 14:06 peterswimm


var Botkit = require('botkit');
var tuling = require('./tuling')

var entry = {
  run:run
}

function run(){
  var controller = Botkit.slackbot({
    debug: process.env.debug || false
    //include "log: false" to disable logging
    //or a "logLevel" integer from 0 to 7 to adjust logging verbosity
  });

  controller.spawn({
    token: process.env.token,
  }).startRTM()

  controller.on('direct_message',function(bot,message){
    var body = tuling.reply(message.text);
    bodyJson = JSON.parse(body)
    bot.reply(message,bodyJson.text);
  })
}

module.exports = entry

ringtail avatar Jun 14 '16 08:06 ringtail

@peterswimm any ideas

ringtail avatar Jun 15 '16 03:06 ringtail

I also has this issue, but it's from the internet connection lost (even a few minutes). My solution is try to check if the process can access the internet periodically, if the internet is lost, I'll try to restart the bot.

spywhere avatar Jun 20 '16 05:06 spywhere

@spywhere I'll check it. thank you ~

ringtail avatar Jun 20 '16 15:06 ringtail

@ringtail Did that solve your issue?

peterswimm avatar Jul 01 '16 16:07 peterswimm

@peterswimm I solved it by restarting it when it is down.

ringtail avatar Jul 02 '16 01:07 ringtail

You can actually specify the number of retries when you spawn your bot (how many times it will try to reconnect if it loses current connection) I usually set mine to something like 500 or 1000

controller.spawn({
    token: process.env.token,
    retries: 500
  }).startRTM()

There should be better documentation on this..

YemSalat avatar Jul 02 '16 01:07 YemSalat

@YemSalat Instead of increasing the retry count, detecting network changes should be the solution in this case.

anonrig avatar Jul 02 '16 12:07 anonrig

@anonrig I borrowed the solution from here: https://github.com/howdyai/botkit/issues/104

Why do you think this case can not be solved by specifying the number of retries?

YemSalat avatar Jul 02 '16 13:07 YemSalat

@YemSalat ,

The config property name of retries should be retry, I think.

code https://github.com/howdyai/botkit/blob/master/lib/Slackbot_worker.js#L24

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

mttrs avatar Jul 08 '16 08:07 mttrs

In some cases, using a finite number of tries, will not be possible. Because one can't determine the amount of time it requires to try to reconnect, because this problem is not based on the amount of tries but based on the change of the network adapter. Therefore, I suggest detecting the network changes and act upon if it is connected.

anonrig avatar Jul 08 '16 12:07 anonrig

@anonrig Note that you can use Infinity for the value of 'retry'. (Indeed it's 'retry', not 'retries').

From Slackbot_worker.js:

// config.retry, can be Infinity too
var retryEnabled = bot.config.retry ? true : false;
var maxRetry = isNaN(bot.config.retry) || bot.config.retry <= 0 ? 3 : bot.config.retry;

itamarro avatar Jul 10 '16 04:07 itamarro

My bots randomly all disconnect at the exact same time - this happens even when the bots are split across two different boxes running different OSes (albeit on the same network). These disconnects occur every 1-3 days, almost always overnight, and may be tied to slack server maintenance windows for all I know.

@anonrig If the preferred behavior is to respond to network changes, can you provide a simple working example of a bot reconnecting itself?

Mr-Wallet avatar Aug 16 '16 21:08 Mr-Wallet

#261 might be helpful.

garymoon avatar Nov 12 '16 04:11 garymoon

Was noticing the same problem and realized slack was closing the rtm connection or the overall connection had dropped. There is a bug in how the retry attributes are defined and used which causes the retry logic to fail.

https://github.com/howdyai/botkit/pull/532

Reconnect has been working flawlessly since. I also use this code for slack notifications on RTM close and reconnect failures. Hope this helps.

console.log('\n\n*** '+moment().tz('America/Los_Angeles').format()+' ** The RTM api just closed');

try {
controller.findTeamById('<SLACK_TEAM_ID_HERE>', function(err, team) {
var alertBot = controller.spawn(team);
alertBot.api.chat.postMessage(
{
channel : '<SLACK_CHANNEL_ID_HERE>',
text: 'GUYS!!! I just got an RTM Close event trigger for this team --> '+bot.team_info.id+' '+bot.team_info.name+' .',
attachments: [],
}, function (err, response) {
});
});
} catch (err) {
console.log('\n\n*** '+moment().tz('America/Los_Angeles').format()+' Tried to send alert for an rtm_close event but failed with this error: '+err);
}
});

controller.on('rtm_reconnect_failed',function(bot) {
console.log('\n\n*** '+moment().tz('America/Los_Angeles').format()+' ** Unable to automatically reconnect to rtm after a closed conection.');

try {
controller.findTeamById('<SLACK_TEAM_ID_HERE>', function(err, team) {
var alertBot = controller.spawn(team);
alertBot.api.chat.postMessage(
{
channel : '<SLACK_CHANNEL_ID_HERE>',
text: 'GUYS!!! I just got an RTM Reconnect Failed trigger for this team --> '+bot.team_info.id+' name: '+bot.team_info.name+'. The team either uninstalled the app or there was an issue which requires a resart of the bot.',
attachments: [],
}, function (err, response) {
});
});
} catch (err) {
console.log('\n\n*** '+moment().tz('America/Los_Angeles').format()+' Tried to send alert for an rtm_reconnect_failed event but failed with this error: '+err);
}
});

inn0v8 avatar Dec 08 '16 01:12 inn0v8