Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. require 'slack-ruby-client'
  2.  
  3. TOKEN = 'AUTH TOKEN'.freeze
  4. CHANNEL = 'CHANNEL ID'.freeze
  5. MESSAGES_PER_REQUEST = 1_000 # 1000 is the limit per request
  6.  
  7. # :nodoc:
  8. class LogChannelHistory
  9. def initialize
  10. Slack.configure { |config| config.token = TOKEN }
  11. @client = Slack::Web::Client.new
  12. end
  13.  
  14. def process_log
  15. request_params = { channel: CHANNEL, count: MESSAGES_PER_REQUEST }
  16.  
  17. first_batch = get_batch(request_params)
  18. save_to_file(first_batch)
  19.  
  20. request_count = 1
  21.  
  22. while first_batch.has_more?
  23. request_params[:latest] = first_batch.messages.last['ts']
  24. first_batch = get_batch(request_params)
  25. save_to_file(first_batch)
  26. request_count += 1
  27.  
  28. sleep 5 if request_count % 5 == 0
  29. end
  30. end
  31.  
  32. private
  33.  
  34. def users
  35. @users ||=
  36. Hash[@client.users_list['members'].map { |m| [m['id'], m['name']] }]
  37. end
  38.  
  39. def list_channels
  40. @client.channels_list.channels.each { |ch| "#{ch.name} | #{ch.id}" }
  41. end
  42.  
  43. def get_batch(request_params)
  44. @client.channels_history(request_params)
  45. end
  46.  
  47. def save_to_file(batch)
  48. File.open('log.txt', 'a+') do |file|
  49. batch.messages.each do |message|
  50. username = users[message['user']]
  51. text = message['text'].inspect
  52. time = Time.at message['ts'].to_f
  53. file << "#{time} - #{username}: #{text} \n"
  54. end
  55. end
  56. end
  57. end
  58.  
  59. # client.channels_list.channels.each { |ch| puts "#{ch.name} | #{ch.id}" }
  60. if __FILE__ == $0
  61. LogChannelHistory.new.process_log
  62. puts "Logged channel history"
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement