Skip to content

Instantly share code, notes, and snippets.

@peterxjang
Last active July 15, 2022 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterxjang/5277fef877031749183eb753badd6e19 to your computer and use it in GitHub Desktop.
Save peterxjang/5277fef877031749183eb753badd6e19 to your computer and use it in GitHub Desktop.
A basic script to view the JSON output of a Slack export
# INSTRUCTIONS
# Export your Slack JSON data: https://slack.com/help/articles/201658943-Export-your-workspace-data
# Open the zip file to create a directory with the Slack data formatted as JSON
# In the root of that directory, make a Ruby file `slack_export_reader.rb` and copy this code into it
# In your terminal, install the tty-prompt gem: `gem install tty-prompt`
# In your terminal, navigate to the directory and run `ruby slack_export_reader.rb`
require "json"
require "pathname"
require "tty-prompt"
def parse_users
users = JSON.parse(File.read("users.json"))
users_by_id = {}
users.each do |user|
users_by_id[user["id"]] = user
end
users_by_id
end
def parse_messages(users)
result = {}
visible_folderpaths = Pathname.new(".").children.select { |c| c.directory? && c.to_s[0] != "." }
visible_folderpaths.each do |visible_folderpath|
channel_messages = []
json_filepaths = visible_folderpath.children.select { |c| c.file? && c.to_s.end_with?(".json") }
json_filepaths.each do |json_filepath|
messages = JSON.parse(File.read(json_filepath))
messages.each do |message|
next if message["subtype"] == "channel_join"
user = users[message["user"]]
name = user ? user["name"] : "<no name>"
channel_messages.push({ ts: message["ts"].to_f, text: message["text"], name: name })
end
result[visible_folderpath.to_s] = channel_messages.sort_by { |message| message[:ts] }
end
end
result
end
def format_message_preview(message)
"#{Time.at(message[:ts].to_f)} - #{message[:name]}: #{message[:text].split("\n").join(" ")[0..50]}"
end
def format_full_message(message)
"#{Time.at(message[:ts].to_f)}\n#{message[:name]}\n#{message[:text]}"
end
def run_viewer(messages)
prompt = TTY::Prompt.new
choices = messages.keys.sort
channel_name = prompt.select("Choose a channel", choices)
channel_messages = messages[channel_name]
choices = {}
channel_messages.each do |channel_message|
choices[format_message_preview(channel_message)] = channel_message
end
message = prompt.select("Choose a message", choices, per_page: 10)
puts format_full_message(message)
end
users = parse_users
messages = parse_messages(users)
run_viewer(messages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment