Last active
June 26, 2019 06:11
-
-
Save tai-cha/a1276f304ac62f3e9c87f28c5bfa4538 to your computer and use it in GitHub Desktop.
Slackで特定チャンネルに全員招待するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'slack-ruby-client' | |
class OutOfRangeError < Exception | |
end | |
TOKEN = 'Replace here your token' | |
if ARGV.include?("--help") | |
puts "このアプリケーションは指定したSlackのチャンネルに対して全員招待をするものです。" | |
puts "使用法: ruby inviteAll.rb [引数]\n" | |
puts "引数" | |
puts "-v --verbose 招待できない場合のエラー内容を表示" | |
else | |
begin | |
Slack.configure do |conf| | |
conf.token = TOKEN | |
end | |
client = Slack::Web::Client.new | |
user_list = client.users_list | |
conversation_list = client.conversations_list | |
channels = conversation_list.channels | |
puts "全員招待したいチャンネルを数字で入力して下さい。" | |
channels.each_with_index do |channel, index| | |
puts "#{index}: #{channel.name}" | |
end | |
begin | |
print("> ") | |
input = Integer(STDIN.readline) | |
if input >= channels.length | |
raise OutOfRangeError # 自作エラー。 | |
end | |
rescue ArgumentError | |
puts "不正な文字列です。数字で入力して下さい。" | |
retry | |
rescue OutOfRangeError | |
puts "存在しないチャンネル番号が指定されました。やり直して下さい。" | |
retry | |
end | |
channel = conversation_list.channels[input] | |
puts "#{channel.name}に全員招待します。" | |
user_list.members.each do |user| | |
begin | |
client.channels_invite(channel: channel.id, user: user.id) | |
rescue => e | |
if ARGV.include?("-v") || ARGV.include?("--verbose") | |
if user.profile.display_name.blank? | |
puts "#{e} #{channel.name} #{user.real_name}" | |
else | |
puts "#{e} #{channel.name} #{user.profile.display_name}" | |
end | |
end | |
end | |
end | |
rescue SocketError, Faraday::ConnectionFailed | |
puts "Slackへの接続ができません。インターネット接続を見直して下さい。" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment