Skip to content

Instantly share code, notes, and snippets.

@Mahito
Created December 21, 2018 02:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mahito/4be3f24bacd715f415eefeccfdafe87b to your computer and use it in GitHub Desktop.
Save Mahito/4be3f24bacd715f415eefeccfdafe87b to your computer and use it in GitHub Desktop.
`invite all members to [channel name] for [topic]` でチャンネルを作ってトピックを設定して全メンバをチャンネルに入れるBOTのコード
require 'slack-ruby-client'
Slack::Web::Client.configure do |config|
config.token = ENV['SLACK_API_USER_TOKEN']
raise 'Missing ENV[SLACK_API_USER_TOKEN]!' unless config.token
STDOUT.sync = true
config.logger = Logger.new(STDOUT)
config.logger.level = Logger::INFO
end
Slack::RealTime::Client.configure do |config|
config.token = ENV['SLACK_API_BOT_TOKEN']
raise 'Missing ENV[SLACK_API_BOT_TOKEN]!' unless config.token
STDOUT.sync = true
config.logger = Logger.new(STDOUT)
config.logger.level = Logger::INFO
end
web = Slack::Web::Client.new
rtm = Slack::RealTime::Client.new
rtm.on :message do |data|
case data.text
when /invite all members to (\S+) for (.+)/
if $2.nil?
payload = {
channel: data.channel,
text: 'Command Error: invite all members to [channel name] for [topic]',
as_user: false
}
web.chat_postMessage(payload)
else
channel_id = web.channels_create(name: $1).channel['id']
web.channels_setTopic(channel: channel_id, topic: $2)
web.users_list.members.each do |member|
next if member['is_bot']
begin
web.channels_invite(channel: channel_id, user: member['id'])
rescue Slack::Web::Api::Errors::SlackError
next
end
end
end
end
end
rtm.start!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment