<commit_before>require "houston/slack/channel"
require "houston/slack/conversation"
require "houston/slack/driver"
require "houston/slack/event"
require "houston/slack/listener"
require "houston/slack/user"
require "houston/slack/errors"
require "faraday"

module Houston
  module Slack
    class Connection
      EVENT_MESSAGE = "message".freeze
      EVENT_GROUP_JOINED = "group_joined".freeze
      EVENT_USER_JOINED = "team_join".freeze
      ME = "@houston".freeze
      
      def initialize
        @user_ids_dm_ids = {}
        @users_by_id = {}
        @user_id_by_name = {}
        @groups_by_id = {}
        @group_id_by_name = {}
        @channels_by_id = {}
        @channel_id_by_name = {}
      end
      
      
      
      def send_message(message, options={})
        channel = options.fetch(:channel) { raise ArgumentError, "Missing parameter :channel" }
        attachments = Array(options[:attachments])
        params = {
          channel: to_channel_id(channel),
          text: message,
          as_user: true, # post as the authenticated user (rather than as slackbot)
          link_names: 1} # find and link channel names and user names
        params.merge!(attachments: MultiJson.dump(attachments)) if attachments.any?
        api("chat.postMessage", params)
      end
      
      
      def listen!
        Houston.daemonize "slack" do
          begin
            @connected_at = Time.now
            @listening = true
            __listen

          rescue Errno::EPIPE
            # We got disconnected retry
            Rails.logger.warn "\e[31m[daemon:slack] Disconnected from Slack; retrying\e[0m"
            Houston.observer.fire "daemon:#{name}:reconnecting"
            sleep 5
            retry
          end
        end

        @listening = false
      end
      
      attr_reader :connected_at
      
      def listening?
        @listening<commit_msg>[skip] Listed the channels Houston can communicate on (5m)<commit_after>require "houston/slack/channel"
require "houston/slack/conversation"
require "houston/slack/driver"
require "houston/slack/event"
require "houston/slack/listener"
require "houston/slack/user"
require "houston/slack/errors"
require "faraday"

module Houston
  module Slack
    class Connection
      EVENT_MESSAGE = "message".freeze
      EVENT_GROUP_JOINED = "group_joined".freeze
      EVENT_USER_JOINED = "team_join".freeze
      ME = "@houston".freeze
      
      def initialize
        @user_ids_dm_ids = {}
        @users_by_id = {}
        @user_id_by_name = {}
        @groups_by_id = {}
        @group_id_by_name = {}
        @channels_by_id = {}
        @channel_id_by_name = {}
      end
      
      
      
      def send_message(message, options={})
        channel = options.fetch(:channel) { raise ArgumentError, "Missing parameter :channel" }
        attachments = Array(options[:attachments])
        params = {
          channel: to_channel_id(channel),
          text: message,
          as_user: true, # post as the authenticated user (rather than as slackbot)
          link_names: 1} # find and link channel names and user names
        params.merge!(attachments: MultiJson.dump(attachments)) if attachments.any?
        api("chat.postMessage", params)
      end
      
      
      def listen!
        Houston.daemonize "slack" do
          begin
            @connected_at = Time.now
            @listening = true
            __listen

          rescue Errno::EPIPE
            # We got disconnected retry
            Rails.logger.warn "\e[31m[daemon:slack] Disconnected from Slack; retrying\e[0m"
            Houston.observer.fire "daemon:#{name}:reconnecting"
            sleep 5
            retry
          end
        end

        @listening = false
      end
      
      attr_reader :connected_at
      
      def listening?
        @listening
      end
      
      def channels
        user_id_by_name.keys + group_id_by_name.keys + channel_id_by_name.keys
{'fraction_non_alphanumeric': 0.08863636363636364, 'fraction_numerical': 0.002777777777777778, 'mean_word_length': 2.1066666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6488604', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1166, 'n_words': 259}