-
Notifications
You must be signed in to change notification settings - Fork 3
/
join.rb
63 lines (56 loc) · 1.87 KB
/
join.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'slack-ruby-client'
slack = Slack::Web::Client.new(token: ENV['SLACK_API_TOKEN'])
members = []
next_cursor = nil
loop do
slack_users = slack.users_list({ limit: 1000, cursor: next_cursor })
members << slack_users['members']
next_cursor = slack_users['response_metadata']['next_cursor']
break if next_cursor.empty?
end
members.flatten!
channels = []
next_cursor = nil
loop do
slack_channels = slack.conversations_list({ limit: 1000, cursor: next_cursor })
channels << slack_channels['channels']
next_cursor = slack_channels['response_metadata']['next_cursor']
break if next_cursor.empty?
end
channels.flatten!
channels.each do |c|
next_cursor = nil
loop do
c['members'] ||= []
break if c['num_members'] == 0 || c['is_archived']
channel_members = slack.conversations_members({ channel: c['id'], limit: 1000, cursor: next_cursor })
c['members'] << channel_members['members']
next_cursor = channel_members['response_metadata']['next_cursor']
break if next_cursor.empty?
end
end
user = members.find { |m| m['name'] == ENV['SLACK_USER'] }
exclude_channels = ENV['SLACK_EXCLUDE_CHANNELS'] ? ENV['SLACK_EXCLUDE_CHANNELS'].split(/,/) : []
channels.each do |c|
next if c['is_archived']
next if c['members'].empty? || c['members'].flatten.find { |m| m == user['id'] }
next if exclude_channels.include?(c['name'])
next if c['is_ext_shared']
begin
slack.conversations_invite({ channel: c['id'], users: user['id'] })
rescue Slack::Web::Api::Errors::AlreadyInChannel
next
rescue Slack::Web::Api::Errors::NotInChannel
slack.conversations_join({ channel: c['id'] })
begin
slack.conversations_invite({ channel: c['id'], users: user['id'] })
rescue Slack::Web::Api::Errors::AlreadyInChannel
next
ensure
slack.conversations_leave({ channel: c['id'] })
end
rescue StandardError => e
pp c
pp e
end
end