Module: SlackBot

Included in:
Bot
Defined in:
lib/slack/slack.rb,
lib/slack/helpers.rb,
lib/slack/session.rb,
lib/slack/wrappers/bot.rb,
lib/slack/wrappers/user.rb,
lib/slack/wrappers/channel.rb,
lib/slack/wrappers/message.rb,
lib/slack/matchers/matcher_group.rb

Defined Under Namespace

Modules: Ext Classes: Bot, Channel, Matcher, MatcherGroup, Message, Session, User

Constant Summary collapse

SLACK_AUTH_URL =
'https://slack.com/api/rtm.start?token='

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auth_urlObject

Returns the value of attribute auth_url.



18
19
20
# File 'lib/slack/slack.rb', line 18

def auth_url
  @auth_url
end

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



15
16
17
# File 'lib/slack/slack.rb', line 15

def debug=(value)
  @debug = value
end

#sessionObject (readonly)

Returns the value of attribute session.



17
18
19
# File 'lib/slack/slack.rb', line 17

def session
  @session
end

#socketObject

Returns the value of attribute socket.



14
15
16
# File 'lib/slack/slack.rb', line 14

def socket
  @socket
end

#team_infoObject (readonly)

Returns the value of attribute team_info.



16
17
18
# File 'lib/slack/slack.rb', line 16

def team_info
  @team_info
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/slack/helpers.rb', line 35

def [](key)
  @team_info[key]
end

#channel(id) ⇒ Object



6
7
8
# File 'lib/slack/helpers.rb', line 6

def channel(id)
  channels[id]
end

#channelsObject



2
3
4
# File 'lib/slack/helpers.rb', line 2

def channels
  @channels ||= load_channels
end

#debug?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/slack/slack.rb', line 115

def debug?
  @debug
end

#get_urlObject



38
39
40
41
42
43
44
45
46
47
# 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

#initialize(auth_key, options = {}) ⇒ Object



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

def initialize(auth_key, options={})
  setup(options)
  @key = auth_key.strip
end

#log(type, message) ⇒ Object



109
110
111
112
113
# File 'lib/slack/slack.rb', line 109

def log(type, message)
  if debug?
    puts "#{type}: #{message}"
  end
end

#meObject



31
32
33
# File 'lib/slack/helpers.rb', line 31

def me
  @me ||= user(@team_info['self']['id'])
end

#post(channel, message) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



105
106
107
# File 'lib/slack/slack.rb', line 105

def reply_to(msg, text)
  post(msg['channel'], text)
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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 = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



27
28
29
# File 'lib/slack/helpers.rb', line 27

def user(id)
  users[id]
end

#user_channel(user) ⇒ Object



14
15
16
17
18
19
20
21
# 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_channelsObject



10
11
12
# File 'lib/slack/helpers.rb', line 10

def user_channels
  @user_channels ||= load_user_channels
end

#usersObject



23
24
25
# File 'lib/slack/helpers.rb', line 23

def users
  @users ||= load_users
end