Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

bot can't reply after several hours #276

Closed
ringtail opened this issue Jun 13, 2016 · 16 comments
Closed

bot can't reply after several hours #276

ringtail opened this issue Jun 13, 2016 · 16 comments

Comments

@ringtail
Copy link

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

@peterswimm
Copy link
Contributor

Could you post some example code?

@ringtail
Copy link
Author


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
Copy link
Author

@peterswimm any ideas

@spywhere
Copy link

spywhere commented Jun 20, 2016

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.

@ringtail
Copy link
Author

@spywhere I'll check it. thank you ~

@peterswimm
Copy link
Contributor

@ringtail Did that solve your issue?

@ringtail
Copy link
Author

ringtail commented Jul 2, 2016

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

@YemSalat
Copy link

YemSalat commented Jul 2, 2016

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..

@anonrig
Copy link
Contributor

anonrig commented Jul 2, 2016

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

@YemSalat
Copy link

YemSalat commented Jul 2, 2016

@anonrig I borrowed the solution from here: #104

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

@mttrs
Copy link

mttrs commented Jul 8, 2016

@anonrig
Copy link
Contributor

anonrig commented Jul 8, 2016

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.

@itamarro
Copy link
Contributor

@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;

@Mr-Wallet
Copy link

Mr-Wallet commented Aug 16, 2016

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?

@garymoon
Copy link

#261 might be helpful.

@inn0v8
Copy link

inn0v8 commented Dec 8, 2016

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.

#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);
}
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests