Created
September 4, 2017 18:42
-
-
Save realityking/8ac1fbba06544d835f62649cdd7e05a1 to your computer and use it in GitHub Desktop.
Count slack messages by bot in a channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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