Sha256: b39100d570339e21fcd49907eb981d3c03ef1fa3be45cbe3265b3bbfc5e243f7

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

require "slack-ruby-client"

require "socrates/adapters/slack"
require "socrates/core/dispatcher"

module Socrates
  module Bots
    class Slack
      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

      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

      private

      def alert(message)
        Socrates.config.logger.warn(message)
        Socrates.config.warn_handler.call(message)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
socrates-0.1.25 lib/socrates/bots/slack.rb
socrates-0.1.24 lib/socrates/bots/slack.rb
socrates-0.1.23 lib/socrates/bots/slack.rb
socrates-0.1.22 lib/socrates/bots/slack.rb
socrates-0.1.21 lib/socrates/bots/slack.rb
socrates-0.1.20 lib/socrates/bots/slack.rb