The Slack RTM(Real Time Messaging) API is especially useful when I want to receive a notification in real time when a user changes presence status, or others events are emitted. So, instead of long polling the API, and check if status changes say every 5 minutes, my app gets notified when an actual event happens. It may sound like a webhook; however, it is different in the sense that RTM API and clients leverage WebSockets, so the client keeps the WebSocket connection open.
This tutorial leverages Node Slack SDK. This repo provides some examples of how the SDK works, however, it lacks specific examples such as when a user changes presence status.
First of all, to get access to the RTM API events, you'll need a Slack access token that enables you to capture events happening in real time. The following explains how to create and retrieve API tokens: https://get.slack.help/hc/en-us/articles/215770388-Creating-and-regenerating-API-tokens
git clone https://github.com/dzuluaga/slack-rtm-users-presence-tutorial.git
var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var token = process.env.SLACK_API_TOKEN || '';
var rtm = new RtmClient(token);
//var rtm = new RtmClient(token, {logLevel: 'debug'});
rtm.on(RTM_EVENTS.PRESENCE_CHANGE, function (event) {
//presence changed either by the user or by the client when the user becomes idle
console.log(event.user + ': ' + event.presence);
});
rtm.on(RTM_EVENTS.MANUAL_PRESENCE_CHANGE, function (event) {
// presence manually changed by the user
console.log('manual presence change: ' + JSON.stringify(event) + ': ' + event.presence);
});
rtm.start();
$ SLACK_API_TOKEN={YOUR_TOKEN_HERE} nodemon app.js
This list of events comes handy to find other events supported by RTM API.