Class: Qbot::Adapter::Slack

Inherits:
Driver
  • Object
show all
Defined in:
lib/qbot/adapter/slack.rb

Constant Summary collapse

SLACK_API_URL =
'https://slack.com/api'

Instance Method Summary collapse

Methods inherited from Driver

build, inherited, #run

Constructor Details

#initialize(api_token: nil) ⇒ Slack

Returns a new instance of Slack.



14
15
16
17
18
# File 'lib/qbot/adapter/slack.rb', line 14

def initialize(api_token: nil)
  @server = URI.join(SLACK_API_URL, '/').to_s

  access_token(api_token || ENV['QBOT_SLACK_API_TOKEN'])
end

Instance Method Details

#access_token(token) ⇒ Object



20
21
22
# File 'lib/qbot/adapter/slack.rb', line 20

def access_token(token)
  @token = token
end

#closeObject



28
29
30
# File 'lib/qbot/adapter/slack.rb', line 28

def close
  EM.stop
end

#listen(&block) ⇒ Object



24
25
26
# File 'lib/qbot/adapter/slack.rb', line 24

def listen(&block)
  EM.run { start_connection(&block) }
end

#post(text, **options) ⇒ Object



32
33
34
35
# File 'lib/qbot/adapter/slack.rb', line 32

def post(text, **options)
  resp = api_call(:post, "/chat.postMessage", options.merge(text: text))
  Qbot.app.logger.info("#{self.class} - Post message: #{resp.status} - '#{text}'")
end

#reply_to(message, text, **options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/qbot/adapter/slack.rb', line 37

def reply_to(message, text, **options)
  if options[:channel_id]
    channel_id = options[:channel_id]
  elsif options[:channel_name]
    channel = channel(options[:channel_name])
    channel_id = channel['id'] if channel
  end

  channel_id ||= message.data['channel'] if message
  return unless channel_id

  post(text, **options.merge(channel: channel_id))
end