class SlackBot::Client

Attributes

commands[RW]
slack_realtime_client[RW]
slack_web_client[RW]

Public Class Methods

new() click to toggle source
# File lib/simple_slack_bot/client.rb, line 9
def initialize
  @commands = []
end

Public Instance Methods

add_command(regex) { |data| ... } click to toggle source
# File lib/simple_slack_bot/client.rb, line 13
def add_command(regex)
  command = Command.new(self)
  command.regex = regex
  command.action = -> (data) { yield(data) }
  @commands << command
end
config() click to toggle source
# File lib/simple_slack_bot/client.rb, line 34
def config
  Config
end
configure() { |Config| ... } click to toggle source
# File lib/simple_slack_bot/client.rb, line 38
def configure
  block_given? ? yield(Config) : Config
end
message(channel, text) click to toggle source
# File lib/simple_slack_bot/client.rb, line 46
def message(channel, text)
  @slack_realtime_client.message channel: channel, text: text
end
start!() click to toggle source
# File lib/simple_slack_bot/client.rb, line 20
def start!
  if config.token.nil?
    puts 'You must setting a token.'
    return
  end

  slack_init
  message_event_init

  EM.run do
    @slack_realtime_client.start!
  end
end
web_message(channel, text) click to toggle source
# File lib/simple_slack_bot/client.rb, line 42
def web_message(channel, text)
  @slack_web_client.chat_postMessage(channel: channel, text: text, as_user: true)
end

Protected Instance Methods

message_event_init() click to toggle source
# File lib/simple_slack_bot/client.rb, line 65
def message_event_init
  client = @slack_realtime_client
  client.on :message do |data|
    File.open('bot.log', 'a') { |file| file.write(data.to_s + "\n") }
    puts data if config.debug.eql?(true)

    case data['subtype']
    when 'channel_join' then
      client.message channel: data['channel'], text: config.join_message
    end

    @commands.each do |command|
      command.execute(data) if command.match?(data['text'])
    end
  end
end
slack_init() click to toggle source
# File lib/simple_slack_bot/client.rb, line 52
def slack_init
  Slack.configure do |config|
    config.token = self.config.token
  end

  Slack::RealTime.configure do |config|
    config.concurrency = Slack::RealTime::Concurrency::Eventmachine
  end

  @slack_web_client = Slack::Web::Client.new
  @slack_realtime_client = Slack::RealTime::Client.new
end