Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'slack-ruby-client'
- TOKEN = 'AUTH TOKEN'.freeze
- CHANNEL = 'CHANNEL ID'.freeze
- MESSAGES_PER_REQUEST = 1_000 # 1000 is the limit per request
- # :nodoc:
- class LogChannelHistory
- def initialize
- Slack.configure { |config| config.token = TOKEN }
- @client = Slack::Web::Client.new
- end
- def process_log
- request_params = { channel: CHANNEL, count: MESSAGES_PER_REQUEST }
- first_batch = get_batch(request_params)
- save_to_file(first_batch)
- request_count = 1
- while first_batch.has_more?
- request_params[:latest] = first_batch.messages.last['ts']
- first_batch = get_batch(request_params)
- save_to_file(first_batch)
- request_count += 1
- sleep 5 if request_count % 5 == 0
- end
- end
- private
- def users
- @users ||=
- Hash[@client.users_list['members'].map { |m| [m['id'], m['name']] }]
- end
- def list_channels
- @client.channels_list.channels.each { |ch| "#{ch.name} | #{ch.id}" }
- end
- def get_batch(request_params)
- @client.channels_history(request_params)
- end
- def save_to_file(batch)
- File.open('log.txt', 'a+') do |file|
- batch.messages.each do |message|
- username = users[message['user']]
- text = message['text'].inspect
- time = Time.at message['ts'].to_f
- file << "#{time} - #{username}: #{text} \n"
- end
- end
- end
- end
- # client.channels_list.channels.each { |ch| puts "#{ch.name} | #{ch.id}" }
- if __FILE__ == $0
- LogChannelHistory.new.process_log
- puts "Logged channel history"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement