Skip to content

Instantly share code, notes, and snippets.

@jstanley0
Created May 11, 2019 02:04
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 jstanley0/0cdf6cb35d8fd29efad1f5c6f2241438 to your computer and use it in GitHub Desktop.
Save jstanley0/0cdf6cb35d8fd29efad1f5c6f2241438 to your computer and use it in GitHub Desktop.
Script to download Slack DMs
require 'slack-ruby-client'
require 'pg'
Slack.configure do |config|
config.token = File.read(File.expand_path('~/.slack/token')).strip
end
client = Slack::Web::Client.new
me = client.auth_test
puts "retrieving DMs for #{me.user} (#{me.user_id})"
print "listing users: "
users = {}
client.users_list do |page|
page.members.each do |user|
users[user.id] = user.name
end
end
puts users.size
print "finding DM conversations: "
ims = {}
client.conversations_list(types: 'im') do |page|
page.channels.each do |channel|
ims[channel.user] = channel.id
end
end
puts ims.size
# CREATE TABLE messages (username TEXT, from_me BOOLEAN, timestamp TIMESTAMP, text TEXT);
# CREATE INDEX ON messages(username);
# CREATE INDEX ON messages(timestamp);
db = PG::Connection.open(:dbname => 'slack-dms')
ims.each do |user_id, channel_id|
username = users[user_id]
print "#{username}: "
n = 0
client.conversations_history(channel: channel_id) do |page|
page.messages.each do |message|
next unless message.type == 'message'
db.exec_params('INSERT INTO messages(username, from_me, timestamp, text) VALUES ($1, $2, $3, $4)', [username, message.user == me.user_id, Time.at(message.ts.to_f), message.text])
n += 1
end
end
puts "#{n} messages"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment