slack-bot-api icon indicating copy to clipboard operation
slack-bot-api copied to clipboard

How to prevent the bot auto disconnect?

Open KoichaDev opened this issue 5 years ago • 15 comments

I have been very successful to use this API to create a bot. The only downside is the bot is auto disconnecting after 6 hours or so on my ubuntu server. I have used pm2 and screen to make sure to run 24/7, but it's not working.

Does anyone know how to prevent the bot from auto disconnecting?

KoichaDev avatar Oct 20 '19 13:10 KoichaDev

Also running into this issue.

No error messages in Node - app is still running fine but all of a sudden after 5-6 hours it is not responding to user input from Slack.

ammanueladdis avatar Nov 03 '19 23:11 ammanueladdis

How did you solve app is still running fine? Did you changed the original creator code of his API?

KoichaDev avatar Nov 03 '19 23:11 KoichaDev

Sorry if I was unclear.

I am having the exact same issue you are having.

Running my slackbot on Ubuntu with PM2 but it disconnects after 5 to 6 hours or so. PM2 isn't at fault since this happens without that.

I don't get error messages in node and it appears the connection to Slack is absolutely fine - however it stops responding to peoples messages from Slack.

ammanueladdis avatar Nov 03 '19 23:11 ammanueladdis

The only way I can see to not fixing the x but the y is using crontab and making sure the bot will turn off and turn on again after 2 hours or something.

KoichaDev avatar Nov 04 '19 00:11 KoichaDev

Funnily enough that's the exact solution my colleague just had.

Seems like this might be the same issue we are having reported earlier https://github.com/mishk0/slack-bot-api/issues/24

ammanueladdis avatar Nov 04 '19 00:11 ammanueladdis

Exactly! Seems the original creator of this API left the project with some bugs :(

KoichaDev avatar Nov 04 '19 00:11 KoichaDev

Based on official docs and other sources I think we need to "ping" in regular intervals...

https://api.slack.com/rtm

Clients should try to quickly detect disconnections, even in idle periods, so that users can easily tell the difference between being disconnected and everyone being quiet. Not all web browsers support the WebSocket ping spec, so the RTM protocol also supports ping/pong messages. When there is no other activity clients should send a ping every few seconds. To send a ping, send the following JSON:

{ "id": 1234, // ID, see "sending messages" above "type": "ping", ... }

But how do we do it using slackbots?

mkt1988 avatar Nov 17 '19 02:11 mkt1988

same issues here. i keep restarting stuff

7c avatar Nov 25 '19 12:11 7c

I am also facing the same issue. Bot getting auto disconnected. Any solution other than restart? Please advice.

sathiya-mit avatar Dec 09 '19 06:12 sathiya-mit

It appears slackbots already does the ping that @mkt1988 suggested?

    /**
     * Establish a WebSocket connection
     */
     connect() {
         this.ws = new WebSocket(this.wsUrl);

         setWsHeartbeat(this.ws, '{ "kind": "ping" }');

alexey-dc avatar Jan 13 '20 09:01 alexey-dc

I have been trying to fix this in the past few days. I'm close but not there yet - maybe we can figure this out together!

First I created an Incoming WebHook (part 3 of this article) : https://api.slack.com/messaging/webhooks

Then I installed this simplified version of the https module :https://www.npmjs.com/package/request#custom-http-headers

And I sent a POST request by following the ping/pong format as pointed out by @mkt1988. I do receive a response but it is 400 Bad request. (even though the app is receiving responses that doesn't prevent disconnection unfortunately)

I tried all sorts of formatting but was still getting 400 res. I even found out the following : https://stackoverflow.com/questions/44334680/cant-post-from-app-to-slack-incoming-webhook

So I turned to the https lib because that's the only way I can control what headers are sent : let num=Math.floor((Math.random()*1000)); let data=JSON.stringify({ "id": num,
"type": "ping" }); console.log(data); const options={ 'protocol': 'https:', 'host': 'hooks.slack.com', 'port': 443, 'path': '/services/"team"/"channel"/"web hook"', 'method': 'POST', 'headers': {'content-length': data.length} }; const req=https.request(options,res=>{ console.log(statusCode: ${res.statusCode}); console.log(res); res.on('data', d=>{ process.stdout.write(d) }) }); req.on('error', error=>{ console.error(error) }) req.write(data); req.end()

But I'm still getting 400 bad request.

I went back to the application settings and I tried the curl. Guess what - even with the example curl I still get a bad request!! (and the curl is incorrect because the header needs to be in double quotes, not single).

Any suggestions?

Thank you.

AleksVAnd avatar Jan 22 '20 07:01 AleksVAnd

Ok so looks like the hangup is not coming from the WebSocket layer, but the layer on top of that.

The WebSocket layer has a ping mechanism, and then Slack also wants a separate ping. So in this issue, the ping is correctly done for WebSockets, but not for Slack.

The solution I'm going for myself is - instead of trying to send a message of type ping to slack - which is supported it seems - literally have my bot post the messages that I want to use him for every 4 hours or so. I'm using it for analytics, and no one is going to complain that we get analytics in slack every 4 hours.

For a more silent bot, I think the slack-bot-api package doesn't provide a way to just do a ping message to slack, so I don't know that there's a path forward there w/o submitting a PR to the npm package to support both pinging and messaging.

alexey-dc avatar Jan 28 '20 04:01 alexey-dc

Ok so looks like the hangup is not coming from the WebSocket layer, but the layer on top of that.

The WebSocket layer has a ping mechanism, and then Slack also wants a separate ping. So in this issue, the ping is correctly done for WebSockets, but not for Slack.

The solution I'm going for myself is - instead of trying to send a message of type ping to slack - which is supported it seems - literally have my bot post the messages that I want to use him for every 4 hours or so. I'm using it for analytics, and no one is going to complain that we get analytics in slack every 4 hours.

For a more silent bot, I think the slack-bot-api package doesn't provide a way to just do a ping message to slack, so I don't know that there's a path forward there w/o submitting a PR to the npm package to support both pinging and messaging.

i using this kindda approach too. but it still not working. the bot keep sending me the response based on what interval did i give, but when i try to send a message, it wasnt listening.

dorman99 avatar Feb 29 '20 15:02 dorman99

I don't know if this is still relevant, but I fixed this problem by adding to the source code a reconnect method. I was told by Slack Help Center that this is normal, and that simply what you need to do is call a method on the api to tell Slack that your app is still running. The reconnect method simply calls the method on the real time messaging api (rtm.connect) which to my understanding sends over a new url to set as your web socket. My pull request didn't go through due to some weird error, and it doesn't seem like anyone is still keeping tabs on this repository. For more clarification take a look at the slack-api documentation here: https://api.slack.com/methods/rtm.connect

Clotonervo avatar Apr 16 '20 20:04 Clotonervo

Another solution, could be to add the bot in to a systemd service and just restart it every X hours. An example of how to do this here

rodolfobandeira avatar Jul 07 '21 13:07 rodolfobandeira