Sha256: a930fa1b8e684147fe5ca2c4d222ef7b234d421c7051d9d7e9d3249130cc1ebf

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require 'slack-ruby-client'

using Rodbot::Refinements

module Rodbot
  class Plugins
    class Slack
      class Relay < Rodbot::Relay
        include Rodbot::Memoize

        def loops
          ::Slack.configure do |config|
            config.token = access_token
          end
          [method(:read_loop), method(:write_loop)]
        end

        private

        def access_token
          Rodbot.config(:plugin, :slack, :access_token)
        rescue => error
          raise Rodbot::PluginError.new("invalid access_token", error.message)
        end

        memoize def client
          ::Slack::RealTime::Client.new
        end

        def channel_id
          Rodbot.config(:plugin, :slack, :channel_id)
        rescue => error
          raise Rodbot::PluginError.new("invalid channel_id", error.message)
        end

        def write_loop
          server = TCPServer.new(*bind)
          loop do
            Thread.start(server.accept) do |remote|
              body = remote.gets("\x04")
              remote.close
              body.force_encoding('UTF-8')
              client.web_client.chat_postMessage(channel: channel_id, text: body, as_user: true)
            end
          end
        end

        def read_loop
          client.on :message do |message|
            on_message(message) if message.channel == channel_id
          end
          client.start!
        end

        def on_message(message)
          if message.text.start_with?('!')
            md = 'pong' if message.text == '!ping'
            md ||= reply_to(message)
            client.web_client.chat_postMessage(channel: message.channel, text: md, as_user: true)
          end
        end

        def reply_to(message)
          command(*message.text[1..].split(/\s+/, 2)).
            psub(placeholders(message.user))
        end

        def placeholders(sender)
          {
            sender: "<@#{sender}>"
          }
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rodbot-0.3.1 lib/rodbot/plugins/slack/relay.rb
rodbot-0.3.0 lib/rodbot/plugins/slack/relay.rb