Skip to content

Instantly share code, notes, and snippets.

@realityking
Created September 4, 2017 18:42
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 realityking/8ac1fbba06544d835f62649cdd7e05a1 to your computer and use it in GitHub Desktop.
Save realityking/8ac1fbba06544d835f62649cdd7e05a1 to your computer and use it in GitHub Desktop.
Count slack messages by bot in a channel
'use strict';
const WebClient = require('@slack/client').WebClient;
const TOKEN = process.env.SLACK_TOKEN;
const CHANNEL_NAME = 'cofftentful';
const BOT_ID = 'B4XNQQK3M';
const web = new WebClient(TOKEN);
function getHistory(channelId) {
const messages = [];
function gatherMessages(info) {
messages.push(...info.messages);
if (info.has_more) {
const lastetTs = info.messages[info.messages.length - 1].ts;
console.log(lastetTs);
return web.channels.history(channelId, {latest: lastetTs})
.then(gatherMessages)
} else {
return messages;
}
}
return web.channels.history(channelId)
.then(gatherMessages)
}
web.channels.list()
.then(info => info.channels)
.then(channels => {
for (const channel of channels) {
if (channel.name === CHANNEL_NAME) {
return channel.id;
}
}
throw new Error('Channel not found')
})
.then(channelId => {
return getHistory(channelId)
})
.then(history => {
return history.filter(function (message) {
return message.subtype === 'bot_message' && message.bot_id === BOT_ID;
})
})
.then(history => {
return console.log(history.length)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment