Getting Users Stats out of Slack with JQ

Slack has nice Web API Methods (https://api.slack.com/methods) that can be used to get data about your team via a Token. I wanted to use the users.list method (https://api.slack.com/methods/users.list) to get the Total # of Users as well as the currently Active #.

First I got my Web Token from https://api.slack.com/docs/oauth-test-tokens using an Admin account I had created. Note: Normally you would create an App and generate OAuth2 Access Tokens to get this done, but this is juts a quick and dirty implementation of how to get the date

Download JQ

Download the correct version of JQ from https://stedolan.github.io/jq/download/

Total # of Users

To get the total number of users we need to use the following command

[code] curl "https://<slack team>.slack.com/api/users.list?token=<TOKEN>&presence=1" | jq ‘[.members[] | select(.id!="USLACKBOT" and .is_bot==false and .deleted==false)] | length’ [/code]

Which will return the current user count which is all users who are not a bot or have been deleted and that the id is not USLACKBOT (apparently Slackbot is not a bot, go figure)

Total # of Active Users

To get the total number of users we use the above command but extend to check the presence variable is active.

[code] curl "https://<slack team>.slack.com/api/users.list?token=<TOKEN>&presence=1" | jq  ’[.members[] | select(.id!="USLACKBOT" and .is_bot==false and .deleted==false and .presence=="active")] | length’ [/code]

Finally Combined

Theses can then be combined to give

[code] curl "https://<slack team>.slack.com/api/users.list?token=<TOKEN>&presence=1" | jq ‘{active: [.members[] | select(.id!="USLACKBOT" and .is_bot==false and .deleted==false and .presence=="active")] | length, total: [.members[]|select(.id!="USLACKBOT" and .is_bot==false and .deleted==false)]|length}’ [/code]

to produce an output like

[code] { "active": 1, "total": 2

} [/code]

Loading Google+ Comments ...