Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract a list of all the reactions to messages from a Slack data export
# Put this script in the root directory of a Slack data export
# Run `bundle install` and then `bundle exec ruby slack_reactions_from_export.rb`
# This will create a file called `reactions.txt` with rows like this:
#
# 2017-04-09 9:56 in #general: 😀
# 2017-04-10 9:45 in #tech-discussions: ⚽️
# 2017-04-19 15:45 in #general: 💖
# 2017-04-20 11:59 in #general: :hugging_face:
# 2017-04-20 22:35 in #general: 🤕
# 2017-04-30 21:38 in #most17: ❤️❤️
# 2017-05-01 20:33 in #general: 💯
# 2017-05-02 14:52 in #library: :thinking_face:
# 2017-05-03 8:49 in #general: 👋:skin-tone-2:
# 2017-05-03 16:34 in #general: 👋:skin-tone-2:👋:skin-tone-2:
#
# Not all the emoji's show up, because the library in use doesn't recognise all
# the emoji's from Slack. ¯\_(ツ)_/¯
#
require 'json'
require 'gemoji'
def convert_slack_time(ts_value)
Time.at ts_value.to_f
end
def formatted_time(ts_value)
convert_slack_time(ts_value).strftime("%F %k:%M")
end
def extract_user_name(id)
"@#{ @users.find { |u| u["id"].eql? id }["name"] }"
end
def convert_emojis(string)
string.gsub(/:([\w+-]+):/) do |match|
if emoji = Emoji.find_by_alias($1)
emoji.raw
else
match
end
end
end
def process_text(message)
text = message['text'].gsub(/<@\S+>/) do |match|
extract_user_name(match.delete("@<>"))
end
convert_emojis(text)
end
def write_message(message)
text = "#{formatted_time(message["ts"])} in ##{message["channel"]}: "
message["reactions"].each do |reaction|
reaction["count"].times.each do
# text += ":#{reaction["name"]}:"
text += convert_emojis(":#{reaction["name"]}:")
end
end
text
end
def selected_messages(channel_names)
chosen_ones = []
channel_names.each do |channel|
message_files = Dir.open(channel).entries.drop(2)
channel_message_json = []
message_files.each do |file_name|
file = File.open [channel, file_name].join("/")
channel_message_json.concat JSON.parse(file.read)
end
messages_with_reactions = channel_message_json.select do |message|
message["reactions"].any? if message.has_key? "reactions"
end
messages_with_reactions.each do |m|
chosen_ones << m.merge({ "channel" => channel })
end
end
chosen_ones.sort_by {|msg| msg["ts"].to_f}
end
@users = JSON.parse(File.open("users.json").read)
channel_names = JSON.parse(File.open('channels.json').read).map do |c|
c["name"]
end
File.open("reactions.txt", "w+") do |file|
selected_messages(channel_names).each do
|message| file << write_message(message) + "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment