2

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?

Eron Salling
  • 185
  • 2
  • 5
  • Doesn't Botkit also support the Events API? – MatejMecka May 25 '18 at 09:31
  • @MatejMecka It does, but Slack has changed how presence events work recently - from what I understand I need to subscribe by passing a parameter to the rtm.start method. When I go to api.slack.com/apps there is no option in the event subscriptions for presence. – Eron Salling May 27 '18 at 22:39

1 Answers1

0

I think first we should separate the two concepts: presence_sub and batch_presence_aware.

  1. 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.

  2. batch_presence_aware is a argument that your app can set when calling rtm.start if you'd like presence updates about different users to be grouped into a single message. Each update arrives as a presence_change event. The tradeoff is that your app might not get the presence_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 to rtm.start, which would opt your connection into receiving presence_change events for only the users that were indicated in the last presence_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 a presence_sub message to get any presence_change events.


Now let's go through how you can use this information in Botkit.

  1. Set up your controller to send messages over RTM.

    var controller = Botkit.slackbot({
      // ...
      send_via_rtm: true,
    });
    

    In the starter repo, the controller is created in bot.js and already contains a few options in bot_options. You'll need to ad the send_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 use batch_presence_aware that I'm aware of. If that's something you desire, I recommend opening an issue on the project.

  2. Set up presence subscriptions for the users you want updates regarding.

    controller.say({
      type: "presence_sub",
      ids: [
        "U061F7AUR",
        "W123456"
      ]
    });
    

    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.

  3. Listen for presence change events

    controller.on('presence_change', (bot, event) => {
      // `event` contains presence information
      console.log(event);
    });
    

    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.

Ankur
  • 2,792
  • 3
  • 23
  • 26
  • Thank you so much for your help! I have tried implementing this, however I get a `controller.say is not a function` error when I try to run step 2 above. I also tried `bot.say` incase that's what you meant, which did not work either, with the error message `Cannot use the RTM API to send messages.` Could you clarify what method you are using there? I don't see "say" as a listed method in botkit's documentation. – Eron Salling Jun 07 '18 at 16:11