bot can't reply after several hours
bot can't reply after several hours.but It works if I restart the bot.
Could you post some example code?
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
@peterswimm any ideas
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 I'll check it. thank you ~
@ringtail Did that solve your issue?
@peterswimm I solved it by restarting it when it is down.
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 Instead of increasing the retry count, detecting network changes should be the solution in this case.
@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 ,
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
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 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;
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?
#261 might be helpful.
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);
}
});