Skip to content

Instantly share code, notes, and snippets.

@leogopal
Forked from daybreaker/reactions.rb
Created February 19, 2020 23:42
Show Gist options
  • Save leogopal/c1810aaec945a182640507b18da43a64 to your computer and use it in GitHub Desktop.
Save leogopal/c1810aaec945a182640507b18da43a64 to your computer and use it in GitHub Desktop.
Ruby script to check slack reaction counts
require 'slack-ruby-client' # first, make sure you do: gem install slack-ruby-client
require 'date'
# Add your Slack API token here
token = [YOUR TOKEN HERE]
Slack.configure do |config|
config.token = token
end
client = Slack::Web::Client.new
messages = []
# You can change 2017-01-01 to whatever is closer to your start date of using slack.
# You might need to move it to a closer date if you keep getting rate limited by the API
while messages.empty? || DateTime.strptime(messages.last.ts,'%s') > Date.parse('2017-01-01') do
# Loop through 500 messages at a time. Any more and the API will crash or rate limit you. It still might.
messages += messages.any? ?
client.channels_history(channel: '#general', count: 500, latest: messages.last.ts).messages :
client.channels_history(channel: '#general', count: 500).messages
puts messages.count
end
# Build a hash where the structure will be:
# { "emoji_name": num_times_used }
reactions = Hash.new(0)
# Select every message with a reaction
# Loop through each reaction in a message
# Some reactions have extra info after the emoji name in the format "emoji_name::other_stuff", so ignore the last part
# reaction[:name] = the name of the emoji
# reaction[:count] = number of times that emoji was used as a reaction on that specific message
messages.select { |mess| mess.reactions.present? }.each{ |mess| mess.reactions.each { |reaction| reactions[reaction[:name].split('::').first] += reaction[:count] } }
# Sort reactions by total number of times used.
puts reactions.sort { |a, b| b[1] <=> a[1] }.inspect
puts "The End"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment