Last active
February 2, 2018 14:20
-
-
Save t-kashima/a8c3220ee7d5c6fc3291c5c8af572e48 to your computer and use it in GitHub Desktop.
sum message reactions on Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'slack' | |
Slack.configure do |config| | |
config.token = ENV['SLACK_TOKEN'] | |
end | |
def getChannelHistory(client, latest, oldest) | |
client.channels_history(channel: ENV['CHANNEL_ID'], latest: latest, oldest: oldest, inclusive: true, count: 1000) | |
end | |
client = Slack::Client.new | |
latest = ENV['LATEST_TS'] | |
oldest = ENV['OLDEST_TS'] | |
history = getChannelHistory(client, latest, oldest) | |
if history.has_key?("has_more") && history["has_more"] then | |
puts "Don't support more messages" | |
return | |
end | |
messages = history["messages"] | |
sum_reactions = {} | |
messages.each do |message| | |
next unless message.has_key?("reactions") | |
message["reactions"].each do |reaction| | |
reaction_name = reaction["name"] | |
if sum_reactions.has_key?(reaction_name) then | |
sum_reactions[reaction_name] += 1 | |
else | |
sum_reactions[reaction_name] = 1 | |
end | |
end | |
end | |
message = "" | |
i = 0 | |
sum_reactions.sort { |(k1, v1), (k2, v2)| v2 <=> v1 }.each { |(key, value)| | |
i += 1 | |
message += "#{i}. :#{key}: #{value}\n" | |
} | |
message += "#{messages.count} messages was analyzed.\n" | |
client.chat_postMessage(channel: ENV["POST_CHANNEL_NAME_TO_MESSAGE"], text: message, as_user: true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment