Skip to content

Instantly share code, notes, and snippets.

@penguinwokrs
Last active January 30, 2024 09:21
Show Gist options
  • Save penguinwokrs/a4a41df6ccc3c584165bbdc9e429a404 to your computer and use it in GitHub Desktop.
Save penguinwokrs/a4a41df6ccc3c584165bbdc9e429a404 to your computer and use it in GitHub Desktop.
slackで検索した結果をcsvに残す
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'slack-ruby-client'
gem 'debug'
# frozen_string_literal: true
require 'csv'
require 'slack-ruby-client'
require 'date'
require 'debug'
Slack.configure do |config|
config.token = 'token'
end
client = Slack::Web::Client.new
search_params = {
query: 'from:me in:#channel before:2024-01-30 after:2023-11-01',
page: 1
}
messages = []
loop do
response = client.search_all(search_params)
break if response.messages.matches.empty?
messages += response.messages.matches
search_params[:page] += 1
end
filename = "slack_channels_#{DateTime.now.strftime('%Y%m%d%H%M')}.csv"
# 日本式曜日
days_of_week = %w[日 月 火 水 木 金 土]
CSV.open(filename, 'wb') do |csv|
csv << ['ID', 'Name', 'Created at', '曜日', 'Creator', 'Message']
messages.each do |message|
channel = message.channel
created_time = Time.at(message.ts.to_f)
created_at = created_time.strftime("%Y年%-m月%-d日 %H:%M")
day_of_week = days_of_week[created_time.wday]
csv << [channel['id'], channel['name'], created_at, day_of_week, channel['creator'], message.text]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment