Skip to content

Instantly share code, notes, and snippets.

@pospi
Last active September 14, 2017 23:24
Show Gist options
  • Select an option

  • Save pospi/545477b576d1ac9470f405c7eef5e81a to your computer and use it in GitHub Desktop.

Select an option

Save pospi/545477b576d1ac9470f405c7eef5e81a to your computer and use it in GitHub Desktop.
Detects old Slack channels for archival / cleanup
/.env
/node_modules

About

This script detects old Slack channels and groups which have not been used in a while and outputs their names to the console. It also sends a configurable warning message to these channels indicating that they have been deemed "old".

Setup

  • Create a new Slack app for the team at https://api.slack.com/
  • Authorize it with incoming-webhook, channels:read, groups:read & chat:write:bot permissions.
  • Install the app manually using the button in the UI and copy the auth token to .env, using the format SLACK_TOKEN=${AUTH_TOKEN_STR}.

You can fiddle with STALE_TIME_DIFF and STALE_CHANNEL_MESSAGE in the code to configure the particulars of the channel detection and warning message posting.

/**
* Checks for channels which haven't been posted to in at least STALE_TIME_DIFF seconds
*
* @author: pospi <pospi@spadgos.com>
* @since: 2017-08-23
* @flow
*/
require('dotenv').load();
const slack = require('slack');
const thenify = require('thenify');
const STALE_TIME_DIFF = 6 * 28 * 24 * 3600 * 1000;
const STALE_CHANNEL_MESSAGE = "This channel has been detected as not having had any updates in a very long time. It will be manually archived 7 days from this message unless notified otherwise by a member of this channel. *IF YOU DO NOT WANT THE CHANNEL ARCHIVED, PLEASE REPLY AND LET US KNOW NOW!*";
const token = process.env.SLACK_TOKEN;
const postMessage = thenify(slack.chat.postMessage);
const STALE_TIME_CUTOFF = Date.now() - STALE_TIME_DIFF;
function locateStaleChannels(channelSet) {
const listChannels = thenify(slack[channelSet].list);
const getChannelInfo = thenify(slack[channelSet].info);
return listChannels({ token })
.then(res => { console.log(`Is OK?- ${res.ok}`); return res.channels || res.groups; })
.then(channels => Promise.all(channels.map(c => getChannelInfo({ token, channel: c.id }).then(info => info.channel))))
.then(channels => {
const staleChannels = channels.filter(c => c.latest && parseFloat(c.latest.ts, 10) * 1000 < STALE_TIME_CUTOFF);
console.log(`Found ${staleChannels.length} stale ${channelSet}`);
console.log(staleChannels.map(c => ({ id: c.id, name: c.name })));
// return Promise.all(staleChannels.map(ch => postMessage({ token, channel: ch.id, text: STALE_CHANNEL_MESSAGE })));
})
.then(console.log);
}
locateStaleChannels('channels')
.then(() => locateStaleChannels('groups'));
{
"name": "slack-archival-bot",
"version": "1.0.0",
"lockfileVersion": 1,
"dependencies": {
"any-promise": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz",
"integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8="
},
"dotenv": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-4.0.0.tgz",
"integrity": "sha1-hk7xN5rO1Vzm+V3r7NzhefegzR0="
},
"options": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/options/-/options-0.0.6.tgz",
"integrity": "sha1-7CLTEoBrtT5zF3Pnza788cZDEo8="
},
"slack": {
"version": "8.3.1",
"resolved": "https://registry.npmjs.org/slack/-/slack-8.3.1.tgz",
"integrity": "sha1-ToNg3VT+oaYlWl3ah0aki/u3T/A="
},
"thenify": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/thenify/-/thenify-3.3.0.tgz",
"integrity": "sha1-5p44obq+lpsBCCB5eLn2K4hgSDk="
},
"tiny-json-http": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/tiny-json-http/-/tiny-json-http-5.1.1.tgz",
"integrity": "sha1-LxIiMArKv+zz7nuwejUKcvgv9xg="
},
"ultron": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz",
"integrity": "sha1-rOEWq1V80Zc4ak6I9GhTeMiy5Po="
},
"ws": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/ws/-/ws-1.1.4.tgz",
"integrity": "sha1-V/QNA2gy5fUFVmKjl8Tedu1mv2E="
}
}
}
{
"name": "slack-archival-bot",
"version": "1.0.0",
"description": "Cleanup bot to identify old channels",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "UNLICENSED",
"dependencies": {
"dotenv": "^4.0.0",
"slack": "^8.3.1",
"thenify": "^3.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment