slackbot_frd 0.1.5 → 0.2.0

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: 23cd85c4a4b08ca1c27c5b7b86a4cd1572762787
4
- data.tar.gz: 2fc8953394f69e1c326f070d43cad528c174a52e
3
+ metadata.gz: 8f18d78abc8751ff7cf6ccab669a856f3e5896eb
4
+ data.tar.gz: 7ccd3d4e72331455e3ae954225dea63421cbc696
5
5
  SHA512:
6
- metadata.gz: b4ac51d2f8b06597245eaa3b18f253eb786718e7254f9e0402e6fef175ce1d6e5630278789783a24785a4c52359922d25a2e145a0b3a513e78d0ba8eacf7f9bf
7
- data.tar.gz: 5201344b4cbb5ec4be6bf21cb31714166fe22a3ad5d6cbdac5c7008a2040c9b23d1eacb80b90fad3443f3ec131945a181f15883af5b890f24c9138c8312f78e3
6
+ metadata.gz: cd865011f64fd16a8d53f39ce099f0ca62308b0228117b598471817cf06be91eab66d4e8ee84e3b388c1a345a7bd94e4dbe1c56b45bd46cd46d7894f4a98e529
7
+ data.tar.gz: 088ccc188cc9f8f4b1bad887ce5beb91107e8053393a927aa6bf36ef29f265036c51c7cfd46ca25994533119ab8101688a6fab6c5c2fe75d78e3e34368bc9740
data/bin/slackbot-frd CHANGED
@@ -16,15 +16,15 @@ end
16
16
 
17
17
  DEBUG = true
18
18
 
19
- PID_FILE_WATCHER = "/tmp/slackbot-frd-watcher.pid"
20
- PID_FILE_CONNECTION = "/tmp/slackbot-frd-connection.pid"
21
- BOT_LIST_FILE = "/tmp/slackbot-frd-bot-list.pid"
22
- ERROR_FILE = "/tmp/slackbot-frd.errors"
23
- DEFAULT_CONFIG_FILE = "slackbot-frd.conf"
24
- LOG_FILE = "slackbot-frd.log"
19
+ PID_FILE_WATCHER = '/tmp/slackbot-frd-watcher.pid'
20
+ PID_FILE_CONNECTION = '/tmp/slackbot-frd-connection.pid'
21
+ BOT_LIST_FILE = '/tmp/slackbot-frd-bot-list.pid'
22
+ ERROR_FILE = '/tmp/slackbot-frd.errors'
23
+ DEFAULT_CONFIG_FILE = 'slackbot-frd.conf'
24
+ LOG_FILE = 'slackbot-frd.log'
25
25
 
26
26
  class SlackbotFrdBin < Thor
27
- desc "list", "List all bots"
27
+ desc 'list', 'List all bots'
28
28
  long_desc <<-LONGDESC
29
29
  list will print out all available bots
30
30
 
@@ -34,16 +34,16 @@ class SlackbotFrdBin < Thor
34
34
  # TODO
35
35
  end
36
36
 
37
- desc "status", "Report status on daemon procs"
37
+ desc 'status', 'Report status on daemon procs'
38
38
  def status
39
39
  if running?(watcher_pid)
40
40
  puts "Daemon running as pid '#{watcher_pid}'".green
41
41
  else
42
- puts "Daemon not running".red
42
+ puts 'Daemon not running'.red
43
43
  end
44
44
  end
45
45
 
46
- desc "new <project-name>", "Generate a new slackbot_frd project"
46
+ desc 'new <project-name>', 'Generate a new slackbot_frd project'
47
47
  long_desc <<-LONGDESC
48
48
  new will generate a skeleton for a new slackbot_frd project
49
49
  LONGDESC
@@ -56,25 +56,25 @@ class SlackbotFrdBin < Thor
56
56
  Dir.mkdir(proj_name)
57
57
  Dir.chdir(proj_name)
58
58
  genconfig
59
- File.write("Gemfile", <<-GEMFILE.strip_heredoc)
59
+ File.write('Gemfile', <<-GEMFILE.strip_heredoc)
60
60
  source "https://rubygems.org"
61
61
 
62
62
  gem "slackbot_frd"
63
63
  GEMFILE
64
- Dir.mkdir("bots")
65
- write_example_bot("bots/example_bot.rb")
66
- Dir.mkdir("lib")
64
+ Dir.mkdir('bots')
65
+ write_example_bot('bots/example_bot.rb')
66
+ Dir.mkdir('lib')
67
67
 
68
68
  unless options[:nospec]
69
- File.append("Gemfile", <<-GEMFILE.strip_heredoc)
69
+ File.append('Gemfile', <<-GEMFILE.strip_heredoc)
70
70
 
71
71
  gem "rspec", "~> 3.2"
72
72
  GEMFILE
73
- Dir.mkdir("spec")
73
+ Dir.mkdir('spec')
74
74
  end
75
75
  end
76
76
 
77
- desc "genconfig", "Generate a skeleton config file"
77
+ desc 'genconfig', 'Generate a skeleton config file'
78
78
  long_desc <<-LONGDESC
79
79
  genconfig will generate a skeleton config file that
80
80
  can be customized
@@ -93,7 +93,7 @@ class SlackbotFrdBin < Thor
93
93
  CONFIG_FILE_SKELETON
94
94
  end
95
95
 
96
- desc "start [bot1] [bot2] [botx...]", "Start all specified bots, or all bots"
96
+ desc 'start [bot1] [bot2] [botx...]', 'Start all specified bots, or all bots'
97
97
  long_desc <<-LONGDESC
98
98
  start [bot1] [bot2] [botx...] will start the specified bots.
99
99
  If no bots are specified, all available bots will be run.
@@ -125,25 +125,25 @@ class SlackbotFrdBin < Thor
125
125
  $slackbotfrd_conf.freeze
126
126
 
127
127
  daemonize = false
128
- daemonize = $slackbotfrd_conf["daemonize"] if $slackbotfrd_conf["daemonize"]
129
- daemonize = ENV["SLACKBOT_FRD_DAEMONIZE"] if ENV["SLACKBOT_FRD_DAEMONIZE"]
128
+ daemonize = $slackbotfrd_conf['daemonize'] if $slackbotfrd_conf['daemonize']
129
+ daemonize = ENV['SLACKBOT_FRD_DAEMONIZE'] if ENV['SLACKBOT_FRD_DAEMONIZE']
130
130
  daemonize = options[:daemonize] if options[:daemonize]
131
131
 
132
132
  botdir = Dir.pwd
133
- botdir = $slackbotfrd_conf["botdir"] if $slackbotfrd_conf["botdir"]
134
- botdir = ENV["SLACKBOT_FRD_BOTDIR"] if ENV["SLACKBOT_FRD_BOTDIR"]
133
+ botdir = $slackbotfrd_conf['botdir'] if $slackbotfrd_conf['botdir']
134
+ botdir = ENV['SLACKBOT_FRD_BOTDIR'] if ENV['SLACKBOT_FRD_BOTDIR']
135
135
  botdir = options[:botdir] if options[:botdir]
136
136
  botdir = File.expand_path(botdir)
137
137
 
138
138
  SlackbotFrd::Log.logfile = "#{botdir}/#{LOG_FILE}"
139
- SlackbotFrd::Log.logfile = $slackbotfrd_conf["log_file"] if $slackbotfrd_conf["log_file"]
140
- SlackbotFrd::Log.logfile = ENV["SLACKBOT_FRD_LOG_FILE"] if ENV["SLACKBOT_FRD_LOG_FILE"]
141
- SlackbotFrd::Log.logfile = options["log-file"] if options['log-file']
139
+ SlackbotFrd::Log.logfile = $slackbotfrd_conf['log_file'] if $slackbotfrd_conf['log_file']
140
+ SlackbotFrd::Log.logfile = ENV['SLACKBOT_FRD_LOG_FILE'] if ENV['SLACKBOT_FRD_LOG_FILE']
141
+ SlackbotFrd::Log.logfile = options['log-file'] if options['log-file']
142
142
 
143
143
  SlackbotFrd::Log.level = :info
144
- SlackbotFrd::Log.level = $slackbotfrd_conf["log_level"] if $slackbotfrd_conf["log_level"]
145
- SlackbotFrd::Log.level = ENV["SLACKBOT_FRD_LOG_LEVEL"] if ENV["SLACKBOT_FRD_LOG_LEVEL"]
146
- SlackbotFrd::Log.level = options["log-level"] if options['log-level']
144
+ SlackbotFrd::Log.level = $slackbotfrd_conf['log_level'] if $slackbotfrd_conf['log_level']
145
+ SlackbotFrd::Log.level = ENV['SLACKBOT_FRD_LOG_LEVEL'] if ENV['SLACKBOT_FRD_LOG_LEVEL']
146
+ SlackbotFrd::Log.level = options['log-level'] if options['log-level']
147
147
 
148
148
  # If we got a number for log level that is valid, just use that
149
149
  if SlackbotFrd::Log.level =~ /\d+/ && SlackbotFrd::Log.levels.values.include?(SlackbotFrd::Log.level.to_i)
@@ -165,11 +165,11 @@ class SlackbotFrdBin < Thor
165
165
  SlackbotFrd::Log.warn("Logging to file '#{SlackbotFrd::Log.logfile}'")
166
166
  SlackbotFrd::Log.warn("Logging level set to '#{SlackbotFrd::Log.level}'")
167
167
 
168
- token = $slackbotfrd_conf["token"]
169
- token = ENV["SLACKBOT_FRD_TOKEN"] if ENV["SLACKBOT_FRD_TOKEN"]
168
+ token = $slackbotfrd_conf['token']
169
+ token = ENV['SLACKBOT_FRD_TOKEN'] if ENV['SLACKBOT_FRD_TOKEN']
170
170
  token = options[:token] if options[:token]
171
171
  unless token
172
- SlackbotFrd::Log.error("No token found. Cannot authenticate to Slack")
172
+ SlackbotFrd::Log.error('No token found. Cannot authenticate to Slack')
173
173
  return
174
174
  end
175
175
 
@@ -180,7 +180,7 @@ class SlackbotFrdBin < Thor
180
180
  end
181
181
  end
182
182
 
183
- desc "stop", "Stop all bots"
183
+ desc 'stop', 'Stop all bots'
184
184
  long_desc <<-LONGDESC
185
185
  stop will stop all bots
186
186
 
@@ -195,7 +195,7 @@ class SlackbotFrdBin < Thor
195
195
  delfile(ERROR_FILE)
196
196
  end
197
197
 
198
- desc "bump", "Kill the bots, but not the watcher"
198
+ desc 'bump', 'Kill the bots, but not the watcher'
199
199
  long_desc <<-LONGDESC
200
200
  bump will kill the connection process, but not the watcher.
201
201
  this causes the watcher to restart the connection. This
@@ -208,7 +208,7 @@ class SlackbotFrdBin < Thor
208
208
  kill_pid(connection_pid)
209
209
  end
210
210
 
211
- desc "restart", "Stop all bots and restart them"
211
+ desc 'restart', 'Stop all bots and restart them'
212
212
  long_desc <<-LONGDESC
213
213
  restart will restart all bots
214
214
 
@@ -17,11 +17,13 @@ class BotStarter
17
17
  bot_enabled = ->(bot) do
18
18
  enabled_bots.empty? ||
19
19
  enabled_bots.include?(bot) ||
20
- enabled_bots.include?(bot.gsub("-", "_").camelize)
20
+ enabled_bots.include?(bot.gsub('-', '_').camelize)
21
21
  end
22
22
 
23
23
  # Create a new Connection to pass to the bot classes
24
- slack_connection = SlackbotFrd::SlackConnection.new(token, errors_file)
24
+ slack_connection = SlackbotFrd::SlackConnection.new(
25
+ token: token, errors_file: errors_file, monitor_connection: true
26
+ )
25
27
 
26
28
  load_bot_files(botdir)
27
29
 
@@ -37,12 +39,12 @@ class BotStarter
37
39
  end
38
40
 
39
41
  if bots.count == 0
40
- SlackbotFrd::Log.error("Not starting: no bots found")
41
- File.append(errors_file, "Not starting: no bots found")
42
+ SlackbotFrd::Log.error('Not starting: no bots found')
43
+ File.append(errors_file, 'Not starting: no bots found')
42
44
  else
43
- SlackbotFrd::Log.debug("Starting SlackConnection")
45
+ SlackbotFrd::Log.debug('Starting SlackConnection')
44
46
  slack_connection.start
45
- SlackbotFrd::Log.debug("Connection closed")
47
+ SlackbotFrd::Log.debug('Connection closed')
46
48
  end
47
49
  end
48
50
 
@@ -12,7 +12,7 @@ module SlackbotFrd
12
12
 
13
13
  # This is where the bot adds all of their callbacks to the bpbot
14
14
  def add_callbacks(slack_connection)
15
- raise StandardError.new("You must override the define() method for your bot to do anything")
15
+ raise StandardError.new('You must override the define() method for your bot to do anything')
16
16
  end
17
17
  end
18
18
  end
@@ -5,7 +5,7 @@ module SlackbotFrd
5
5
  if message
6
6
  super(message)
7
7
  else
8
- super("An API token is required for authenticating to the Slack API")
8
+ super('An API token is required for authenticating to the Slack API')
9
9
  end
10
10
  end
11
11
  end
@@ -48,7 +48,7 @@ module SlackbotFrd
48
48
  om = "#{DateTime.now.strftime('%Y-%m-%e %H:%M:%S.%L %z')}: [#{loglevel}]: #{message}\n"
49
49
  print om.send(color)
50
50
  begin
51
- raise StandardError.new("No log file specified. (Set with SlackbotFrd::Log.logfile=)") unless @logfile
51
+ raise StandardError.new('No log file specified. (Set with SlackbotFrd::Log.logfile=)') unless @logfile
52
52
  File.open(@logfile, 'a') do |f|
53
53
  f.write(om)
54
54
  end
@@ -21,16 +21,17 @@ module SlackbotFrd
21
21
  FILE_DIR = File.dirname(FILE_PATH)
22
22
  LOG_FILE = "#{APP_ROOT}/bp-slackbot.log"
23
23
  PID_FILE_NAME = "#{APP_ROOT}/bp-slackbot.pid"
24
+ PING_INTERVAL_SECONDS = 5
24
25
 
25
26
  attr_accessor :token
26
27
 
27
- def initialize(token, errors_file)
28
- unless token
29
- log_and_add_to_error_file("No token passed to #{self.class}")
30
- end
28
+ def initialize(token:, errors_file:, monitor_connection: true)
29
+ log_and_add_to_error_file("No token passed to #{self.class}") unless token
31
30
 
32
31
  @token = token
33
32
  @errors_file = errors_file
33
+ @monitor_connection = monitor_connection
34
+
34
35
  @event_id = 0
35
36
  @on_connected_callbacks = []
36
37
  @on_disconnected_callbacks = []
@@ -44,7 +45,8 @@ module SlackbotFrd
44
45
  @channel_id_to_name = {}
45
46
  @channel_name_to_id = {}
46
47
 
47
- restrict_actions_to_channels_joined
48
+ @pong_received = true
49
+
48
50
  SlackbotFrd::Log.debug("Done initializing #{self.class}")
49
51
  end
50
52
 
@@ -62,7 +64,9 @@ module SlackbotFrd
62
64
  end
63
65
 
64
66
  unless wss_url
65
- log_and_add_to_error_file("No Real Time stream opened by slack. Check for network connection and correct authentication token")
67
+ log_and_add_to_error_file(
68
+ 'No Real Time stream opened by slack. Check for network connection and correct authentication token'
69
+ )
66
70
  return
67
71
  end
68
72
  @ws = Faye::WebSocket::Client.new(wss_url)
@@ -72,10 +76,15 @@ module SlackbotFrd
72
76
  @ws.on(:message) { |event| process_message_received(event) }
73
77
 
74
78
  # Clean up our pid file
75
- @ws.on(:close) { |event| File.delete(PID_FILE_NAME) }
79
+ @ws.on(:close) { |_event| File.delete(PID_FILE_NAME) }
80
+
81
+ # This should ensure that we get a pong back at least every
82
+ # PING_INTERVAL_SECONDS, otherwise we die because our
83
+ # connection is probably toast
84
+ EM.add_periodic_timer(PING_INTERVAL_SECONDS) { check_ping }
76
85
  end
77
86
 
78
- SlackbotFrd::Log.debug("#{self.class}: event machine started")
87
+ SlackbotFrd::Log.info("#{self.class}: event machine loop terminated")
79
88
  end
80
89
 
81
90
  def event_id
@@ -93,13 +102,21 @@ module SlackbotFrd
93
102
 
94
103
  def on_message(user: :any, channel: :any, &block)
95
104
  wrap_user_or_channel_lookup_on_callback('on_message', user, channel) do
96
- @on_message_callbacks.add(user: user_name_to_id(user), channel: channel_name_to_id(channel), callback: block)
105
+ @on_message_callbacks.add(
106
+ user: user_name_to_id(user),
107
+ channel: channel_name_to_id(channel),
108
+ callback: block
109
+ )
97
110
  end
98
111
  end
99
112
 
100
113
  def on_channel_left(user: :any, channel: :any, &block)
101
114
  wrap_user_or_channel_lookup_on_callback('on_message_channel_left', user, channel) do
102
- @on_channel_left_callbacks.add(user: user_name_to_id(user), channel: channel_name_to_id(channel), callback: block)
115
+ @on_channel_left_callbacks.add(
116
+ user: user_name_to_id(user),
117
+ channel: channel_name_to_id(channel),
118
+ callback: block
119
+ )
103
120
  end
104
121
  end
105
122
 
@@ -138,7 +155,9 @@ module SlackbotFrd
138
155
  end
139
156
 
140
157
  def post_reaction(name:, channel: nil, timestamp: nil)
141
- SlackbotFrd::Log.debug("#{self.class}: Posting reaction '#{name}' to channel '#{channel}' with timestamp '#{timestamp}'")
158
+ SlackbotFrd::Log.debug(
159
+ "#{self.class}: Posting reaction '#{name}' to channel '#{channel}' with timestamp '#{timestamp}'"
160
+ )
142
161
 
143
162
  resp = SlackbotFrd::SlackMethods::ReactionsAdd.add(
144
163
  token: @token,
@@ -150,24 +169,25 @@ module SlackbotFrd
150
169
  SlackbotFrd::Log.debug("#{self.class}: Received response: #{resp}")
151
170
  end
152
171
 
153
- def invite_user(user: user, channel: channel)
154
- SlackbotFrd::Log.debug("#{self.class}: Inviting user '#{user}' to channel '#{channel}'")
172
+ def invite_user(user:, channel:)
173
+ SlackbotFrd::Log.debug(
174
+ "#{self.class}: Inviting user '#{user}' to channel '#{channel}'"
175
+ )
155
176
 
156
177
  resp = SlackbotFrd::SlackMethods::ChannelsInvite.invite(
157
178
  token: @token,
158
179
  user: user_name_to_id(user),
159
- channel: channel_name_to_id(channel),
180
+ channel: channel_name_to_id(channel)
160
181
  )
161
182
 
162
183
  SlackbotFrd::Log.debug("#{self.class}: Received response: #{resp}")
163
184
  end
164
185
 
165
- def restrict_actions_to_channels_joined(value = true)
166
- @restrict_actions_to_channels_joined = value
167
- end
168
-
169
186
  def users_in_channel(channel)
170
- a = SlackMethods::ChannelsInfo.members(token: @token, channel: channel_name_to_id(channel))
187
+ a = SlackMethods::ChannelsInfo.members(
188
+ token: @token,
189
+ channel: channel_name_to_id(channel)
190
+ )
171
191
  a.map{ |id| user_id_to_name(id) }
172
192
  end
173
193
 
@@ -189,52 +209,70 @@ module SlackbotFrd
189
209
 
190
210
  def user_id_to_name(user_id)
191
211
  return user_id if user_id == :any || user_id == :bot
192
- unless @user_id_to_name && @user_id_to_name.has_key?(user_id)
212
+ unless @user_id_to_name && @user_id_to_name.key?(user_id)
193
213
  refresh_user_info
194
214
  end
195
- SlackbotFrd::Log.warn("#{self.class}: User id '#{user_id}' not found") unless @user_id_to_name.include?(user_id)
215
+ unless @user_id_to_name.include?(user_id)
216
+ SlackbotFrd::Log.warn("#{self.class}: User id '#{user_id}' not found")
217
+ end
196
218
  @user_id_to_name[user_id]
197
219
  end
198
220
 
199
221
  def user_name_to_id(user_name)
200
222
  return user_name if user_name == :any || user_name == :bot
201
- unless @user_name_to_id && @user_name_to_id.has_key?(user_name)
223
+ unless @user_name_to_id && @user_name_to_id.key?(user_name)
202
224
  refresh_user_info
203
225
  end
204
- SlackbotFrd::Log.warn("#{self.class}: User name '#{user_name}' not found") unless @user_name_to_id.include?(user_name)
226
+ unless @user_name_to_id.include?(user_name)
227
+ SlackbotFrd::Log.warn(
228
+ "#{self.class}: User name '#{user_name}' not found"
229
+ )
230
+ end
205
231
  @user_name_to_id[user_name]
206
232
  end
207
233
 
208
234
  def channel_id_to_name(channel_id)
209
- unless @channel_id_to_name && @channel_id_to_name.has_key?(channel_id)
235
+ unless @channel_id_to_name && @channel_id_to_name.key?(channel_id)
210
236
  refresh_channel_info
211
237
  end
212
- SlackbotFrd::Log.warn("#{self.class}: Channel id '#{channel_id}' not found") unless @channel_id_to_name.include?(channel_id)
238
+ unless @channel_id_to_name.include?(channel_id)
239
+ SlackbotFrd::Log.warn(
240
+ "#{self.class}: Channel id '#{channel_id}' not found"
241
+ )
242
+ end
213
243
  @channel_id_to_name[channel_id]
214
244
  end
215
245
 
216
246
  def channel_name_to_id(channel_name)
217
247
  return channel_name if channel_name == :any
218
248
  nc = normalize_channel_name(channel_name)
219
- unless @channel_name_to_id && @channel_name_to_id.has_key?(nc)
249
+ unless @channel_name_to_id && @channel_name_to_id.key?(nc)
220
250
  refresh_channel_info
221
251
  end
222
- SlackbotFrd::Log.warn("#{self.class}: Channel name '#{nc}' not found") unless @channel_name_to_id.include?(nc)
252
+ unless @channel_name_to_id.include?(nc)
253
+ SlackbotFrd::Log.warn(
254
+ "#{self.class}: Channel name '#{nc}' not found"
255
+ )
256
+ end
223
257
  @channel_name_to_id[nc]
224
258
  end
225
259
 
226
260
  private
227
- def send_message_as_user(channel: channel, message: message)
261
+ def send_message_as_user(channel:, message:)
228
262
  unless @ws
229
- log_and_add_to_error_file("Cannot send message '#{message}' as user to channel '#{channel}' because not connected to wss stream")
263
+ log_and_add_to_error_file(
264
+ "Cannot send message '#{message}' as user to channel '#{channel}' because not connected to wss stream"
265
+ )
230
266
  end
231
267
 
232
- SlackbotFrd::Log.debug("#{self.class}: Sending message '#{message}' as user to channel '#{channel}'")
268
+ SlackbotFrd::Log.debug(
269
+ "#{self.class}: Sending message '#{message}' as user to channel '#{channel}'"
270
+ )
233
271
 
234
272
  begin
235
273
  resp = @ws.send({
236
274
  id: event_id,
237
- type: "message",
275
+ type: 'message',
238
276
  channel: channel_name_to_id(channel),
239
277
  text: message
240
278
  }.to_json)
@@ -247,7 +285,9 @@ module SlackbotFrd
247
285
 
248
286
  private
249
287
  def send_message_as_bot(channel:, message:, username:, avatar_emoji: nil, avatar_url: nil)
250
- SlackbotFrd::Log.debug("#{self.class}: Sending message '#{message}' as bot user '#{username}' to channel '#{channel}'")
288
+ SlackbotFrd::Log.debug(
289
+ "#{self.class}: Sending message '#{message}' as bot user '#{username}' to channel '#{channel}'"
290
+ )
251
291
 
252
292
  resp = SlackbotFrd::SlackMethods::ChatPostMessage.postMessage(
253
293
  token: @token,
@@ -265,10 +305,14 @@ module SlackbotFrd
265
305
  def wrap_user_or_channel_lookup_on_callback(callback_name, user, channel)
266
306
  begin
267
307
  return yield
268
- rescue SlackbotFrd::InvalidChannelError => e
269
- log_and_add_to_error_file("Unable to add #{callback_name} callback for channel '#{channel}'. Lookup of channel name to ID failed. Check network connection, and ensure channel exists and is accessible")
270
- rescue SlackbotFrd::InvalidUserError => e
271
- log_and_add_to_error_file("Unable to add #{callback_name} callback for user '#{user}'. Lookup of user name to ID failed. Check network connection and ensure user exists")
308
+ rescue SlackbotFrd::InvalidChannelError => _e
309
+ log_and_add_to_error_file(
310
+ "Unable to add #{callback_name} callback for channel '#{channel}'. Lookup of channel name to ID failed. Check network connection, and ensure channel exists and is accessible"
311
+ )
312
+ rescue SlackbotFrd::InvalidUserError => _e
313
+ log_and_add_to_error_file(
314
+ "Unable to add #{callback_name} callback for user '#{user}'. Lookup of user name to ID failed. Check network connection and ensure user exists"
315
+ )
272
316
  end
273
317
  end
274
318
 
@@ -282,44 +326,48 @@ module SlackbotFrd
282
326
  def process_message_received(event)
283
327
  message = JSON.parse(event.data)
284
328
  SlackbotFrd::Log.verbose("#{self.class}: Message received: #{message}")
285
- if message["type"] == "message"
286
- if message["subtype"] == "channel_join"
287
- process_join_message(message)
288
- elsif message["subtype"] == "channel_leave"
289
- process_leave_message(message)
290
- elsif message["subtype"] == "file_share"
291
- process_file_share(message)
292
- else
293
- process_chat_message(message)
294
- end
329
+
330
+ return unless message['type'] == 'message'
331
+ if message['subtype'] == 'channel_join'
332
+ process_join_message(message)
333
+ elsif message['subtype'] == 'channel_leave'
334
+ process_leave_message(message)
335
+ elsif message['subtype'] == 'file_share'
336
+ process_file_share(message)
337
+ else
338
+ process_chat_message(message)
295
339
  end
296
340
  end
297
341
 
298
342
  private
299
343
  def process_file_share(message)
300
- SlackbotFrd::Log.verbose("#{self.class}: Processing file share: #{message}")
301
- SlackbotFrd::Log.debug("#{self.class}: Not processing file share because it is not implemented:")
344
+ SlackbotFrd::Log.verbose(
345
+ "#{self.class}: Processing file share: #{message}"
346
+ )
347
+ SlackbotFrd::Log.debug(
348
+ "#{self.class}: Not processing file share because it is not implemented:"
349
+ )
302
350
  end
303
351
 
304
352
  private
305
353
  def extract_user(message)
306
- user = message["user"]
307
- user = :bot if message["subtype"] == "bot_message"
308
- user = message["message"]["user"] if !user && message["message"]
354
+ user = message['user']
355
+ user = :bot if message['subtype'] == 'bot_message'
356
+ user = message['message']['user'] if !user && message['message']
309
357
  user
310
358
  end
311
359
 
312
360
  private
313
361
  def extract_ts(message)
314
- ts = message["ts"]
315
- ts = message["message"]["ts"] if message["message"] && message["message"]["ts"]
362
+ ts = message['ts']
363
+ ts = message['message']['ts'] if message['message'] && message['message']['ts']
316
364
  ts
317
365
  end
318
366
 
319
367
  private
320
368
  def extract_text(message)
321
- text = message["text"]
322
- text = message["message"]["text"] if !text && message["message"]
369
+ text = message['text']
370
+ text = message['message']['text'] if !text && message['message']
323
371
  text
324
372
  end
325
373
 
@@ -328,7 +376,7 @@ module SlackbotFrd
328
376
  SlackbotFrd::Log.verbose("#{self.class}: Processing chat message: #{message}")
329
377
 
330
378
  user = extract_user(message)
331
- channel = message["channel"]
379
+ channel = message['channel']
332
380
  text = extract_text(message)
333
381
  ts = extract_ts(message)
334
382
 
@@ -362,9 +410,9 @@ module SlackbotFrd
362
410
  private
363
411
  def process_join_message(message)
364
412
  SlackbotFrd::Log.verbose("#{self.class}: Processing join message: #{message}")
365
- user = message["user"]
366
- user = :bot if message["subtype"] == "bot_message"
367
- channel = message["channel"]
413
+ user = message['user']
414
+ user = :bot if message['subtype'] == 'bot_message'
415
+ channel = message['channel']
368
416
  @on_channel_joined_callbacks.where_include_all(user: user, channel: channel).each do |callback|
369
417
  callback.call(user: user_id_to_name(user), channel: channel_id_to_name(channel))
370
418
  end
@@ -373,9 +421,9 @@ module SlackbotFrd
373
421
  private
374
422
  def process_leave_message(message)
375
423
  SlackbotFrd::Log.verbose("#{self.class}: Processing leave message: #{message}")
376
- user = message["user"]
377
- user = :bot if message["subtype"] == "bot_message"
378
- channel = message["channel"]
424
+ user = message['user']
425
+ user = :bot if message['subtype'] == 'bot_message'
426
+ channel = message['channel']
379
427
  @on_channel_left_callbacks.where_include_all(user: user, channel: channel).each do |callback|
380
428
  callback.call(user: user_id_to_name(user), channel: channel_id_to_name(channel))
381
429
  end
@@ -421,5 +469,29 @@ module SlackbotFrd
421
469
  SlackbotFrd::Log.error(err)
422
470
  File.append(@errors_file, "#{err}\n")
423
471
  end
472
+
473
+ private
474
+ def check_ping
475
+ @pong_received ? send_ping : die_from_no_pong
476
+ end
477
+
478
+ private
479
+ def send_ping
480
+ SlackbotFrd::Log.verbose('Sending ping')
481
+ @pong_received = false
482
+ @ws.ping do
483
+ @pong_received = true
484
+ SlackbotFrd::Log.verbose('Pong received')
485
+ end
486
+ end
487
+
488
+ private
489
+ def die_from_no_pong
490
+ SlackbotFrd::Log.error(
491
+ 'Pong not received after 5 seconds. Stopping EM loop...'
492
+ )
493
+ @ws.close
494
+ EM.stop_event_loop
495
+ end
424
496
  end
425
497
  end
@@ -24,8 +24,8 @@ module SlackbotFrd
24
24
  end
25
25
 
26
26
  def members
27
- if @response["channel"]
28
- @response["channel"]["members"]
27
+ if @response['channel']
28
+ @response['channel']['members']
29
29
  else
30
30
  []
31
31
  end
@@ -20,16 +20,16 @@ module SlackbotFrd
20
20
 
21
21
  def ids_to_names
22
22
  retval = {}
23
- @response["channels"].each do |channel|
24
- retval[channel["id"]] = channel["name"]
23
+ @response['channels'].each do |channel|
24
+ retval[channel['id']] = channel['name']
25
25
  end
26
26
  retval
27
27
  end
28
28
 
29
29
  def names_to_ids
30
30
  retval = {}
31
- @response["channels"].each do |channel|
32
- retval[channel["name"]] = channel["id"]
31
+ @response['channels'].each do |channel|
32
+ retval[channel['name']] = channel['id']
33
33
  end
34
34
  retval
35
35
  end
@@ -20,16 +20,16 @@ module SlackbotFrd
20
20
 
21
21
  def ids_to_names
22
22
  retval = {}
23
- @response["groups"].each do |group|
24
- retval[group["id"]] = group["name"]
23
+ @response['groups'].each do |group|
24
+ retval[group['id']] = group['name']
25
25
  end
26
26
  retval
27
27
  end
28
28
 
29
29
  def names_to_ids
30
30
  retval = {}
31
- @response["groups"].each do |group|
32
- retval[group["name"]] = group["id"]
31
+ @response['groups'].each do |group|
32
+ retval[group['name']] = group['id']
33
33
  end
34
34
  retval
35
35
  end
@@ -20,16 +20,16 @@ module SlackbotFrd
20
20
 
21
21
  def ids_to_names
22
22
  retval = {}
23
- @response["ims"].each do |im|
24
- retval[im["id"]] = im["user"]
23
+ @response['ims'].each do |im|
24
+ retval[im['id']] = im['user']
25
25
  end
26
26
  retval
27
27
  end
28
28
 
29
29
  def names_to_ids
30
30
  retval = {}
31
- @response["ims"].each do |im|
32
- retval[im["user"]] = im["id"]
31
+ @response['ims'].each do |im|
32
+ retval[im['user']] = im['id']
33
33
  end
34
34
  retval
35
35
  end
@@ -24,7 +24,7 @@ module SlackbotFrd
24
24
 
25
25
  def wss_url
26
26
  #return "ERR" unless @response.has_key?("url")
27
- @response["url"]
27
+ @response['url']
28
28
  end
29
29
  end
30
30
  end
@@ -20,16 +20,16 @@ module SlackbotFrd
20
20
 
21
21
  def ids_to_names
22
22
  retval = {}
23
- @response["members"].each do |user|
24
- retval[user["id"]] = user["name"]
23
+ @response['members'].each do |user|
24
+ retval[user['id']] = user['name']
25
25
  end
26
26
  retval
27
27
  end
28
28
 
29
29
  def names_to_ids
30
30
  retval = {}
31
- @response["members"].each do |user|
32
- retval[user["name"]] = user["id"]
31
+ @response['members'].each do |user|
32
+ retval[user['name']] = user['id']
33
33
  end
34
34
  retval
35
35
  end
data/lib/slackbot_frd.rb CHANGED
@@ -1,3 +1,3 @@
1
- Gem.find_files("slackbot_frd/**/*.rb").each do |path|
1
+ Gem.find_files('slackbot_frd/**/*.rb').each do |path|
2
2
  require path.gsub(/\.rb$/, '') unless path =~ /bot.*cli/
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slackbot_frd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Porter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-16 00:00:00.000000000 Z
11
+ date: 2015-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport