Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.