Skip to content

Instantly share code, notes, and snippets.

@goosetav
Created August 2, 2015 22:27
Show Gist options
  • Save goosetav/b85b7541080b9540e747 to your computer and use it in GitHub Desktop.
Save goosetav/b85b7541080b9540e747 to your computer and use it in GitHub Desktop.
clean up slack: a channel archival script
#!/usr/bin/env ruby
# The MIT License (MIT)
#
# Copyright (c) 2015 Erik Gustavson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
################################################################################
#
# Usage:
#
# Make sure you chmod the script first...
# ./slacker.rb <SLACK API TOKEN>
#
# Archive rules
# 1. check channel member count
# 2. check last time a human posted a message
#
# archive if 0 members
# archive if 1 member and last message > 30 days
# archive if 2,3 members and last message > 60 days
# archive if 4 members and last message > 90 days
# archive if 5-10 members and last message > 120 days
# if more than 10 members, assume its a low volume notification channel and do not archive
#
# YMMV
#
require 'rubygems'
require 'slack'
require 'hashie/mash'
require 'date'
def die!(s)
puts s
exit(-1)
end
def m(o)
Hashie::Mash.new(o)
end
def archive?(members, age)
case
when members < 1
true
when (members == 1) && (age >= 30)
true
when (members >= 2) && (members < 4) && (age >= 60)
true
when (members == 4) && (age >= 90)
true
when (members <= 10) && (age >= 120)
true
else
false
end
end
token = ARGV.shift
die!("usage: slacker.rb SLACK_TOKEN") unless token
Slack.configure do |config|
config.token = token
end
client = Slack.client
archive_list = []
now = DateTime.now
channels_list = m client.channels_list(:exclude_archived => 1)
print "Analyzing #{channels_list.channels.count} channels "
## analyze
channels_list.channels.each do |channel|
history = m(client.channels_history(:channel => channel.id))
last_real_msg = history.messages.detect {|x| !x.subtype} || history.messages.last
ts = DateTime.strptime(last_real_msg.ts, '%s')
age = (now.to_date - ts.to_date).to_i
if archive?(channel.num_members, age)
archive_list << m({
:channel => channel,
:members => channel.num_members,
:age => age,
:last => ts
})
print 'x'
else
print '.'
end
end
## process
if archive_list.length > 0
puts "\n\nThe following channels should be archived...\n\n"
puts ["Channel".ljust(21), "Members".ljust(10), "Days Stale".ljust(12)].join(" ")
puts ["-"*(21), "-"*(10), "-"*(12)].join(" ")
archive_list.each do |a|
puts [a.channel.name.ljust(21), a.members.to_s.ljust(10), a.age.to_s.ljust(12)].join(" ")
end
print "\nArchive these? (y/N): "
answer = gets.downcase.chomp!
if ['y','yes'].include?(answer)
print "\nArchiving "
archive_list.each do |x|
result = m client.channels_archive(:channel => x.channel.id)
print result.ok ? '.' : result.to_s
end
puts "\n\nDone!\n\n"
else
puts "Ok... stopping here."
end
else
puts "\n\nAll good!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment