Module: SlackBot
- Included in:
- Bot
- Defined in:
- lib/slack/bot.rb,
lib/slack/user.rb,
lib/slack/slack.rb,
lib/slack/message.rb,
lib/slack/matchers/matcher_group.rb
Defined Under Namespace
Classes: Bot, Matcher, MatcherGroup, Message, User
Constant Summary
collapse
- DEFAULT_ATTRS =
[
:id,
:team_id,
:name,
:deleted,
:status,
:color,
:real_name,
:tz,
:tz_label,
:tz_offset,
:profile,
:is_admin,
:is_owner,
:is_primary_owner,
:is_restricted,
:is_ultra_restricted,
:is_bot,
:presence
]
- SLACK_AUTH_URL =
'https://slack.com/api/rtm.start?token='
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#debug=(value) ⇒ Object
12
13
14
|
# File 'lib/slack/slack.rb', line 12
def debug=(value)
@debug = value
end
|
#socket ⇒ Object
Returns the value of attribute socket.
11
12
13
|
# File 'lib/slack/slack.rb', line 11
def socket
@socket
end
|
#team_info ⇒ Object
Returns the value of attribute team_info.
13
14
15
|
# File 'lib/slack/slack.rb', line 13
def team_info
@team_info
end
|
Instance Method Details
#channel(name) ⇒ Object
99
100
101
|
# File 'lib/slack/slack.rb', line 99
def channel(id)
@team_info['channels'].select { |ch| ch['id'] == id }.first rescue nil
end
|
#debug? ⇒ Boolean
131
132
133
|
# File 'lib/slack/slack.rb', line 131
def debug?
@debug
end
|
#get_url ⇒ Object
21
22
23
24
25
26
|
# File 'lib/slack/slack.rb', line 21
def get_url
data = Net::HTTP.get(URI(SLACK_AUTH_URL + @key))
json = JSON.parse(data)
@team_info = json
json['url']
end
|
#hook(action, *args) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/slack/slack.rb', line 62
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
|
#initialize(auth_key, options = {}) ⇒ Object
15
16
17
18
19
|
# File 'lib/slack/slack.rb', line 15
def initialize(auth_key, options={})
@debug = options[:log]
@key = auth_key.strip
@matchers = Hash.new
end
|
#load_users ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/slack/slack.rb', line 103
def load_users
users = Hash.new
(@team_info['users'] + @team_info['bots']).map do |info|
users[info['id']] = User.new info
end
users
end
|
#log(type, message) ⇒ Object
125
126
127
128
129
|
# File 'lib/slack/slack.rb', line 125
def log(type, message)
if debug?
puts "#{type}: #{message}"
end
end
|
#post(channel, message) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/slack/slack.rb', line 85
def post(channel, message)
data = {
id: 1,
type: 'message',
channel: channel,
text: message
}
@socket.send data.to_json
end
|
#reply_to(data, message) ⇒ Object
95
96
97
|
# File 'lib/slack/slack.rb', line 95
def reply_to(data, message)
post(data['channel'], message)
end
|
#run ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/slack/slack.rb', line 28
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)
hook(:opened)
end
ws.on :message do |event|
log(:action, event.data)
begin
json = JSON.parse(event.data)
type = json.delete('type')
if type
hook(type, Message.new(json, self))
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)
end
end
end
|
#user(id) ⇒ Object
116
117
118
119
|
# File 'lib/slack/slack.rb', line 116
def user(id)
@users ||= load_users
@users[id]
end
|
#users ⇒ Object
111
112
113
114
|
# File 'lib/slack/slack.rb', line 111
def users
@users ||= load_users
@users.values
end
|