realtime-slackbot 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a79b3cf82fbab72c62706f8a80b4e6412b4f7a64
4
- data.tar.gz: 25d33eb53caf8fd8b6583141b26398a844f6829e
3
+ metadata.gz: 883564a8619951253252b18061d9621e99bb2090
4
+ data.tar.gz: 876583a388ca028b4b8c2104451f0b0732527968
5
5
  SHA512:
6
- metadata.gz: 6c5b65cf27eeee8b1fad311404703fadc0266de28fb6ecfda1a48d4d094183598aa348cb5d6f66d85963d542bd03bf80326ed64e71f73c0e8e3db2d6bfa0b927
7
- data.tar.gz: 06e51f1d0bf0badad597d7929baa0e40c927a9db141803a9b368baca2c8e46fe14612e9b77a675ff495b7bb3f5b9c03e6dcbfcd780df30f7646320deafdc861d
6
+ metadata.gz: d5e96150c5702f5274e8a61efd94d8374ca6a522e0ad3ab7bd1893a08f39706a17f0aa72380f15bd8a8987c1cc2d9024c367c08d25c6be3ee4cbcdeac2cd9488
7
+ data.tar.gz: 6e03e2678ae703a7f638e2bff3ac6413ab68816562bd9c9ac80b091b8a3e1ed5492e036be4bb03e507cf5f76bf04db6244c6ce472cbd072315f85777bda1c123
@@ -1,2 +1,3 @@
1
1
  require_relative 'slack/slack'
2
- require_relative 'slack/bot'
2
+ require_relative 'slack/wrappers/bot'
3
+ require_relative 'slack/ext/redis_session'
@@ -0,0 +1,36 @@
1
+ module SlackBot::Ext
2
+ class RedisSession
3
+ attr_reader :prefix
4
+
5
+ def initialize(team_id, args={})
6
+ @prefix = args[:prefix] || "team:#{team_id}:"
7
+ @store = args[:store]
8
+ @user_scoped = Hash.new
9
+ @channel_scoped = Hash.new
10
+ end
11
+
12
+ def [](key)
13
+ @store["#{@prefix}#{key}"]
14
+ end
15
+
16
+ def []=(key, val)
17
+ @store["#{@prefix}#{key}"] = val
18
+ end
19
+
20
+ def for_user(id)
21
+ @user_scoped[id] ||= RedisSession.new(nil, prefix: "#{@prefix}user:#{id}:", store: @store)
22
+ end
23
+
24
+ def for_channel(id)
25
+ @channel_scoped[id] ||= RedisSession.new(nil, prefix: "#{@prefix}channel:#{id}:", store: @store)
26
+ end
27
+
28
+ def core
29
+ @store
30
+ end
31
+
32
+ def to_s
33
+ @store.to_s
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,63 @@
1
+ module SlackBot
2
+ def channels
3
+ @channels ||= load_channels
4
+ end
5
+
6
+ def channel(id)
7
+ channels[id]
8
+ end
9
+
10
+ def user_channels
11
+ @user_channels ||= load_user_channels
12
+ end
13
+
14
+ def user_channel(user)
15
+ if user.is_a? User
16
+ id = user.id
17
+ else
18
+ id = user
19
+ end
20
+ user_channels[id]
21
+ end
22
+
23
+ def users
24
+ @users ||= load_users
25
+ end
26
+
27
+ def user(id)
28
+ users[id]
29
+ end
30
+
31
+ def me
32
+ @team_info['self']
33
+ end
34
+
35
+ def [](key)
36
+ @team_info[key]
37
+ end
38
+
39
+ private
40
+ def load_channels
41
+ channels = Hash.new
42
+ @team_info['channels'].each do |chan|
43
+ channels[chan['id']] = Channel.new chan, self
44
+ end
45
+ channels
46
+ end
47
+
48
+ def load_user_channels
49
+ channels = Hash.new
50
+ @team_info['ims'].each do |chan|
51
+ channels[chan['user']] = Channel.new chan, self
52
+ end
53
+ channels
54
+ end
55
+
56
+ def load_users
57
+ users = Hash.new
58
+ (@team_info['users'] + @team_info['bots']).map do |info|
59
+ users[info['id']] = User.new info, self
60
+ end
61
+ users
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ module SlackBot
2
+ class Session
3
+ def initialize(team_id, args={})
4
+ @user_scoped = Hash.new
5
+ @channel_scoped = Hash.new
6
+ @general = Hash.new
7
+ end
8
+
9
+ def [](key)
10
+ @general[key]
11
+ end
12
+
13
+ def []=(key, val)
14
+ @general[key] = val
15
+ end
16
+
17
+ def for_user(id)
18
+ @user_scoped[id] ||= Hash.new
19
+ end
20
+
21
+ def for_channel(id)
22
+ @channel_scoped[id] ||= Hash.new
23
+ end
24
+
25
+ def to_s
26
+ @general.to_s
27
+ end
28
+ end
29
+ end
data/lib/slack/slack.rb CHANGED
@@ -1,7 +1,9 @@
1
- require_relative 'user'
2
- require_relative 'message'
3
- require_relative 'channel'
1
+ require_relative 'wrappers/user'
2
+ require_relative 'wrappers/message'
3
+ require_relative 'wrappers/channel'
4
+ require_relative 'session'
4
5
  require_relative 'matchers/matcher_group'
6
+ require_relative 'helpers'
5
7
  require 'net/http'
6
8
  require 'faye/websocket'
7
9
  require 'eventmachine'
@@ -12,18 +14,36 @@ module SlackBot
12
14
  attr_accessor :socket
13
15
  attr_writer :debug
14
16
  attr_reader :team_info
17
+ attr_reader :session
18
+ attr_accessor :auth_url
15
19
 
16
20
  def initialize(auth_key, options={})
17
- @debug = options[:log]
21
+ setup(options)
18
22
  @key = auth_key.strip
23
+ end
24
+
25
+ def setup(options={})
26
+ @debug = options[:log]
19
27
  @matchers = Hash.new
28
+ if options[:session]
29
+ @session_type = options[:session].delete(:use)
30
+ @session_args = options[:session]
31
+ else
32
+ @session_type = Session
33
+ @session_args = {}
34
+ end
35
+ @auth_url = SLACK_AUTH_URL
20
36
  end
21
37
 
22
38
  def get_url
23
- data = Net::HTTP.get(URI(SLACK_AUTH_URL + @key))
39
+ data = Net::HTTP.get(URI(@auth_url + @key))
24
40
  json = JSON.parse(data)
25
41
  @team_info = json
26
- json['url']
42
+ if json['ok']
43
+ return json['url']
44
+ else
45
+ raise "ERROR: #{json.to_s}"
46
+ end
27
47
  end
28
48
 
29
49
  def run
@@ -33,6 +53,7 @@ module SlackBot
33
53
 
34
54
  ws.on :open do |event|
35
55
  log(:open, event.to_s)
56
+ create_session(team_info['team']['id'])
36
57
  hook(:opened)
37
58
  end
38
59
 
@@ -56,29 +77,7 @@ module SlackBot
56
77
  log(:close, "#{event.code} #{event.reason}")
57
78
  @socket = ws = nil
58
79
  hook(:closed)
59
- end
60
- end
61
- end
62
-
63
- def hook(action, *args)
64
- if self.respond_to? "#{action}_matcher"
65
- unless @matchers.has_key? action
66
- matcher_group = MatcherGroup.new(action)
67
- self.send("#{action}_matcher", matcher_group)
68
- @matchers[action] = matcher_group
69
- end
70
- begin
71
- @matchers[action].respond_for(args.first)
72
- rescue Exception => e
73
- puts e.message
74
- puts e.backtrace.join "\n"
75
- end
76
- elsif self.respond_to? action
77
- begin
78
- send(action, *args)
79
- rescue Exception => e
80
- puts e.message
81
- puts e.backtrace.join "\n"
80
+ EM.stop
82
81
  end
83
82
  end
84
83
  end
@@ -104,68 +103,6 @@ module SlackBot
104
103
  post(msg['channel'], text)
105
104
  end
106
105
 
107
- def load_channels
108
- channels = Hash.new
109
- @team_info['channels'].each do |chan|
110
- channels[chan['id']] = Channel.new chan, self
111
- end
112
- channels
113
- end
114
-
115
- def channels
116
- @channels ||= load_channels
117
- end
118
-
119
- def channel(id)
120
- channels[id]
121
- end
122
-
123
- def load_user_channels
124
- channels = Hash.new
125
- @team_info['ims'].each do |chan|
126
- channels[chan['user']] = Channel.new chan, self
127
- end
128
- channels
129
- end
130
-
131
- def user_channels
132
- @user_channels ||= load_user_channels
133
- end
134
-
135
- def user_channel(user)
136
- if user.is_a? User
137
- id = user.id
138
- else
139
- id = user
140
- end
141
- user_channels[id]
142
- end
143
-
144
-
145
- def load_users
146
- users = Hash.new
147
- (@team_info['users'] + @team_info['bots']).map do |info|
148
- users[info['id']] = User.new info
149
- end
150
- users
151
- end
152
-
153
- def users
154
- @users ||= load_users
155
- end
156
-
157
- def user(id)
158
- users[id]
159
- end
160
-
161
- def me
162
- @team_info['self']
163
- end
164
-
165
- def [](key)
166
- @team_info[key]
167
- end
168
-
169
106
  def log(type, message)
170
107
  if debug?
171
108
  puts "#{type}: #{message}"
@@ -175,4 +112,32 @@ module SlackBot
175
112
  def debug?
176
113
  @debug
177
114
  end
178
- end
115
+
116
+ private
117
+ def hook(action, *args)
118
+ if self.respond_to? "#{action}_matcher"
119
+ unless @matchers.has_key? action
120
+ matcher_group = MatcherGroup.new(action)
121
+ self.send("#{action}_matcher", matcher_group)
122
+ @matchers[action] = matcher_group
123
+ end
124
+ begin
125
+ @matchers[action].respond_for(args.first)
126
+ rescue Exception => e
127
+ puts e.message
128
+ puts e.backtrace.join "\n"
129
+ end
130
+ elsif self.respond_to? action
131
+ begin
132
+ send(action, *args)
133
+ rescue Exception => e
134
+ puts e.message
135
+ puts e.backtrace.join "\n"
136
+ end
137
+ end
138
+ end
139
+
140
+ def create_session(team_id)
141
+ @session = @session_type.new(team_id, @session_args)
142
+ end
143
+ end
@@ -1,4 +1,4 @@
1
- require_relative 'slack'
1
+ require_relative '../slack'
2
2
 
3
3
  module SlackBot
4
4
  class Bot
@@ -34,6 +34,9 @@ module SlackBot
34
34
  p = @data['topic']
35
35
  p && p['value']
36
36
  end
37
+ def session
38
+ @bot.session.for_channel(self.id)
39
+ end
37
40
 
38
41
  def to_s
39
42
  if user_channel?
@@ -43,4 +46,4 @@ module SlackBot
43
46
  end
44
47
  end
45
48
  end
46
- end
49
+ end
@@ -15,7 +15,7 @@ module SlackBot
15
15
  end
16
16
 
17
17
  def to_s
18
- "#{@user.name}: #{self.text}"
18
+ "#{@user}: #{self.text}"
19
19
  end
20
20
 
21
21
  def [](key)
@@ -35,7 +35,7 @@ module SlackBot
35
35
  end
36
36
  def channel
37
37
  chan = @data['channel']
38
- @bot.channel chan
38
+ @bot.channel(chan) || @bot.user_channel(chan)
39
39
  end
40
40
  end
41
41
  end
@@ -1,7 +1,8 @@
1
1
  module SlackBot
2
2
  class User
3
- def initialize(data)
3
+ def initialize(data, bot)
4
4
  @data = data
5
+ @bot = bot
5
6
  end
6
7
 
7
8
  def to_s
@@ -35,5 +36,8 @@ module SlackBot
35
36
  def bot?; @data['is_bot'] end
36
37
  def presence; @data['presence'] end
37
38
  def user_channel; @bot.user_channel self end
39
+ def session
40
+ @bot.session.for_user(self.id)
41
+ end
38
42
  end
39
- end
43
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: realtime-slackbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Richardson
@@ -45,13 +45,16 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
47
  - lib/realtime-slackbot.rb
48
- - lib/slack/bot.rb
49
- - lib/slack/channel.rb
48
+ - lib/slack/ext/redis_session.rb
49
+ - lib/slack/helpers.rb
50
50
  - lib/slack/matchers/matcher.rb
51
51
  - lib/slack/matchers/matcher_group.rb
52
- - lib/slack/message.rb
52
+ - lib/slack/session.rb
53
53
  - lib/slack/slack.rb
54
- - lib/slack/user.rb
54
+ - lib/slack/wrappers/bot.rb
55
+ - lib/slack/wrappers/channel.rb
56
+ - lib/slack/wrappers/message.rb
57
+ - lib/slack/wrappers/user.rb
55
58
  homepage: http://github.com/javanut13/realtime-slackbot
56
59
  licenses:
57
60
  - MIT