Sha256: c366361b78dc7f91174b61a26617956bd3ac8a2601f0c76fb381e497274495f6

Contents?: true

Size: 960 Bytes

Versions: 2

Compression:

Stored size: 960 Bytes

Contents

require "slack-ruby-client"

module Socrates
  module Bots
    class SlackBot
      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::SlackAdapter.new(@slack_client)
        @dispatcher   = Core::Dispatcher.new(adapter: @adapter, state_factory: state_factory)
      end

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

          # When first connecting, Slack may resend the last message. Ignore it...
          next if data.reply_to.present?

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

        @slack_client.start!
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
socrates-0.1.3 lib/socrates/bots/slack_bot.rb
socrates-0.1.2 lib/socrates/bots/slack_bot.rb