Class: Socrates::Bots::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/socrates/bots/slack.rb,
lib/socrates/bots/slack/ping.rb

Defined Under Namespace

Classes: Ping

Instance Method Summary collapse

Constructor Details

#initialize(state_factory:) ⇒ Slack

Returns a new instance of Slack.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/socrates/bots/slack.rb', line 9

def initialize(state_factory:)
  ::Slack.configure do |config|
    config.token        = ENV["SLACK_API_TOKEN"]
    config.logger       = Logger.new($stdout)
    config.logger.level = Logger::INFO

    raise "Missing ENV['SLACK_API_TOKEN']!" unless config.token
  end

  @slack_client = ::Slack::RealTime::Client.new
  @adapter      = Adapters::Slack.new(@slack_client)
  @dispatcher   = Core::Dispatcher.new(adapter: @adapter, state_factory: state_factory)
end

Instance Method Details

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/socrates/bots/slack.rb', line 23

def start
  @slack_client.on :message do |data|
    # puts "> #{data}"

    # Slack sends us messages from ourslves sometimes, this skips them.
    next if @slack_client.self.id == data.user

    @dispatcher.dispatch(data.text, context: data)
  end

  @slack_client.start_async

  ping = Ping.new
  loop do
    sleep 60
    next if ping.alive?

    alert "Slack RTC has gone offline; restarting it"
    @slack_client.stop! if @slack_client.started?
    sleep 1 # give the websocket a chance to completely disconnect
    @slack_client.start_async
  end
end