Skip to content

Instantly share code, notes, and snippets.

@adamhepton
Last active February 3, 2017 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamhepton/65a52d0c4287e33ca0010a13560e594a to your computer and use it in GitHub Desktop.
Save adamhepton/65a52d0c4287e33ca0010a13560e594a to your computer and use it in GitHub Desktop.
Dirty node.js script that queries the Slack API to find out who has posted most (or least) into a Slack channel of your choice.
{
"name": "channel-stats",
"version": "1.0.0",
"description": "Find out who has posted most into a Slack channel",
"main": "slack.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Adam Hepton <adam@hepton.org>",
"license": "ISC",
"dependencies": {
"console.table": "^0.8.0",
"slack-api": "^0.1.12"
}
}
'use strict';
const Slack = require('slack-api').promisify();
require('console.table');
const options = {
token: "<your valid slack api token>"
};
var messagesByUser = {};
const getChannel = (channelName) => {
return Slack.channels.list(options).then( (data) => {
return data.channels.filter( (channel) => {
return channel.name === channelName;
}).map( (channel) => {
return channel.id;
});
});
};
const getMessagesInChannel = (channel, latest) => {
return getMessagesBatch(channel, latest).then(countMessagesPerUserInBatch).then(getNextBatch);
};
const getMessagesBatch = (channel, latest) => {
console.log('batch requested for ts:', latest || "default");
options.channel = channel;
if(latest !== null) {
options.latest = latest;
}
return Slack.channels.history(options).then( (data) => {
return {
channel: channel,
has_more: data.has_more,
messages: data.messages.filter( (msg) => {
return msg.hasOwnProperty('user') && !msg.hasOwnProperty('subtype');
})
};
});
};
const getNextBatch = (batch) => {
if(batch.has_more) {
let latest = batch.messages.pop().ts;
return getMessagesInChannel(batch.channel, latest);
}
return batch;
};
const countMessagesPerUserInBatch = (batch) => {
messagesByUser = batch.messages
.map( (msg) => {
return msg.user;
})
.reduce( (authors, value) => {
if(authors.hasOwnProperty(value)) {
authors[value]++;
} else {
authors[value] = 1;
}
return authors;
}, messagesByUser);
return batch;
};
const getActiveUserInformation = () => {
return Slack.users.list({ token: options.token }).then( (data) => {
return data.members.filter( (member) => {
return messagesByUser.hasOwnProperty(member.id) && !member.is_bot;
})
.map( (member) => {
return {
name: member.real_name,
username: member.name,
messages: messagesByUser[member.id]
}
});
});
};
const sortByNumberOfMessages = (authors) => {
let keys = Object.keys(authors).sort( (a, b) => authors[b].messages - authors[a].messages );
let sortedAuthors = [];
keys.forEach( (k) => {
sortedAuthors.push(authors[k]);
});
return sortedAuthors;
};
const displaySummary = (data) => {
console.table(data);
};
/////////////
getChannel('<the channel to query>')
.then(getMessagesInChannel)
.then(getActiveUserInformation)
.then(sortByNumberOfMessages)
.then(displaySummary)
.catch(Slack.errors.SlackError, function (error) {
console.log('Slack did not like what you did: ' + error.message);
});
@adamhepton
Copy link
Author

Sample output:

name              username        messages
----------------  --------------  --------
Zack Brown        captainredmuff  1563
das scawp         scawp           1287
Prox o'Moron      prox            1023
Steven Jefferson  stejay          316
Al Paterson       acp85           203
Andy Humphreys    rink            1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment