Skip to content

Instantly share code, notes, and snippets.

@spasiu
Created November 3, 2020 16:12
Show Gist options
  • Save spasiu/326e49d436f5f49cf27f8130107cb166 to your computer and use it in GitHub Desktop.
Save spasiu/326e49d436f5f49cf27f8130107cb166 to your computer and use it in GitHub Desktop.
New Slack channel Archiver
'use strict';
const request = require('request')
const token = '<BotUserOAuthAccessToken>'
const paths = {
list: `https://slack.com/api/conversations.list?token=${token}&exclude_archived=true`,
archive: `https://slack.com/api/conversations.archive?token=${token}&channel=`,
join: `https://slack.com/api/conversations.join?token=${token}&channel=`
}
const log = tag => v => {
console.log(tag, v);
return v;
}
const listChannels = () => new Promise(function(resolve, reject) {
request.get(paths.list, function(err, res, body) {
err ? reject(err) : resolve(body)
})
})
const joinChannel = (channel) => new Promise(function(resolve, reject) {
request.get(paths.join + channel, function(err, res, body) {
err ? reject(err) : resolve(body)
})
})
const archiveChannel = (channel) => new Promise(function(resolve, reject) {
request.get(paths.archive + channel, function(err, res, body) {
err ? reject(err) : resolve(body)
})
})
const archive = (arr) => {
const channel = arr.pop()
console.log(channel.name);
joinChannel(channel.id)
.then(body => JSON.parse(body))
.then(log('JOIN CHANNEL'))
.then(data => archiveChannel(data.channel.id))
.then(log('ARCHIVE CHANNEL'))
.catch(log('ERROR'))
.then(() => {
arr.length === 0 ? null : setTimeout(() => archive(arr), 1000)
})
}
listChannels().then(body => {
log('LIST CHANNELS')(body)
archive(JSON.parse(body).channels.filter(channel => {
return channel.name.slice(0, 3) === 'sk-'
}))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment