Skip to content

Commit

Permalink
Switched to app rather than bot, as archive will not work with bot pe…
Browse files Browse the repository at this point in the history
…rmissions
  • Loading branch information
jshiell committed Oct 25, 2018
1 parent f29d48d commit c4250eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,29 @@ You'll need the following pre-requisites.
* Ruby 2.5.1
* Bundler

You'll need an API token to run it. This can be specified in three ways, in order of priority:

1. Pass it as the second argument, e.g. `bin/slack-channel-archiver 90 api-token`
1. Set it as env var `SLACK_API_TOKEN`, e.g. `SLACK_API_TOKEN=api-token bin/slack-channel-archiver`
1. Set in `~/.slack-channel-archiver`, e.g. `api-token: an-api-token`
You'll need a bot & user API tokens to run it. This can be specified in three ways, in order of priority:

1. Pass it as the trailing arguments, e.g. `bin/slack-channel-archiver 90 bot-api-token user-api-token`
1. Set it as env var `BOT_SLACK_API_TOKEN`/`USER_SLACK_API_TOKEN`, e.g. `BOT_SLACK_API_TOKEN=api-token USER_SLACK_API_TOKEN=another-token bin/slack-channel-archiver`
1. Set in `~/.slack-channel-archiver`, e.g.
```
bot-api-token: an-api-token
user-api-token: another-api-token
```

You can obtain a new API token by [creating a new Bot Integration](https://my.slack.com/services/new/bot) in Slack.
You can obtain new API tokens by [creating a new App](https://api.slack.com/apps/new) in Slack. You'll need the scopes:
* `bot`
* `chat:write:bot`
* `channels:write`
* `channels:read`
* `channels:history`.

Note that channels will be marked as archived by the user who installed the app, as Slack doesn't allow bots to archive channels.

Usage:

bin/slack-channel-archiver [inactivity-period = 90 days] [api-token]
bin/slack-channel-archiver [inactivity-period = 90 days] [bot-api-token] [user-api-token]

On the first run it'll install dependencies, which may be a bit noisy.

Due to rate limits on the Slack side it'll run quite slowly - about 1 channel/second will be tested.
41 changes: 20 additions & 21 deletions lib/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

class SlackChannelArchiver

def initialize(api_token)
Slack.configure do |config|
config.token = api_token
end

@client = Slack::Web::Client.new
def initialize(bot_api_token, user_api_token)
@client = Slack::Web::Client.new(token: bot_api_token)
@client.auth_test

@user_client = Slack::Web::Client.new(token: user_api_token)
@user_client.auth_test
end

def archive_channels_inactive_for(number_of_days)
Expand All @@ -33,17 +32,17 @@ def archive_channel_if_inactive_for(number_of_days, channel)
archived = false

if days_ago(Time.at(channel.created)) > number_of_days
last_messages = @client.channels_history(channel: channel.id, count: 1)
last_messages = @user_client.channels_history(channel: channel.id, count: 1)
if last_messages.messages.empty?
puts "x Channel #{channel.name} is older than #{number_of_days} and has no messages"
@client.chat_postMessage(channel: channel.id, text: "This channel has had no new messages in #{number_of_days} and will hence be archived - any queries, please see #slack-admin")
@client.channels_archive(channel: channel.id)
@user_client.channels_archive(channel: channel.id)
archived = true

elsif days_ago(last_messages.messages.first.ts.to_d) > number_of_days
puts "x Channel #{channel.name} is older than #{number_of_days} days and has had no messages in at least #{number_of_days} days"
@client.chat_postMessage(channel: channel.id, text: "This channel is older than #{number_of_days} days and has no messages, and will hence be archived - any queries, please see #slack-admin")
@client.channels_archive(channel: channel.id)
@user_client.channels_archive(channel: channel.id)
archived = true
else
puts "- Channel #{channel.name} is in regular use"
Expand All @@ -65,17 +64,17 @@ class Launcher

def initialize(args)
number_of_days = (args[0] || DEFAULT_NUMBER_OF_DAYS).to_i
api_token = read_api_token(args[1])
if number_of_days.nil? || api_token.nil?
$stderr.puts "Usage: $0 [number-of-days=#{DEFAULT_NUMBER_OF_DAYS}] [api-token]"
$stderr.puts "Alternately you may specify the API token in ~/.slack-channel-archiver as 'api-token', or via env car SLACK_API_TOKEN"
bot_api_token, user_api_token = read_api_token(args[1], args[2])
if number_of_days.nil? || bot_api_token.nil? || user_api_token.nil?
$stderr.puts "Usage: $0 [number-of-days=#{DEFAULT_NUMBER_OF_DAYS}] [bot-api-token] [user-api-token]"
$stderr.puts "Alternately you may specify the API token in ~/.slack-channel-archiver as 'api-token', or via envs BOT_SLACK_API_TOKEN / USER_SLACK_API_TOKEN"
exit(1)
end

puts "Channels that have existed but have had no new messages for at least #{number_of_days} days will be archived"
puts "Please note only one channel will be checked per second due to Slack API rate limits"

slack_channel_archiver = SlackChannelArchiver.new(api_token)
slack_channel_archiver = SlackChannelArchiver.new(bot_api_token, user_api_token)
archived_count, total_channels = slack_channel_archiver.archive_channels_inactive_for(number_of_days)

puts "#{archived_count} of #{total_channels} channels were archived"
Expand All @@ -85,17 +84,17 @@ def initialize(args)

DEFAULT_NUMBER_OF_DAYS = 90

def read_api_token(api_token_arg)
api_token = api_token_arg

api_token = ENV['SLACK_API_TOKEN'] if api_token.nil?
def read_api_token(bot_api_token_arg, user_api_token_arg)
bot_api_token = bot_api_token_arg || ENV['BOT_SLACK_API_TOKEN']
user_api_token = user_api_token_arg || ENV['USER_SLACK_API_TOKEN']

if api_token.nil? && File.exists?("#{Dir.home}/.slack-channel-archiver")
if (bot_api_token.nil? || user_api_token.nil?) && File.exists?("#{Dir.home}/.slack-channel-archiver")
config = YAML.load_file("#{Dir.home}/.slack-channel-archiver")
api_token = config['api-token']
bot_api_token = bot_api_token || config['bot-api-token']
user_api_token = user_api_token || config['user-api-token']
end

api_token
[bot_api_token, user_api_token]
end

end
Expand Down

0 comments on commit c4250eb

Please sign in to comment.