Sha256: 70bcbe88e2a3fb44c097b660112889fb363001a4668ffe8c333aa0945f6fa797

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 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
        reply_to_messages = {}

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

          if data.reply_to.present?
            # Stash this message away because we may need it later.
            reply_to_messages[data.channel] = data.text
          end

          # Only dispatch the message if it does not match a previous reply_to message for the channel.
          if reply_to_messages[data.channel] != data.text
            @dispatcher.dispatch(data.text, context: data)
          end
        end

        @slack_client.start!
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
socrates-0.1.12 lib/socrates/bots/slack.rb
socrates-0.1.11 lib/socrates/bots/slack.rb
socrates-0.1.10 lib/socrates/bots/slack.rb
socrates-0.1.9 lib/socrates/bots/slack.rb
socrates-0.1.8 lib/socrates/bots/slack.rb