Class: Nanobot::Channels::Slack

Inherits:
BaseChannel show all
Defined in:
lib/nanobot/channels/slack.rb

Overview

Slack channel integration using Socket Mode via the slack-ruby-client gem. Supports DMs, group mentions, and configurable access control policies.

Instance Attribute Summary

Attributes inherited from BaseChannel

#bus, #config, #logger, #name

Instance Method Summary collapse

Methods inherited from BaseChannel

#initialize, #running?

Constructor Details

This class inherits a constructor from Nanobot::Channels::BaseChannel

Instance Method Details

#allowed?(_sender_id) ⇒ Boolean

Always returns true; Slack uses its own ACL via allowed_slack? instead.

Returns:

  • (Boolean)


58
59
60
# File 'lib/nanobot/channels/slack.rb', line 58

def allowed?(_sender_id)
  true
end

#send(message) ⇒ Object

Send a reply via Slack, threading in channels but not in DMs.

Parameters:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nanobot/channels/slack.rb', line 42

def send(message)
  return unless @web_client

  slack_meta = message.['slack'] || {}
  thread_ts = slack_meta['thread_ts']
  channel_type = slack_meta['channel_type']
  use_thread = thread_ts && channel_type != 'im'

  @web_client.chat_postMessage(
    channel: message.chat_id,
    text: message.content || '',
    thread_ts: use_thread ? thread_ts : nil
  )
end

#start ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nanobot/channels/slack.rb', line 14

def start
  @running = true
  @bot_user_id = nil

  ::Slack.configure do |c|
    c.token = @config.bot_token
  end

  @web_client = ::Slack::Web::Client.new
  @socket_client = ::Slack::RealTime::Client.new(token: @config.app_token)

  resolve_bot_user_id

  @socket_client.on :message do |data|
    handle_slack_message(data)
  end

  @logger.info 'Slack bot connecting via Socket Mode...'
  @socket_client.start!
end

#stop ⇒ Object



35
36
37
38
# File 'lib/nanobot/channels/slack.rb', line 35

def stop
  @running = false
  @socket_client&.stop!
end