Skip to content

Commit

Permalink
SlackPatron::SlackClient
Browse files Browse the repository at this point in the history
  • Loading branch information
cookie-s committed Apr 28, 2023
1 parent 076a90b commit 66d6cc5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
35 changes: 35 additions & 0 deletions lib/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,38 @@
Slack::Events.configure do |c|
c.signing_secret = config['slack']['use_events_api'] ? config['slack']['signing_secret'] : nil
end

module SlackPatron
class SlackClient
attr_reader :client

def initialize
@client = Slack::Web::Client.new
end

def conversations_list
channels = []
client.conversations_list({type: 'public_channel', limit: 1000}) do |response|
channels += response.channels
end
channels
end

def users_list
members = []
client.users_list do |response|
members += response.members
end
members
end

def emoji_list
# paginationがないらしい
client.emoji_list.emoji
end

def conversations_history(channel, count)
client.conversations_history({channel: channel, limit: count}).messages
end
end
end
44 changes: 20 additions & 24 deletions lib/slack_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
require './lib/db'

class SlackLogger
attr_reader :client
attr_reader :slack

def initialize
@client = Slack::Web::Client.new
@slack = SlackPatron::SlackClient.new
end

def is_private_channel(channel_name)
Expand Down Expand Up @@ -47,33 +47,31 @@ def drop_reaction(ts, name, user)
end

def update_users
users = client.users_list['members']
users = slack.users_list
replace_users(users)
end

def update_channels
channels = client.conversations_list({type: 'public_channel'})['channels']
channels = slack.conversations_list
replace_channels(channels)
end

def update_emojis
emojis = client.emoji_list['emoji'] rescue nil
emojis = slack.emoji_list
replace_emojis(emojis)
end

# log history messages
def fetch_history(target, channel)
messages = client.send(
target,
channel: channel,
count: 1000,
)['messages'] rescue nil

unless messages.nil?
messages.each do |m|
m['channel'] = channel
insert_message(m)
end
def fetch_history(channel)
begin
messages = slack.conversations_history(channel, 1000)
rescue Slack::Web::Api::Errors::NotInChannel
return # どうしようもないね
end
return if messages.nil?

messages.each do |m|
m['channel'] = channel
insert_message(m)
end
end

Expand All @@ -85,12 +83,10 @@ def start(collector)
update_users
update_channels

Channels.find.each do |c|
puts "loading messages from #{c[:name]}"
if c[:is_channel]
fetch_history(:conversations_history, c[:id])
end
sleep(1)
Channels.find.each do |channel|
puts "loading messages from #{channel[:name]}"
fetch_history channel[:id]
sleep 1
end

# realtime event is joined and dont exit current thread
Expand Down

0 comments on commit 66d6cc5

Please sign in to comment.