module SlackBot

Constants

SLACK_AUTH_URL

Attributes

auth_url[RW]
debug[W]
session[R]
socket[RW]
team_info[R]

Public Class Methods

new(auth_key, options={}) click to toggle source
# File lib/slack/slack.rb, line 20
def initialize(auth_key, options={})
  setup(options)
  @key = auth_key.strip
end

Public Instance Methods

[](key) click to toggle source
# File lib/slack/helpers.rb, line 35
def [](key)
  @team_info[key]
end
channel(id) click to toggle source
# File lib/slack/helpers.rb, line 6
def channel(id)
  channels[id]
end
channels() click to toggle source
# File lib/slack/helpers.rb, line 2
def channels
  @channels ||= load_channels
end
debug?() click to toggle source
# File lib/slack/slack.rb, line 115
def debug?
  @debug
end
get_url() click to toggle source
# File lib/slack/slack.rb, line 38
def get_url
  data = Net::HTTP.get(URI(@auth_url + @key))
  json = JSON.parse(data)
  @team_info = json
  if json['ok']
    return json['url']
  else
    raise "ERROR: #{json.to_s}"
  end
end
log(type, message) click to toggle source
# File lib/slack/slack.rb, line 109
def log(type, message)
  if debug?
    puts "#{type}: #{message}"
  end
end
me() click to toggle source
# File lib/slack/helpers.rb, line 31
def me
  @me ||= user(@team_info['self']['id'])
end
post(channel, message) click to toggle source
# File lib/slack/slack.rb, line 88
def post(channel, message)
  if channel.is_a? String
    chan = channel
  elsif channel.is_a? Channel
    chan = channel.id
  else
    raise "Not a valid channel: #{channel}"
  end
  data = {
    id: 1,
    type: 'message',
    channel: chan,
    text: message.to_s
  }
  @socket.send data.to_json
end
reply_to(msg, text) click to toggle source
# File lib/slack/slack.rb, line 105
def reply_to(msg, text)
  post(msg['channel'], text)
end
run() click to toggle source
# File lib/slack/slack.rb, line 49
def run
  url = get_url()
  EM.run do
    @socket = ws = Faye::WebSocket::Client.new(url)

    ws.on :open do |event|
      log(:open, event.to_s)
      create_session(team_info['team']['id'])
      hook(:opened)
    end

    ws.on :message do |event|
      log(:action, event.data)
      begin
        json = JSON.parse(event.data)
        type = json.delete('type')
        if type
          message = Message.new(json, self)
          unless type == "message" && message.user == me
            hook(type, message)
          end
        else
          hook(:unknown, json)
        end
      rescue JSON::ParserError => e
        log(:error, e.message)
      end
      
    end

    ws.on :close do |event|
      log(:close, "#{event.code} #{event.reason}")
      @socket = ws = nil
      hook(:closed)
      EM.stop
    end
  end
end
setup(options={}) click to toggle source
# File lib/slack/slack.rb, line 25
def setup(options={})
  @debug = options[:log]
  @matchers = Hash.new
  if options[:session]
    @session_type = options[:session].delete(:use)
    @session_args = options[:session]
  else
    @session_type = Session
    @session_args = {}
  end
  @auth_url = SLACK_AUTH_URL
end
user(id) click to toggle source
# File lib/slack/helpers.rb, line 27
def user(id)
  users[id]
end
user_channel(user) click to toggle source
# File lib/slack/helpers.rb, line 14
def user_channel(user)
  if user.is_a? User
    id = user.id
  else
    id = user
  end
  user_channels[id]
end
user_channels() click to toggle source
# File lib/slack/helpers.rb, line 10
def user_channels
  @user_channels ||= load_user_channels
end
users() click to toggle source
# File lib/slack/helpers.rb, line 23
def users
  @users ||= load_users
end

Private Instance Methods

create_session(team_id) click to toggle source
# File lib/slack/slack.rb, line 143
def create_session(team_id)
  @session = @session_type.new(team_id, @session_args)
end
hook(action, *args) click to toggle source
# File lib/slack/slack.rb, line 120
def hook(action, *args)
  if self.respond_to? "#{action}_matcher"
    unless @matchers.has_key? action
      matcher_group = MatcherGroup.new(action)
      self.send("#{action}_matcher", matcher_group)
      @matchers[action] = matcher_group
    end
    begin
      @matchers[action].respond_for(args.first)
    rescue Exception => e
      puts e.message
      puts e.backtrace.join "\n"
    end
  elsif self.respond_to? action
    begin
      send(action, *args)
    rescue Exception => e
      puts e.message
      puts e.backtrace.join "\n"
    end
  end
end
load_channels() click to toggle source
# File lib/slack/helpers.rb, line 40
def load_channels
  channels = Hash.new
  @team_info['channels'].each do |chan|
    channels[chan['id']] = Channel.new chan, self
  end
  channels
end
load_user_channels() click to toggle source
# File lib/slack/helpers.rb, line 48
def load_user_channels
  channels = Hash.new
  @team_info['ims'].each do |chan|
    channels[chan['user']] = Channel.new chan, self
  end
  channels
end
load_users() click to toggle source
# File lib/slack/helpers.rb, line 56
def load_users
  users = Hash.new
  (@team_info['users'] + @team_info['bots']).map do |info| 
    users[info['id']] = User.new info, self
  end
  users
end