Last active
June 2, 2021 15:10
-
-
Save plainice/de6d7df0b3c33f3caa8ef44d40ef64d5 to your computer and use it in GitHub Desktop.
Slack history as csv based on content in message
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
# dependencies: | |
# gem install slack-ruby-client | |
# | |
# Create a slack app (https://api.slack.com/apps) | |
# | |
# Add permissions (Basic Information > Add features and functionality > Permissions) | |
# Bot Token scopes: channels:history, channels:read | |
# | |
# Get Slack token (Install app > Bot User OAuth Token) | |
# - set as environment variable | |
# | |
# SLACK_TOKEN=<your token> | |
require 'slack-ruby-client' | |
require 'date' | |
# Dates of history to request | |
oldest = Date.new(2021,1,1).to_time.to_i | |
latest = Date.new(2021,6,1).to_time.to_i | |
# Slack channel you want history from | |
CHANNEL = "#pushback" | |
puts "date,email" | |
def print_messages(messages) | |
messages.each do |msg| | |
next unless msg | |
# Regex matching the message you want to see | |
if msg["text"] =~ /email: (.*)\s+/ | |
puts "#{DateTime.strptime(msg["ts"].to_s, '%s')},#{$1}" | |
end | |
end | |
end | |
Slack.configure do |config| | |
config.token = ENV['SLACK_TOKEN'] | |
end | |
client = Slack::Web::Client.new | |
puts client.auth_test | |
resp = client.conversations_history(latest: latest, oldest: oldest, channel: CHANNEL) | |
print_messages(resp["messages"]) | |
has_more = resp["has_more"] | |
while has_more | |
resp = client.conversations_history(latest: latest, oldest: oldest, channel: CHANNEL, cursor: resp["response_metadata"]["next_cursor"] ) | |
raise "Failed" unless resp["ok"] | |
print_messages(resp["messages"]) | |
has_more = resp["has_more"] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment