Created
January 7, 2016 23:25
-
-
Save ekilah/f69f92afad5c48015939 to your computer and use it in GitHub Desktop.
save slack history
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'httparty' | |
require 'json' | |
require 'ap' | |
require 'fileutils' | |
# require 'Date' | |
API_TOKEN = "FILL THIS IN WITH YOUR API KEY" | |
BASE_URL = "https://slack.com/api/" | |
def default_params | |
{token: API_TOKEN} | |
end | |
def get_users_list | |
path = "users.list" | |
raw_response = HTTParty.get(BASE_URL + path, query: default_params) | |
JSON.parse(raw_response.body)["members"] | |
end | |
def get_dm_list | |
path = "im.list" | |
raw_response = HTTParty.get(BASE_URL + path, query: default_params) | |
JSON.parse(raw_response.body)["ims"] | |
end | |
def get_channel_list | |
path = "channels.list" | |
raw_response = HTTParty.get(BASE_URL + path, query: default_params.merge({exclude_archived:1})) | |
JSON.parse(raw_response.body)["channels"] | |
end | |
def get_groups_list | |
path = "groups.list" | |
raw_response = HTTParty.get(BASE_URL + path, query: default_params.merge({exclude_archived:1})) | |
JSON.parse(raw_response.body)["groups"] | |
end | |
# def format_msg(msg) | |
# output = "" | |
# output << DateTime.strptime(msg["ts"], '%s') | |
# output << msg["text"] | |
# end | |
def get_convo_type(convo_id, type) | |
path = "#{type}.history" | |
params = default_params.merge({channel:convo_id, count:1000}) | |
msgs = [] | |
loop do | |
raw_response = HTTParty.get(BASE_URL + path, query: params) | |
json = JSON.parse(raw_response.body) | |
has_more = false | |
if json["ok"] | |
if json["messages"].nil? or json["messages"].empty? | |
ap "no more messages" | |
break | |
end | |
has_more = json["has_more"] | |
msgs += json["messages"] | |
last_timestamp = json["messages"].last["ts"] | |
params = params.merge({ latest: last_timestamp }) | |
else | |
ap "JSON response not OK" | |
ap json | |
end | |
ap "more messages!!" if has_more | |
break if !has_more | |
end | |
msgs.reverse | |
end | |
def get_dm(user_id) | |
get_convo_type(user_id, "im") | |
end | |
def get_channel(channel_id) | |
get_convo_type(channel_id, "channels") | |
end | |
def get_group(group_id) | |
get_convo_type(group_id, "groups") | |
end | |
def get_dms | |
users = get_users_list | |
dms = get_dm_list | |
users.each do |user| | |
user = user | |
id = user["id"] | |
name = user["name"] | |
dm = dms.select{|dm| dm["user"] == id}.first | |
if dm.nil? or dm.empty? or !name | |
ap "DM not found for #{name || id}" | |
next | |
end | |
folder_name = 'dms' | |
FileUtils.mkdir_p folder_name | |
File.open("#{folder_name}/#{name.downcase}_#{dm["id"]}_#{id}.json", "w+") { |f| | |
ap "fetching dms from #{name}" | |
result = get_dm(dm["id"]) | |
#ap result | |
f.write(result.to_json) unless result.empty? | |
} | |
end | |
end | |
def get_channel_history | |
channels = get_channel_list | |
channels.each do |channel| | |
channel = channel | |
id = channel["id"] | |
name = channel["name"] | |
#next unless ["alumni", "bd", "engineering", "general", "outdoors", "product", "qa"].include? name | |
folder_name = 'channels' | |
FileUtils.mkdir_p folder_name | |
File.open("#{folder_name}/#{name.downcase}_#{id}.json", "w+") { |f| | |
ap "fetching channel #{name}" | |
result = get_channel(id) | |
#ap result | |
f.write(result.to_json) unless result.empty? | |
} | |
end | |
end | |
#private channels | |
def get_groups_history | |
groups = get_groups_list | |
groups.each do |groups| | |
groups = groups | |
id = groups["id"] | |
name = groups["name"] | |
#next unless ["only include this private channel name"].include? name | |
folder_name = 'private_channels' | |
FileUtils.mkdir_p folder_name | |
File.open("#{folder_name}/#{name.downcase}_#{id}.json", "w+") { |f| | |
ap "fetching private channel #{name}" | |
result = get_group(id) | |
#ap result | |
f.write(result.to_json) unless result.empty? | |
} | |
end | |
end | |
def save_users_list | |
File.open("users.json", "w+") { |f| | |
result = get_users_list | |
f.write(result.to_json) unless result.empty? | |
} | |
end | |
save_users_list | |
get_dms | |
get_channel_history | |
get_groups_history |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://api.slack.com/web is where you find your token