Sha256: df1fe7779f63ad730e9d936685dd755147865d48a8765bbb68b5a4da0a1b9eaa

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Socrates
  module Adapters
    class SlackAdapter
      def initialize(real_time_client)
        @real_time_client = real_time_client
      end

      def client_id_from_context(context)
        context&.user
      end

      def send_message(message, context:)
        @real_time_client.message(text: message, channel: context.channel)
      end

      def send_direct_message(message, user, *)
        user = user.id if user.respond_to?(:id)

        im_channel = lookup_im_channel(user)

        @real_time_client.message(text: message, channel: im_channel)
      end

      def users_list
        client = @real_time_client.web_client
        client.users_list
      end

      def lookup_email(context:)
        client = @real_time_client.web_client
        client.users_info(user: context.user)
      end

      private

      def lookup_im_channel(user)
        im = @real_time_client.ims.values.find { |i| i.user == user }

        return im if im.present?

        # Start a new conversation with this user.
        response = @real_time_client.web_client.im_open(user: user.id)
        response.channel.id
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
socrates-0.1.4 lib/socrates/adapters/slack_adapter.rb