I am trying to figure out how to subscribe to presence changes in Slack via Botkit's startRTM call. Currently I'm using Botkit's starter slack bot which has an rtm_manager script for handling all rtm starts and closes.
bot.startRTM(function(err, bot) {
...do stuff
});
I've looked into Slack's new way of subscribing to presence events, and understand that I need to send a presence_sub or batch_presence_aware parameter in order to subscribe to the presence_change event.
I've also looked at the documentation for the node-slack-sdk rtm client for handling presence updates, which uses rtm.start itself
rtm.start({
batch_presence_aware: true
});
Since I am using botkit, which uses a startRTM method, I am struggling to understand how to combine all of this information. Is there any documentation or examples for subscribing to presence updates while using botkit's startRTM method?
I think first we should separate the two concepts:
presence_sub
andbatch_presence_aware
.presence_sub
is an RTM message type which your app can send to Slack to indicate for which users your client would like presence updates regarding.batch_presence_aware
is a argument that your app can set when callingrtm.start
if you'd like presence updates about different users to be grouped into a single message. Each update arrives as apresence_change
event. The tradeoff is that your app might not get thepresence_change
as soon as possible, but it would be dealing with fewer messages which can benefit performance. This is optional but recommended.The "new way" you are referring to is a change in behavior since January 2018. Previous to the change, your app could have supplied a
presence_sub
argument tortm.start
, which would opt your connection into receivingpresence_change
events for only the users that were indicated in the lastpresence_sub
message; otherwise your app would receive those updates for every user in the workspace. Since the change,presence_sub=true
for all connections, which effectively means your app must send apresence_sub
message to get anypresence_change
events.Now let's go through how you can use this information in Botkit.
Set up your controller to send messages over RTM.
In the starter repo, the controller is created in
bot.js
and already contains a few options inbot_options
. You'll need to ad thesend_via_rtm
option to allow for your sent messages to go over RTM. Unfortunately, Botkit doesn't have an API that will allow you to usebatch_presence_aware
that I'm aware of. If that's something you desire, I recommend opening an issue on the project.Set up presence subscriptions for the users you want updates regarding.
You'll want to do this as early in your program as possible. In the starter repo, you might want to group this and the next step into a "skill", so placing it in a new file in that directory, inside the exported function would be a good option. Notice that you'll need a list of the user IDs for whom you want subscriptions. It's up to you how you'd like to source that list. You also want to perform this same action, with a complete list of the users, each time you want to add or remove a subscription.
Listen for presence change events
Once a user for which you have set up a presence subscription changes presence status, you'll receive an event in this callback. The exact structure is described here: https://api.slack.com/events/presence_change.