Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent the bot auto disconnect? #140

Open
KoichaDev opened this issue Oct 20, 2019 · 15 comments
Open

How to prevent the bot auto disconnect? #140

KoichaDev opened this issue Oct 20, 2019 · 15 comments

Comments

@KoichaDev
Copy link

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?

@ammanueladdis
Copy link

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.

@KoichaDev
Copy link
Author

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

@ammanueladdis
Copy link

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.

@KoichaDev
Copy link
Author

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.

@ammanueladdis
Copy link

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

Seems like this might be the same issue we are having reported earlier
#24

@KoichaDev
Copy link
Author

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

@mkt1988
Copy link

mkt1988 commented Nov 17, 2019

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?

@7c
Copy link

7c commented Nov 25, 2019

same issues here. i keep restarting stuff

@sathiya-mit
Copy link

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

@alexey-dc
Copy link

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" }');

@AleksVAnd
Copy link

AleksVAnd commented Jan 22, 2020

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.

@alexey-dc
Copy link

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.

@dorman99
Copy link

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.

@Clotonervo
Copy link

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

@rodolfobandeira
Copy link

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

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

No branches or pull requests

10 participants