slack-bot-server 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1a580f416814eef8f1582047b3f748022b2f094
4
- data.tar.gz: e307931094203fe4590049d3d4eb966ad4a96fed
3
+ metadata.gz: 905bc700aa34e23d3da3e04a4956248ceb8f6d09
4
+ data.tar.gz: 8e85e1157e0d351c58b861f30586570786d8e871
5
5
  SHA512:
6
- metadata.gz: 88d60ed5fc1b11d4df51d4ebfe9f92042e660a61bed513130d22a200fb4bee70001655320f5683b0c0418336ec50ede9facfbc36961105bbfa90199b1962a0d2
7
- data.tar.gz: 48eae3c8528df390699a124782b558d64d36685d4f83b014b11dc3b4d89857c654c6c27efdfb02ae47e200b112e8c5773af0ed0b1eaebd9c0c4a992f8b8ac721
6
+ metadata.gz: a4836b1679ba77a3089bca65188317847a8a3b7c797d87e18a9cad009237fa3e0655e9dcc5a9b6faf7a9e5054b8f23863a44bcf3db4db8a4beed9495b8fcfda1
7
+ data.tar.gz: b6e71a1aafb3139235875f2d07c82b2d9f780647416ae3bb32ba5af8a63cb31c352c453ee3d3f1b05cff202d706139d4fb17f8592aca2a1d719ca23437f447a9
data/README.md CHANGED
@@ -35,7 +35,7 @@ require 'slack_bot_server/simple_bot'
35
35
 
36
36
  # Use a Redis-based queue to add/remove bots and to trigger
37
37
  # bot messages to be sent
38
- queue = SlackBotServer::RedisQueue.new(Redis.new)
38
+ queue = SlackBotServer::RedisQueue.new
39
39
 
40
40
  # Create a new server using that queue
41
41
  server = SlackBotServer::Server.new(queue: queue)
@@ -10,12 +10,17 @@ class SlackBotServer::Bot
10
10
  @key = key || @token
11
11
  @api = ::Slack::Client.new(token: @token)
12
12
  @im_channel_ids = []
13
+ @channel_ids = []
14
+ @connected = false
15
+ @running = false
13
16
 
14
17
  raise InvalidToken unless auth_test['ok']
15
18
  end
16
19
 
17
20
  def say(options)
18
- @api.chat_postMessage(default_message_options.merge(options))
21
+ @channel_ids.each do |channel_id|
22
+ @api.chat_postMessage(default_message_options.merge(options).merge(channel: channel_id))
23
+ end
19
24
  end
20
25
 
21
26
  def reply(options)
@@ -35,34 +40,49 @@ class SlackBotServer::Bot
35
40
  end
36
41
 
37
42
  def start
43
+ @running = true
38
44
  @ws = Faye::WebSocket::Client.new(websocket_url, nil, ping: 60)
39
45
 
40
46
  @ws.on :open do |event|
47
+ @connected = true
41
48
  log "connected to '#{team}'"
42
49
  load_im_channels
50
+ load_channels
43
51
  end
44
52
 
45
53
  @ws.on :message do |event|
46
54
  begin
47
- debug event
55
+ debug event.data
48
56
  handle_message(event)
49
57
  rescue => e
50
58
  log error: e
59
+ log backtrace: e.backtrace
51
60
  end
52
61
  end
53
62
 
54
63
  @ws.on :close do |event|
55
64
  log "disconnected"
65
+ @connected = false
66
+ if @running
67
+ start
68
+ end
56
69
  end
57
70
  end
58
71
 
59
72
  def stop
60
73
  log "closing connection"
74
+ @running = false
61
75
  @ws.close
62
76
  log "closed"
63
77
  end
64
78
 
79
+ def connected?
80
+ @connected
81
+ end
82
+
65
83
  class << self
84
+ attr_reader :mention_keywords
85
+
66
86
  def username(name)
67
87
  default_message_options[:username] = name
68
88
  end
@@ -71,8 +91,12 @@ class SlackBotServer::Bot
71
91
  default_message_options[:icon_url] = url
72
92
  end
73
93
 
94
+ def mention_as(*keywords)
95
+ @mention_keywords = keywords
96
+ end
97
+
74
98
  def default_message_options
75
- @default_message_options ||= {channel: '#general'}
99
+ @default_message_options ||= {}
76
100
  end
77
101
 
78
102
  def callbacks_for(type)
@@ -92,9 +116,9 @@ class SlackBotServer::Bot
92
116
  def on_mention(&block)
93
117
  on(:message) do |data|
94
118
  if !bot_message?(data) &&
95
- (data['text'] =~ /\A#{user}[\s\:](.*)/ ||
96
- data['text'] =~ /\A<@#{user_id}>[\s\:](.*)/)
97
- message = $1.strip
119
+ (data['text'] =~ /\A(#{mention_keywords.join('|')})[\s\:](.*)/i ||
120
+ data['text'] =~ /\A(<@#{user_id}>)[\s\:](.*)/)
121
+ message = $2.strip
98
122
  @last_received_data = data.merge('message' => message)
99
123
  instance_exec(@last_received_data, &block)
100
124
  end
@@ -117,6 +141,18 @@ class SlackBotServer::Bot
117
141
  @im_channel_ids << channel_id
118
142
  end
119
143
 
144
+ on :channel_joined do |data|
145
+ channel_id = data['channel']['id']
146
+ log "Adding new channel: #{channel_id}"
147
+ @channel_ids << channel_id
148
+ end
149
+
150
+ on :channel_left do |data|
151
+ channel_id = data['channel']
152
+ log "Removing channel: #{channel_id}"
153
+ @channel_ids.delete(channel_id)
154
+ end
155
+
120
156
  def to_s
121
157
  "<#{self.class.name} key:#{key}>"
122
158
  end
@@ -170,12 +206,20 @@ class SlackBotServer::Bot
170
206
  log im_channels: @im_channel_ids
171
207
  end
172
208
 
209
+ def load_channels
210
+ log "Loading IM channels"
211
+ result = @api.channels_list(exclude_archived: 1)
212
+ @channel_ids = result['channels'].select { |d| d['is_member'] == true }.map { |d| d['id'] }
213
+ log channels: @channel_ids
214
+ end
215
+
173
216
  def is_im_channel?(id)
174
217
  @im_channel_ids.include?(id)
175
218
  end
176
219
 
177
220
  def bot_message?(data)
178
- data['subtype'] == 'bot_message'
221
+ data['subtype'] == 'bot_message' ||
222
+ data['user'] == 'USLACKBOT'
179
223
  end
180
224
 
181
225
  def websocket_url
@@ -185,4 +229,8 @@ class SlackBotServer::Bot
185
229
  def default_message_options
186
230
  self.class.default_message_options
187
231
  end
232
+
233
+ def mention_keywords
234
+ self.class.mention_keywords || [user]
235
+ end
188
236
  end
@@ -1,9 +1,14 @@
1
1
  require 'multi_json'
2
2
 
3
3
  class SlackBotServer::RedisQueue
4
- def initialize(redis)
4
+ def initialize(redis=nil)
5
5
  @key = 'slack_bot_server:queue'
6
- @redis = redis
6
+ @redis = if redis
7
+ redis
8
+ else
9
+ require 'redis'
10
+ Redis.new
11
+ end
7
12
  end
8
13
 
9
14
  def push(value)
@@ -13,7 +18,7 @@ class SlackBotServer::RedisQueue
13
18
  def pop
14
19
  json_value = @redis.lpop @key
15
20
  if json_value
16
- MultiJson.load(json_value)
21
+ MultiJson.load(json_value, symbolize_keys: true)
17
22
  else
18
23
  nil
19
24
  end
@@ -1,3 +1,3 @@
1
1
  module SlackBotServer
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.10"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "redis"
27
28
  spec.add_development_dependency "rspec"
28
29
  spec.add_development_dependency "rspec-eventmachine"
29
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-bot-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Adam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-30 00:00:00.000000000 Z
11
+ date: 2015-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-api
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec
71
85
  requirement: !ruby/object:Gem::Requirement