Class: BotMob::Networks::Slack::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/bot_mob/networks/slack/connection.rb

Overview

# BotMob::Networks::Slack::Connection

Handles all the connection details between a bot and Slack

Instance Attribute Summary collapse

Attributes inherited from Connection

#options

Instance Method Summary collapse

Methods inherited from Connection

setup

Constructor Details

#initialize(bot, options = {}) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
# File 'lib/bot_mob/networks/slack/connection.rb', line 10

def initialize(bot, options = {})
  @bot = bot
  @token = options[:token]
  raise BotMob::NetworkConnectionError, 'Slack connection requires a :token parameter' if @token.nil?
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



8
9
10
# File 'lib/bot_mob/networks/slack/connection.rb', line 8

def bot
  @bot
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/bot_mob/networks/slack/connection.rb', line 8

def token
  @token
end

Instance Method Details

#clientObject



27
28
29
30
31
32
33
# File 'lib/bot_mob/networks/slack/connection.rb', line 27

def client
  @client ||= begin
    client = ::Slack::Web::Client.new(token: token)
    client.auth_test
    client
  end
end

#deliver(outbound_message, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/bot_mob/networks/slack/connection.rb', line 16

def deliver(outbound_message, options = {})
  BotMob.logger.info("  Sending to Slack: \"#{options.inspect}\"\n")
  BotMob.logger.info("  Text: \"#{outbound_message.body.inspect}\"\n")

  client.chat_postMessage(
    channel: outbound_message.channel,
    text: outbound_message.body,
    as_user: true
  )
end