class Pepito::Adapters::Slack

Adapter for slack

Attributes

client[R]

Slack RealTime client @return [Slack::RealTime::Client]

Public Class Methods

configs() click to toggle source

Configs needed for Slack @return [Array<Hash>]

# File lib/pepito/adapters/slack.rb, line 16
def configs
  [
    {
      name: 'SLACK_API_TOKEN',
      required: true
    }
  ]
end
new(robot, config) click to toggle source
Calls superclass method
# File lib/pepito/adapters/slack.rb, line 30
def initialize(robot, config)
  super(robot, config)

  configure_slack_api

  @client = ::Slack::RealTime::Client.new
  @client.web_client.auth_test

  slack_on_message
end

Public Instance Methods

run() click to toggle source

Runs the slack client @return [void]

# File lib/pepito/adapters/slack.rb, line 43
def run
  @client.start!
end
send_messages(target, strings) click to toggle source

Send messages to slack. @param target [Pepito::Source] Where to send the message to. @param strings [Array<String>] Strings to output. @return [void]

# File lib/pepito/adapters/slack.rb, line 51
def send_messages(target, strings)
  message = ''
  strings.each do |s|
    message = message + s + "\n"
  end
  @client.message(channel: target.room, text: message)
end
stop() click to toggle source

Stops the client @return [void]

# File lib/pepito/adapters/slack.rb, line 68
def stop
  @client.stop!
end
topic(target, topic) click to toggle source

Set topic @param topic [String] new topic string @return [void]

# File lib/pepito/adapters/slack.rb, line 62
def topic(target, topic)
  @client.web_client.channels_setTopic(channel: target.room, topic: topic)
end

Private Instance Methods

configure_slack_api() click to toggle source

Configures the slack API @return [void]

# File lib/pepito/adapters/slack.rb, line 76
def configure_slack_api
  ::Slack.configure do |c|
    c.token = @config['SLACK_API_TOKEN']
  end

  ::Slack::RealTime.configure do |c|
    c.concurrency = ::Slack::RealTime::Concurrency::Eventmachine
  end
end
slack_on_message() click to toggle source

Behavior for slack when it receives a message @return [void]

# File lib/pepito/adapters/slack.rb, line 88
def slack_on_message
  @client.on :message do |data|
    unless data['reply_to']
      source = Source.new(self, data['user'], data['channel'], false)
      @robot.receive_message(Message.new(@robot, source, data['text']))
    end
  end
end