Class: RailsConsoleAi::SlackBot

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_ai/slack_bot.rb

Constant Summary collapse

PING_INTERVAL =

seconds — send ping if no data received

30
PONG_TIMEOUT =

seconds — reconnect if no pong after ping

60

Instance Method Summary collapse

Constructor Details

#initializeSlackBot

Returns a new instance of SlackBot.

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_console_ai/slack_bot.rb', line 17

def initialize
  @bot_token = RailsConsoleAi.configuration.slack_bot_token || ENV['SLACK_BOT_TOKEN']
  @app_token = RailsConsoleAi.configuration.slack_app_token || ENV['SLACK_APP_TOKEN']
  @channel_ids = resolve_channel_ids

  raise ConfigurationError, "SLACK_BOT_TOKEN is required" unless @bot_token
  raise ConfigurationError, "SLACK_APP_TOKEN is required (Socket Mode)" unless @app_token
  raise ConfigurationError, "slack_allowed_usernames must be configured (e.g. ['alice'] or 'ALL')" unless RailsConsoleAi.configuration.slack_allowed_usernames

  @bot_user_id = nil
  @sessions = {}       # thread_ts → { channel:, engine:, thread: }
  @user_cache = {}     # slack user_id → display_name
  @mutex = Mutex.new
end

Instance Method Details

#startObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rails_console_ai/slack_bot.rb', line 32

def start
  $stdout.sync = true
  $stderr.sync = true
  $stdout = RailsConsoleAi::PrefixedIO.new($stdout) unless $stdout.is_a?(RailsConsoleAi::PrefixedIO)
  $stderr = RailsConsoleAi::PrefixedIO.new($stderr) unless $stderr.is_a?(RailsConsoleAi::PrefixedIO)

  # Eager load the Rails app so class-level initializers (e.g. Secret.get)
  # run before safety guards are active during user code execution.
  if defined?(Rails) && Rails.application.respond_to?(:eager_load!)
    puts "Eager loading application..."
    Rails.application.eager_load!
  end

  @bot_user_id = slack_api("auth.test", token: @bot_token).dig("user_id")
  log_startup

  loop do
    run_socket_mode
    puts "Reconnecting in 5s..."
    sleep 5
  end
rescue Interrupt
  puts "\nSlackBot shutting down."
end