Slack Plugin - active users list

Here is a crude plugin to get the list of active users on a Slack organization.
It needs a token that must be generated by an admin on slack API site, and assigned the appropriate permissions. It returns an array list of all users marked as Active.
To minimize network traffic the list of users is only loaded on startup. If more people join the organization you just need to restart the app.
(I’m NOT a js programmer so I’m sure there are a lot of things that could be done better)

plugin.Name = "Slack";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 60000;
plugin.DefaultSettings = {};

var http = new HTTPClient();
var users ={};
  
function onChangeRequest(device, attribute, value) {
}

function onConnect() {
    var url = "https://slack.com/api/users.list?token="+ plugin.Settings["Token"];
    var response = http.get(url);
    var json = response.data.members; 
    json.forEach(function(obj)  { users[obj.id] = obj.name; });     
}

function onDisconnect() {
    console.log("slack plugin disconnected");
}

function onPoll() {
   var status = [];
    for (var key in users) {
        var url = "https://slack.com/api/users.getPresence?token="+ plugin.Settings["Token"]+"&user="+key;
        var response = http.get(url);
        var json = response.data.presence; 
        if (json == 'active')
         	status.push({user : users[key]});
    
    }
    var mainDevice = plugin.Devices["slack"];
    mainDevice.output  = status;              
}

function onSynchronizeDevices() {
}
1 Like