class Voltron::Defender::SlackAdapter

Attributes

asker[RW]
questioned[RW]

Public Class Methods

new() click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 10
def initialize
  client.on(:message) { |message| message_received(message) }
  client.on(:close) { socket_closed }
end

Public Instance Methods

ask(question, asker, **args) click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 35
def ask(question, asker, **args)
  @questioned = true
  @asker = asker
  message(question, args)
end
attach(title=nil, text=nil, color=nil, **args) click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 46
def attach(title=nil, text=nil, color=nil, **args)
  payload = { pretext: title, color: color, text: text, mrkdwn_in: ['text', 'pretext'] }.compact.merge(args)
  client.web_client.chat_postMessage channel: channel, attachments: [payload].to_json, as_user: true
end
client() click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 71
def client
  @client ||= ::Slack::RealTime::Client.new(token: Voltron.config.defender.slack_api_token, timeout: 10, open_timeout: 15)
end
commander() click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 59
def commander
  @commander ||= ::Voltron::Defender::Commander.new
end
is_command?(text) click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 67
def is_command?(text)
  text.to_s.strip.starts_with?('.')
end
message(text, **args) click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 51
def message(text, **args)
  client.web_client.chat_postMessage channel: channel, text: (text % args), mrkdwn: true, as_user: true
end
message_received(message) click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 15
def message_received(message)
  # The bot is the user that posted the message, ignore whatever it said
  return if client.self.id == message.user

  nvm if is_command?(message.text)

  if waiting?
    client.typing channel: channel
    asker.answer(message.text, self)
    nvm
  elsif commander.matches?(message.text)
    client.typing channel: channel
    commander.processor(message.text).respond_with(self)
  end
end
nvm() click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 41
def nvm
  @questioned = false
  @asker = nil
end
socket_closed() click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 31
def socket_closed
  message "Connection has been closed, I'm no longer listening for commands"
end
upload(name, title, content) click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 55
def upload(name, title, content)
  client.web_client.files_upload channels: channel, filename: name, title: title, content: content, as_user: true
end
waiting?() click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 63
def waiting?
  questioned == true
end

Private Instance Methods

channel() click to toggle source
# File lib/voltron/defender/slack_adapter.rb, line 77
def channel
  '#' + Voltron.config.defender.slack_channel.to_s.gsub(/[^a-z0-9\-]/i, '')
end