user4383363
user4383363

Reputation:

Detect when a user logged in or out

I need to know how to detect when a user has logged in or out in a team in Slack.

I'm building my first Slack app that consists of storing user's status in the database, and I need a guidance to know how to detect when a user logged in and out, to automatically change their status.

I'm developing with Node.js and hosting it on Heroku.

Edit

I've installed @slack/client module and this is how I tried to manage when a user logs in or out:

rtm.start();
rtm.on(RTM_EVENTS.PRESENCE_CHANGE, function(x) {
    db.users.insert({
        userid: x.user,
        status: x.presence === 'active' ? 'HERE' : 'OFF'
    });
});

But it doesn't detect when I close the browser tab, just when I stay idle for 30 minutes and when I refresh the page or log in.

Upvotes: 0

Views: 1592

Answers (1)

user94559
user94559

Reputation: 60133

I believe you'll see the presence_change event via the RTM API. Someone will be set to "away" when they log out (or manually set themselves to away) and "active" when they log in (or manually set themselves back to active).

EDIT

Here's my code:

var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;

var token = '<REDACTED>';

var rtm = new RtmClient(token);

rtm.on(RTM_EVENTS.PRESENCE_CHANGE, function (event) {
    console.log(event.user + ': ' + event.presence);
});

rtm.start();

And the output when I opened a tab to that Slack team and then closed it:

U11C8V57T: active
U11C8V57T: away

Upvotes: 2

Related Questions