module SlackCLI
Main module
Constants
- VERSION
Public Instance Methods
access_token()
click to toggle source
# File lib/slack_cli.rb, line 68 def access_token @access_token ||= begin File.file?(cli_app.authorization_path) || raise("Application not authorized! Try calling `slack authorize`") JSON.parse(File.read(cli_app.authorization_path))['access_token'] end end
bot_token()
click to toggle source
# File lib/slack_cli.rb, line 75 def bot_token @access_token ||= begin File.file?(cli_app.authorization_path) || raise("Application not authorized! Try calling `slack authorize`") JSON.parse(File.read(cli_app.authorization_path))['bot']['bot_access_token'] end end
cache_path()
click to toggle source
# File lib/slack_cli.rb, line 82 def cache_path @cache_path ||= begin File.join(cli_app.config_path, 'cache').tap do |t| FileUtils.mkdir_p(t) end end end
channels(refresh: false)
click to toggle source
# File lib/slack_cli.rb, line 130 def channels(refresh: false) slack_get(path: 'channels.list', refresh: refresh) end
channels_by_name()
click to toggle source
# File lib/slack_cli.rb, line 134 def channels_by_name channels['channels'].each_with_object({}) do |row, agg| agg[row['name']] = row end end
clear_cache()
click to toggle source
# File lib/slack_cli.rb, line 36 def clear_cache FileUtils.rm_rf cache_path end
cli_app()
click to toggle source
# File lib/slack_cli.rb, line 48 def cli_app @cli_app ||= SlackCLI::Server.new end
log_string_io()
click to toggle source
# File lib/slack_cli.rb, line 32 def log_string_io @log_string_io ||= StringIO.new end
post(channel:, text:)
click to toggle source
# File lib/slack_cli.rb, line 150 def post(channel:, text:) place = users_by_name[channel] || channels_by_name[channel] if place slack_post( path: 'chat.postMessage', json: { channel: place['id'], text: text, as_user: true} ) else raise "No channel or user found for `#{channel}`" end end
rtm_connect(**params, &block)
click to toggle source
# File lib/slack_cli.rb, line 162 def rtm_connect(**params, &block) resp = slack_get(path: 'rtm.connect', refresh: true) $stderr.puts resp.to_json ws = WebSocket::EventMachine::Client.connect(uri: resp['url']) ws.onopen do $stderr.puts({status: 'connected'}.to_json) # ws.send(connect_payload.to_json) end ws.onmessage do |msg, type| if block block.call(JSON.parse(msg)) else puts msg end end ws.onclose do |code, reason| $stderr.puts({status: 'disconnected', code: code, reason: reason}.to_json) rtm_connect end end
slack_get(path:, refresh: true)
click to toggle source
# File lib/slack_cli.rb, line 90 def slack_get(path:, refresh: true) url = "https://slack.com/api/#{path}" cache_key = Digest::MD5.hexdigest(url).gsub('=', '') full_cache_path = File.join(cache_path, cache_key) save = false data = if refresh == true || !File.file?(full_cache_path) save = true open( url, 'Authorization' => "Bearer #{access_token}", 'Content-type' => 'application/json' ).read else File.read(full_cache_path) end json = JSON.parse(data) if (error = json['error']) raise Error, json.inspect end File.open(full_cache_path, 'w'){|f| f << data} if save json end
slack_post(path:, json:)
click to toggle source
# File lib/slack_cli.rb, line 114 def slack_post(path:, json:) uri = URI.parse("https://slack.com/api/#{path}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.set_debug_output($stderr) if $DEBUG header = { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } request = Net::HTTP::Post.new(uri, header) request.body = json.to_json resp = http.request(request) JSON.parse(resp.body) end
string_logger()
click to toggle source
# File lib/slack_cli.rb, line 28 def string_logger @logger ||= Logger.new(log_string_io) end
users(refresh: false)
click to toggle source
# File lib/slack_cli.rb, line 140 def users(refresh: false) slack_get(path: 'users.list', refresh: refresh) end
users_by_name()
click to toggle source
# File lib/slack_cli.rb, line 144 def users_by_name users['members'].each_with_object({}) do |row, agg| agg[row['name']] = row end end
webrick_options()
click to toggle source
# File lib/slack_cli.rb, line 40 def webrick_options { Port: 65187, Logger: string_logger, AccessLog: string_logger } end