slack-smart-bot 0.4.2 → 0.5.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
  SHA256:
3
- metadata.gz: 3c4cad085b7771ff84ae60072108883b4fbde15f5a5584418789e7984936fdde
4
- data.tar.gz: 47d65333516b1dcd87c6ac8afdfe5dcf4f1bdbdf8df694b6b452b0a8e1a0841d
3
+ metadata.gz: 2e7d4c61f5af43020d2948de27af7ed5ee143f031c312c48eec1853938c42138
4
+ data.tar.gz: b064405f4cd90e726098d1dd1ab3c9e70c779144cd834489af62a1eb4ea743ac
5
5
  SHA512:
6
- metadata.gz: cb0b438bece6997bd8c554b3ad4bea1f584cae6b37e927ea8f960040604720edca6f01a645ee18b0405ee1130580724218947adf561ecb98f9591da738bbc83c
7
- data.tar.gz: 41a36c012dde42dd32c59a2070504919f9adfed530ec227df08efc571d3088c2e21f7e266662eb001235f5b144d36d9be009a46dcd0fa3ca861671ba91901100
6
+ metadata.gz: 514ad2e812b624790e75116fac89a98873218c72f250ce303c18bd27b2492f96c59d0a75d30fd84b7f7dc6036dc9cd3e242a0128fccb8ffce5054619e4d3abd9
7
+ data.tar.gz: a6ff2c48d41f219816ac4a658fe9cb7b71f5da47c9aa542f5b2a409c6b81ca4d60f59fbcc576d9dfe70c860bb2ff254cb4df88073cfb6595ecae9e69cf8e07a8
data/README.md CHANGED
@@ -57,7 +57,7 @@ The rules file can be edited and will be only affecting this particular bot.
57
57
  You can add all the rules you want for your bot in the rules file, this is an example:
58
58
 
59
59
  ```ruby
60
- def rules(user, command, processed, id_user)
60
+ def rules(user, command, processed, dest)
61
61
  from = user.name
62
62
  firstname = from.split(" ").first
63
63
  case command
@@ -142,25 +142,63 @@ class SlackSmartBot
142
142
  end
143
143
  end
144
144
 
145
+ #help: ===================================
146
+ #help:
147
+ #help: *Commands from Channels without a bot:*
148
+ #help:
149
+ #help: ----------------------------------------------
150
+ #help:
151
+ #help: `@BOT_NAME on #CHANNEL_NAME COMMAND`
152
+ #help: `@BOT_NAME #CHANNEL_NAME COMMAND`
153
+ #help: It will run the supplied command using the rules on the channel supplied.
154
+ #help: You need to join the specified channel to be able to use those rules.
155
+ #help: The only
145
156
  def listen
146
157
  @salutations = [config[:nick], config[:nick_id], "bot", "smart"]
147
158
  client.on :message do |data|
148
- if data.channel[0] == "D" #Direct message
149
- id_user = data.user
150
- else
151
- id_user = nil
159
+ if data.channel[0] == 'D' or data.channel[0] == "C" #Direct message or Channel
160
+ dest = data.channel
161
+ else # not treated
162
+ dest = nil
152
163
  end
164
+ #todo: sometimes data.user is nil, check the problem.
165
+ @logger.warn "!dest is nil. user: #{data.user}, channel: #{data.channel}, message: #{data.text}" if dest.nil?
153
166
  # Direct messages are treated only on the master bot
154
- if id_user.nil? or (!id_user.nil? and ON_MASTER_BOT)
155
- #todo: sometimes data.user is nil, check the problem.
156
- if data.user.nil?
157
- @logger.fatal "user is nil. channel: #{data.channel}, message: #{data.text}"
158
- else
159
- user_info = wclient.users_info(user: data.user)
160
- if !id_user.nil? or @channels_id[CHANNEL] == data.channel or user_info.user.name == config[:nick]
161
- res = process_first(user_info.user, data.text, id_user, data.channel)
162
- next if res.to_s == "next"
167
+ @logger.info dest.inspect
168
+ @logger.info data.text
169
+ if !dest.nil? and ((dest[0] == 'D' and ON_MASTER_BOT) or (dest[0] == 'C'))
170
+ user_info = wclient.users_info(user: data.user)
171
+ #todo: check to remove user_info.user.name == config[:nick] since I think we will never get messages from the bot on slack
172
+ # if Direct message or we are in the channel of the bot
173
+ if dest[0]=='D' or @channels_id[CHANNEL] == data.channel or user_info.user.name == config[:nick]
174
+ res = process_first(user_info.user, data.text, dest, data.channel)
175
+ next if res.to_s == "next"
176
+ # if @botname on #channel_rules: do something
177
+ elsif data.text.match(/^<@#{config[:nick_id]}>\s(on\s)?<#(\w+)\|(.+)>\s*:?\s*(.+)$/i)
178
+ channel_rules = $2
179
+ channel_rules_name = $3
180
+ command = $4
181
+ command = "!" + command unless command[0]=="!"
182
+
183
+ if @channels_id[CHANNEL] == channel_rules #to be treated only on the bot of the requested channel
184
+ dest = data.channel
185
+ channels = wclient.channels_list.channels
186
+ channel_found = channels.detect { |c| c.name == channel_rules_name }
187
+ if channel_found.nil?
188
+ @logger.fatal "Not possible to find the channel #{channel_rules_name}"
189
+ elsif channel_found.name == MASTER_CHANNEL
190
+ respond "You cannot use the rules from Master Channel on any other channel.", dest
191
+ elsif user_info.user.id == channel_found.creator or channel_found.members.include?(user_info.user.id)
192
+ res = process_first(user_info.user, command, dest, channel_rules)
193
+ next if res.to_s == "next"
194
+ else
195
+ respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
196
+ end
163
197
  end
198
+ elsif @questions.keys.include?(user_info.user.name)
199
+ dest = data.channel
200
+ res = process_first(user_info.user, data.text, dest, @channels_id[CHANNEL])
201
+ next if res.to_s == "next"
164
202
  end
165
203
  end
166
204
  end
@@ -169,7 +207,7 @@ class SlackSmartBot
169
207
  client.start!
170
208
  end
171
209
 
172
- def process_first(user, text, id_user, dchannel)
210
+ def process_first(user, text, dest, dchannel)
173
211
  nick = user.name
174
212
  #todo: verify if on slack on anytime nick == config[:nick]
175
213
  if nick == config[:nick] or nick == (config[:nick] + " · Bot") #if message is coming from the bot
@@ -204,7 +242,7 @@ class SlackSmartBot
204
242
  elsif @shortcuts.keys.include?(:all) and @shortcuts[:all].keys.include?(shortcut)
205
243
  text = @shortcuts[:all][shortcut].dup
206
244
  else
207
- respond "Shortcut not found", id_user
245
+ respond "Shortcut not found", dest
208
246
  return :next
209
247
  end
210
248
  text = "!" + text if addexcl and text[0] != "!"
@@ -219,7 +257,7 @@ class SlackSmartBot
219
257
  begin
220
258
  t = Thread.new do
221
259
  begin
222
- processed = process(user, command, id_user, dchannel)
260
+ processed = process(user, command, dest, dchannel)
223
261
  @logger.info "command: #{nick}> #{command}" if processed
224
262
  on_demand = false
225
263
  if command.match(/^@?(#{config[:nick]}):*\s+(.+)$/i) or
@@ -231,10 +269,10 @@ class SlackSmartBot
231
269
  if @status == :on and
232
270
  (@questions.keys.include?(nick) or
233
271
  @listening.include?(nick) or
234
- !id_user.nil? or on_demand)
272
+ dest[0]=='D' or on_demand)
235
273
  @logger.info "command: #{nick}> #{command}" unless processed
236
274
  #todo: verify this
237
- if id_user.nil? #only for channels, not for DM
275
+ if dest[0]=='C' #only for channels, not for DM
238
276
  rules_file = RULES_FILE
239
277
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
240
278
  unless @bots_created.key?(@rules_imported[user.id][dchannel])
@@ -256,7 +294,7 @@ class SlackSmartBot
256
294
  if defined?(rules)
257
295
  command[0] = "" if command[0] == "!"
258
296
  command.gsub!(/^@\w+:*\s*/, "")
259
- rules(user, command, processed, id_user)
297
+ rules(user, command, processed, dest)
260
298
  else
261
299
  @logger.warn "It seems like rules method is not defined"
262
300
  end
@@ -281,7 +319,7 @@ class SlackSmartBot
281
319
  if defined?(rules)
282
320
  command[0] = "" if command[0] == "!"
283
321
  command.gsub!(/^@\w+:*\s*/, "")
284
- rules(user, command, processed, id_user)
322
+ rules(user, command, processed, dest)
285
323
  else
286
324
  @logger.warn "It seems like rules method is not defined"
287
325
  end
@@ -289,7 +327,7 @@ class SlackSmartBot
289
327
  @logger.info "it is a direct message with no rules file selected so no rules file executed."
290
328
  unless processed
291
329
  resp = ["what", "huh", "sorry", "what do you mean", "I don't understand"].sample
292
- respond "#{resp}?", id_user
330
+ respond "#{resp}?", dest
293
331
  end
294
332
  end
295
333
  end
@@ -306,7 +344,7 @@ class SlackSmartBot
306
344
  #help:
307
345
  #help: *General commands:*
308
346
  #help:
309
- def process(user, command, id_user, dchannel)
347
+ def process(user, command, dest, dchannel)
310
348
  from = user.name
311
349
  firstname = from.split(/ /).first
312
350
  processed = true
@@ -317,17 +355,17 @@ class SlackSmartBot
317
355
  #help: `Hello Bot`
318
356
  #help: `Hello Smart`
319
357
  #help: `Hello THE_NAME_OF_THE_BOT`
320
- #help: Also apart of Hello you can use _Hallo, Hi, Hola, What's up, Hey, Hæ_
358
+ #help: Also apart of Hello you can use _Hallo, Hi, Hola, What's up, Hey, Hæ_
321
359
  #help: Bot starts listening to you
322
360
  #help:
323
361
  when /^(Hello|Hallo|Hi|Hola|What's\sup|Hey|Hæ)\s(#{@salutations.join("|")})\s*$/i
324
362
  if @status == :on
325
363
  greetings = ["Hello", "Hallo", "Hi", "Hola", "What's up", "Hey", "Hæ"].sample
326
- respond "#{greetings} #{firstname}", id_user
327
- if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and !id_user.nil?
328
- respond "You are using specific rules for channel: <##{@rules_imported[user.id][user.id]}>", id_user
329
- elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and id_user.nil?
330
- respond "You are using specific rules for channel: <##{@rules_imported[user.id][dchannel]}>", id_user
364
+ respond "#{greetings} #{firstname}", dest
365
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0]=='D'
366
+ respond "You are using specific rules for channel: <##{@rules_imported[user.id][user.id]}>", dest
367
+ elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and dest[0]=='C'
368
+ respond "You are using specific rules for channel: <##{@rules_imported[user.id][dchannel]}>", dest
331
369
  end
332
370
  @listening << from unless @listening.include?(from)
333
371
  end
@@ -336,13 +374,13 @@ class SlackSmartBot
336
374
  #help: `Bye Bot`
337
375
  #help: `Bye Smart`
338
376
  #help: `Bye NAME_OF_THE_BOT`
339
- #help: Also apart of Bye you can use _Bæ, Good Bye, Adiós, Ciao, Bless, Bless Bless, Adeu_
377
+ #help: Also apart of Bye you can use _Bæ, Good Bye, Adiós, Ciao, Bless, Bless Bless, Adeu_
340
378
  #help: Bot stops listening to you
341
379
  #help:
342
380
  when /^(Bye|Bæ|Good\sBye|Adiós|Ciao|Bless|Bless\sBless|Adeu)\s(#{@salutations.join("|")})\s*$/i
343
381
  if @status == :on
344
382
  bye = ["Bye", "Bæ", "Good Bye", "Adiós", "Ciao", "Bless", "Bless bless", "Adeu"].sample
345
- respond "#{bye} #{firstname}", id_user
383
+ respond "#{bye} #{firstname}", dest
346
384
  @listening.delete(from)
347
385
  end
348
386
 
@@ -357,12 +395,12 @@ class SlackSmartBot
357
395
  if ON_MASTER_BOT
358
396
  if ADMIN_USERS.include?(from) #admin user
359
397
  unless @questions.keys.include?(from)
360
- ask("are you sure?", command, from, id_user)
398
+ ask("are you sure?", command, from, dest)
361
399
  else
362
400
  case @questions[from]
363
401
  when /yes/i, /yep/i, /sure/i
364
- respond "Game over!", id_user
365
- respond "Ciao #{firstname}!", id_user
402
+ respond "Game over!", dest
403
+ respond "Ciao #{firstname}!", dest
366
404
  @bots_created.each { |key, value|
367
405
  value[:thread] = ""
368
406
  send_msg_channel(key, "Bot has been closed by #{from}")
@@ -373,17 +411,17 @@ class SlackSmartBot
373
411
  exit!
374
412
  when /no/i, /nope/i, /cancel/i
375
413
  @questions.delete(from)
376
- respond "Thanks, I'm happy to be alive", id_user
414
+ respond "Thanks, I'm happy to be alive", dest
377
415
  else
378
- respond "I don't understand", id_user
379
- ask("are you sure do you want me to close? (yes or no)", "quit bot", from, id_user)
416
+ respond "I don't understand", dest
417
+ ask("are you sure do you want me to close? (yes or no)", "quit bot", from, dest)
380
418
  end
381
419
  end
382
420
  else
383
- respond "Only admin users can kill me", id_user
421
+ respond "Only admin users can kill me", dest
384
422
  end
385
423
  else
386
- respond "To do this you need to be an admin user in the master channel: <##{@channels_id[MASTER_CHANNEL]}>", id_user
424
+ respond "To do this you need to be an admin user in the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
387
425
  end
388
426
 
389
427
  #helpadmin: ----------------------------------------------
@@ -394,14 +432,14 @@ class SlackSmartBot
394
432
  #helpadmin:
395
433
  when /^start\s(this\s)?bot$/i
396
434
  if ADMIN_USERS.include?(from) #admin user
397
- respond "This bot is running and listening from now on. You can pause again: pause this bot", id_user
435
+ respond "This bot is running and listening from now on. You can pause again: pause this bot", dest
398
436
  @status = :on
399
437
  unless ON_MASTER_BOT
400
438
  get_channels_name_and_id() unless @channels_name.keys.include?(MASTER_CHANNEL) and @channels_name.keys.include?(CHANNEL)
401
439
  send_msg_channel @channels_name[MASTER_CHANNEL], "Changed status on #{@channels_name[CHANNEL]} to :on"
402
440
  end
403
441
  else
404
- respond "Only admin users can change my status", id_user
442
+ respond "Only admin users can change my status", dest
405
443
  end
406
444
 
407
445
  #helpadmin: ----------------------------------------------
@@ -412,15 +450,15 @@ class SlackSmartBot
412
450
  #helpadmin:
413
451
  when /^pause\s(this\s)?bot$/i
414
452
  if ADMIN_USERS.include?(from) #admin user
415
- respond "This bot is paused from now on. You can start it again: start this bot", id_user
416
- respond "zZzzzzZzzzzZZZZZZzzzzzzzz", id_user
453
+ respond "This bot is paused from now on. You can start it again: start this bot", dest
454
+ respond "zZzzzzZzzzzZZZZZZzzzzzzzz", dest
417
455
  @status = :paused
418
456
  unless ON_MASTER_BOT
419
457
  get_channels_name_and_id() unless @channels_name.keys.include?(MASTER_CHANNEL) and @channels_name.keys.include?(CHANNEL)
420
458
  send_msg_channel @channels_name[MASTER_CHANNEL], "Changed status on #{@channels_name[CHANNEL]} to :paused"
421
459
  end
422
460
  else
423
- respond "Only admin users can put me on pause", id_user
461
+ respond "Only admin users can put me on pause", dest
424
462
  end
425
463
 
426
464
  #helpadmin: ----------------------------------------------
@@ -429,12 +467,12 @@ class SlackSmartBot
429
467
  #helpadmin: If on master channel and admin user also it will display info about bots created
430
468
  #helpadmin:
431
469
  when /^bot\sstatus/i
432
- respond "Status: #{@status}. Version: #{VERSION}. Rules file: #{File.basename RULES_FILE} ", id_user
470
+ respond "Status: #{@status}. Version: #{VERSION}. Rules file: #{File.basename RULES_FILE} ", dest
433
471
  if @status == :on
434
- respond "I'm listening to [#{@listening.join(", ")}]", id_user
472
+ respond "I'm listening to [#{@listening.join(", ")}]", dest
435
473
  if ON_MASTER_BOT and ADMIN_USERS.include?(from)
436
474
  @bots_created.each { |key, value|
437
- respond "#{key}: #{value}", id_user
475
+ respond "#{key}: #{value}", dest
438
476
  }
439
477
  end
440
478
  end
@@ -460,13 +498,13 @@ class SlackSmartBot
460
498
  channel_found = channels.detect { |c| c.name == channel }
461
499
 
462
500
  if channel_id.nil?
463
- respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", id_user
501
+ respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
464
502
  elsif channel == MASTER_CHANNEL
465
- respond "There is already a bot in this channel: #{channel}", id_user
503
+ respond "There is already a bot in this channel: #{channel}", dest
466
504
  elsif @bots_created.keys.include?(channel_id)
467
- respond "There is already a bot in this channel: #{channel}, kill it before", id_user
505
+ respond "There is already a bot in this channel: #{channel}, kill it before", dest
468
506
  elsif config[:nick_id] != channel_found.creator and !channel_found.members.include?(config[:nick_id])
469
- respond "You need to add first to the channel the smart bot user: #{config[:nick]}, kill it before", id_user
507
+ respond "You need to add first to the channel the smart bot user: #{config[:nick]}, kill it before", dest
470
508
  else
471
509
  if channel_id != config[:channel]
472
510
  begin
@@ -498,22 +536,22 @@ class SlackSmartBot
498
536
  admins: admin_users.join(","),
499
537
  thread: t,
500
538
  }
501
- respond "The bot has been created on channel: #{channel}. Rules file: #{File.basename rules_file}", id_user
539
+ respond "The bot has been created on channel: #{channel}. Rules file: #{File.basename rules_file}", dest
502
540
  update_bots_file()
503
541
  rescue Exception => stack
504
542
  @logger.fatal stack
505
543
  message = "Problem creating the bot on channel #{channel}. Error: <#{stack}>."
506
544
  @logger.error message
507
- respond message, id_user
545
+ respond message, dest
508
546
  end
509
547
  else
510
- respond "There is already a bot in this channel: #{channel}, and it is the Master Channel!", id_user
548
+ respond "There is already a bot in this channel: #{channel}, and it is the Master Channel!", dest
511
549
  end
512
550
  end
513
551
  else
514
552
  @logger.info MASTER_CHANNEL
515
553
  @logger.info @channel_id.inspect
516
- respond "Sorry I cannot create bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", id_user
554
+ respond "Sorry I cannot create bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
517
555
  end
518
556
 
519
557
  #helpmaster: ----------------------------------------------
@@ -534,7 +572,7 @@ class SlackSmartBot
534
572
  channel_id = @channels_id[channel]
535
573
  end
536
574
  if channel_id.nil?
537
- respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", id_user
575
+ respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
538
576
  elsif @bots_created.keys.include?(channel_id)
539
577
  if @bots_created[channel_id][:admins].split(",").include?(from)
540
578
  if @bots_created[channel_id][:thread].kind_of?(Thread) and @bots_created[channel_id][:thread].alive?
@@ -542,16 +580,16 @@ class SlackSmartBot
542
580
  end
543
581
  @bots_created.delete(channel_id)
544
582
  update_bots_file()
545
- respond "Bot on channel: #{channel}, has been killed and deleted.", id_user
583
+ respond "Bot on channel: #{channel}, has been killed and deleted.", dest
546
584
  send_msg_channel(channel, "Bot has been killed by #{from}")
547
585
  else
548
- respond "You need to be the creator or an admin of that bot channel", id_user
586
+ respond "You need to be the creator or an admin of that bot channel", dest
549
587
  end
550
588
  else
551
- respond "There is no bot in this channel: #{channel}", id_user
589
+ respond "There is no bot in this channel: #{channel}", dest
552
590
  end
553
591
  else
554
- respond "Sorry I cannot kill bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", id_user
592
+ respond "Sorry I cannot kill bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
555
593
  end
556
594
 
557
595
  #help: ----------------------------------------------
@@ -565,21 +603,21 @@ class SlackSmartBot
565
603
  channels = wclient.channels_list.channels
566
604
  channel_found = channels.detect { |c| c.name == channel }
567
605
  if channel_found.nil?
568
- respond "The channel you are trying to use doesn't exist", id_user
606
+ respond "The channel you are trying to use doesn't exist", dest
569
607
  elsif channel_found.name == MASTER_CHANNEL
570
- respond "You cannot use the rules from Master Channel on any other channel.", id_user
608
+ respond "You cannot use the rules from Master Channel on any other channel.", dest
571
609
  else
572
610
  if user.id == channel_found.creator or channel_found.members.include?(user.id)
573
611
  @rules_imported[user.id] = {} unless @rules_imported.key?(user.id)
574
- if id_user.nil? #todo: take in consideration bots that are not master
612
+ if dest[0]=="C" #todo: take in consideration bots that are not master
575
613
  @rules_imported[user.id][dchannel] = channel_found.id
576
614
  else
577
615
  @rules_imported[user.id][user.id] = channel_found.id
578
616
  end
579
617
  update_rules_imported() if ON_MASTER_BOT
580
- respond "I'm using now the rules from <##{channel_found.id}>", id_user
618
+ respond "I'm using now the rules from <##{channel_found.id}>", dest
581
619
  else
582
- respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", id_user
620
+ respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
583
621
  end
584
622
  end
585
623
 
@@ -595,29 +633,29 @@ class SlackSmartBot
595
633
  else
596
634
  channel_id = channel
597
635
  end
598
- if id_user.nil? #channel
636
+ if dest[0]=='C' #channel
599
637
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
600
638
  if @rules_imported[user.id][dchannel] != channel_id
601
- respond "You are not using those rules.", id_user
639
+ respond "You are not using those rules.", dest
602
640
  else
603
641
  @rules_imported[user.id].delete(dchannel)
604
642
  update_rules_imported() if ON_MASTER_BOT
605
- respond "You won't be using those rules from now on.", id_user
643
+ respond "You won't be using those rules from now on.", dest
606
644
  end
607
645
  else
608
- respond "You were not using those rules.", id_user
646
+ respond "You were not using those rules.", dest
609
647
  end
610
648
  else #direct message
611
649
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id)
612
650
  if @rules_imported[user.id][user.id] != channel_id
613
- respond "You are not using those rules.", id_user
651
+ respond "You are not using those rules.", dest
614
652
  else
615
653
  @rules_imported[user.id].delete(user.id)
616
654
  update_rules_imported() if ON_MASTER_BOT
617
- respond "You won't be using those rules from now on.", id_user
655
+ respond "You won't be using those rules from now on.", dest
618
656
  end
619
657
  else
620
- respond "You were not using those rules.", id_user
658
+ respond "You were not using those rules.", dest
621
659
  end
622
660
  end
623
661
 
@@ -636,14 +674,14 @@ class SlackSmartBot
636
674
  if !specific
637
675
  help_message = IO.readlines(__FILE__).join
638
676
  if ADMIN_USERS.include?(from) #admin user
639
- respond "*Commands for administrators:*\n#{help_message.scan(/#\s*help\s*admin:(.*)/).join("\n")}", id_user
677
+ respond "*Commands for administrators:*\n#{help_message.scan(/#\s*help\s*admin:(.*)/).join("\n")}", dest
640
678
  end
641
- if ON_MASTER_BOT and id_user.nil?
642
- respond "*Commands only on Master Channel <##{@channels_id[MASTER_CHANNEL]}>:*\n#{help_message.scan(/#\s*help\s*master:(.*)/).join("\n")}", id_user
679
+ if ON_MASTER_BOT and dest[0]=="C"
680
+ respond "*Commands only on Master Channel <##{@channels_id[MASTER_CHANNEL]}>:*\n#{help_message.scan(/#\s*help\s*master:(.*)/).join("\n")}", dest
643
681
  end
644
- respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), id_user
682
+ respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), dest
645
683
  end
646
- if id_user.nil? # on a channel
684
+ if dest[0]=="C" # on a channel
647
685
  rules_file = RULES_FILE
648
686
 
649
687
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
@@ -655,12 +693,12 @@ class SlackSmartBot
655
693
  end
656
694
  if @bots_created.key?(@rules_imported[user.id][dchannel])
657
695
  rules_file = @bots_created[@rules_imported[user.id][dchannel]][:rules_file]
658
- respond "*You are using rules from another channel: <##{@rules_imported[user.id][dchannel]}>. These are the specific commands for that channel:*", id_user
696
+ respond "*You are using rules from another channel: <##{@rules_imported[user.id][dchannel]}>. These are the specific commands for that channel:*", dest
659
697
  end
660
698
  end
661
699
  help_message_rules = IO.readlines(rules_file).join
662
- respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), id_user
663
- elsif !id_user.nil? and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
700
+ respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
701
+ elsif dest[0]=='D' and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
664
702
  unless @bots_created.key?(@rules_imported[user.id][user.id])
665
703
  file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
666
704
  unless file_conf.to_s() == ""
@@ -669,12 +707,12 @@ class SlackSmartBot
669
707
  end
670
708
  if @bots_created.key?(@rules_imported[user.id][user.id])
671
709
  rules_file = @bots_created[@rules_imported[user.id][user.id]][:rules_file]
672
- respond "*You are using rules from channel: <##{@rules_imported[user.id][user.id]}>. These are the specific commands for that channel:*", id_user
710
+ respond "*You are using rules from channel: <##{@rules_imported[user.id][user.id]}>. These are the specific commands for that channel:*", dest
673
711
  help_message_rules = IO.readlines(rules_file).join
674
- respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), id_user
712
+ respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
675
713
  end
676
714
  end
677
- respond "Github project: https://github.com/MarioRuiz/slack-smart-bot", id_user if !specific
715
+ respond "Github project: https://github.com/MarioRuiz/slack-smart-bot", dest if !specific
678
716
  else
679
717
  processed = false
680
718
  end
@@ -691,7 +729,7 @@ class SlackSmartBot
691
729
  if @status == :on and
692
730
  (@questions.keys.include?(from) or
693
731
  @listening.include?(from) or
694
- !id_user.nil? or on_demand)
732
+ dest[0]=='D' or on_demand)
695
733
  processed2 = true
696
734
 
697
735
  #help: ===================================
@@ -725,31 +763,31 @@ class SlackSmartBot
725
763
  @shortcuts[from] = Hash.new() unless @shortcuts.keys.include?(from)
726
764
 
727
765
  if !ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut_name) and !@shortcuts[from].include?(shortcut_name)
728
- respond "Only the creator of the shortcut or an admin user can modify it", id_user
766
+ respond "Only the creator of the shortcut or an admin user can modify it", dest
729
767
  elsif !@shortcuts[from].include?(shortcut_name)
730
768
  #new shortcut
731
769
  @shortcuts[from][shortcut_name] = command_to_run
732
770
  @shortcuts[:all][shortcut_name] = command_to_run if for_all.to_s != ""
733
771
  update_shortcuts_file()
734
- respond "shortcut added", id_user
772
+ respond "shortcut added", dest
735
773
  else
736
774
 
737
775
  #are you sure? to avoid overwriting existing
738
776
  unless @questions.keys.include?(from)
739
- ask("The shortcut already exists, are you sure you want to overwrite it?", command, from, id_user)
777
+ ask("The shortcut already exists, are you sure you want to overwrite it?", command, from, dest)
740
778
  else
741
779
  case @questions[from]
742
780
  when /^(yes|yep)/i
743
781
  @shortcuts[from][shortcut_name] = command_to_run
744
782
  @shortcuts[:all][shortcut_name] = command_to_run if for_all.to_s != ""
745
783
  update_shortcuts_file()
746
- respond "shortcut added", id_user
784
+ respond "shortcut added", dest
747
785
  @questions.delete(from)
748
786
  when /^no/i
749
- respond "ok, I won't add it", id_user
787
+ respond "ok, I won't add it", dest
750
788
  @questions.delete(from)
751
789
  else
752
- respond "I don't understand, yes or no?", id_user
790
+ respond "I don't understand, yes or no?", dest
753
791
  end
754
792
  end
755
793
  end
@@ -764,30 +802,30 @@ class SlackSmartBot
764
802
  deleted = false
765
803
 
766
804
  if !ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut) and !@shortcuts[from].include?(shortcut)
767
- respond "Only the creator of the shortcut or an admin user can delete it", id_user
805
+ respond "Only the creator of the shortcut or an admin user can delete it", dest
768
806
  elsif (@shortcuts.keys.include?(from) and @shortcuts[from].keys.include?(shortcut)) or
769
807
  (ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut))
770
808
  #are you sure? to avoid deleting by mistake
771
809
  unless @questions.keys.include?(from)
772
- ask("are you sure you want to delete it?", command, from, id_user)
810
+ ask("are you sure you want to delete it?", command, from, dest)
773
811
  else
774
812
  case @questions[from]
775
813
  when /^(yes|yep)/i
776
- respond "shortcut deleted!", id_user
777
- respond "#{shortcut}: #{@shortcuts[from][shortcut]}", id_user
814
+ respond "shortcut deleted!", dest
815
+ respond "#{shortcut}: #{@shortcuts[from][shortcut]}", dest
778
816
  @shortcuts[from].delete(shortcut)
779
817
  @shortcuts[:all].delete(shortcut)
780
818
  @questions.delete(from)
781
819
  update_shortcuts_file()
782
820
  when /^no/i
783
- respond "ok, I won't delete it", id_user
821
+ respond "ok, I won't delete it", dest
784
822
  @questions.delete(from)
785
823
  else
786
- respond "I don't understand, yes or no?", id_user
824
+ respond "I don't understand, yes or no?", dest
787
825
  end
788
826
  end
789
827
  else
790
- respond "shortcut not found", id_user
828
+ respond "shortcut not found", dest
791
829
  end
792
830
 
793
831
  #help: ----------------------------------------------
@@ -802,7 +840,7 @@ class SlackSmartBot
802
840
  @shortcuts[:all].each { |name, value|
803
841
  msg += " _#{name}: #{value}_\n"
804
842
  }
805
- respond msg, id_user
843
+ respond msg, dest
806
844
  end
807
845
 
808
846
  if @shortcuts.keys.include?(from) and @shortcuts[from].keys.size > 0
@@ -813,10 +851,10 @@ class SlackSmartBot
813
851
  new_hash.each { |name, value|
814
852
  msg += " _#{name}: #{value}_\n"
815
853
  }
816
- respond msg, id_user
854
+ respond msg, dest
817
855
  end
818
856
  end
819
- respond "No shortcuts found", id_user if msg == ""
857
+ respond "No shortcuts found", dest if msg == ""
820
858
 
821
859
  #help: ----------------------------------------------
822
860
  #help: `id channel CHANNEL_NAME`
@@ -826,9 +864,9 @@ class SlackSmartBot
826
864
  channel_name = $1
827
865
  get_channels_name_and_id()
828
866
  if @channels_id.keys.include?(channel_name)
829
- respond "the id of #{channel_name} is #{@channels_id[channel_name]}", id_user
867
+ respond "the id of #{channel_name} is #{@channels_id[channel_name]}", dest
830
868
  else
831
- respond "channel: #{channel_name} not found", id_user
869
+ respond "channel: #{channel_name} not found", dest
832
870
  end
833
871
 
834
872
  #help: ----------------------------------------------
@@ -849,18 +887,18 @@ class SlackSmartBot
849
887
  stdout, stderr, status = Open3.capture3("ruby -e \"#{code.gsub('"', '\"')}\"")
850
888
  if stderr == ""
851
889
  if stdout == ""
852
- respond "Nothing returned. Remember you need to use p or puts to print", id_user
890
+ respond "Nothing returned. Remember you need to use p or puts to print", dest
853
891
  else
854
- respond stdout, id_user
892
+ respond stdout, dest
855
893
  end
856
894
  else
857
- respond stderr, id_user
895
+ respond stderr, dest
858
896
  end
859
897
  rescue Exception => exc
860
- respond exc, id_user
898
+ respond exc, dest
861
899
  end
862
900
  else
863
- respond "Sorry I cannot run this due security issues", id_user
901
+ respond "Sorry I cannot run this due security issues", dest
864
902
  end
865
903
  else
866
904
  processed2 = false
@@ -871,21 +909,27 @@ class SlackSmartBot
871
909
  return processed
872
910
  end
873
911
 
874
- def respond(msg, id_user = nil)
875
- if id_user.nil?
912
+ def respond(msg, dest = nil)
913
+ if dest.nil?
876
914
  client.message(channel: @channels_id[CHANNEL], text: msg, as_user: true)
877
- else #private message
878
- send_msg_user(id_user, msg)
915
+ elsif dest[0]=="C" # channel
916
+ client.message(channel: dest, text: msg, as_user: true)
917
+ elsif dest[0]=='D' # Direct message
918
+ send_msg_user(dest, msg)
919
+ else
920
+ @logger.warn("method respond not treated correctly: msg:#{msg} dest:#{dest}")
879
921
  end
880
922
  end
881
923
 
882
924
  #context: previous message
883
925
  #to: user that should answer
884
- def ask(question, context, to, id_user = nil)
885
- if id_user.nil?
926
+ def ask(question, context, to, dest = nil)
927
+ if dest.nil?
886
928
  client.message(channel: @channels_id[CHANNEL], text: "#{to}: #{question}", as_user: true)
887
- else #private message
888
- send_msg_user(id_user, "#{to}: #{question}")
929
+ elsif dest[0]=="C" # channel
930
+ client.message(channel: dest, text: "#{to}: #{question}", as_user: true)
931
+ elsif dest[0]=='D' #private message
932
+ send_msg_user(dest, "#{to}: #{question}")
889
933
  end
890
934
  @questions[to] = context
891
935
  end
@@ -909,8 +953,12 @@ class SlackSmartBot
909
953
  #to send messages without listening for a response to users
910
954
  def send_msg_user(id_user, msg)
911
955
  unless msg == ""
912
- im = wclient.im_open(user: id_user)
913
- client.message(channel: im["channel"]["id"], as_user: true, text: msg)
956
+ if id_user[0]=="D"
957
+ client.message(channel: id_user, as_user: true, text: msg)
958
+ else
959
+ im = wclient.im_open(user: id_user)
960
+ client.message(channel: im["channel"]["id"], as_user: true, text: msg)
961
+ end
914
962
  end
915
963
  end
916
964
 
@@ -5,13 +5,13 @@ else
5
5
  @testing = true
6
6
  @questions = Hash.new()
7
7
 
8
- def respond(message, id_user)
8
+ def respond(message, dest)
9
9
  puts message
10
10
  end
11
11
 
12
12
  #context: previous message
13
13
  #to: user that should answer
14
- def ask(question, context, to, id_user)
14
+ def ask(question, context, to, dest)
15
15
  puts "Bot: #{question}"
16
16
  @questions[to] = context
17
17
  end
@@ -30,7 +30,7 @@ end
30
30
  # help: `@NAME_OF_BOT THE_COMMAND`
31
31
  # help: `NAME_OF_BOT THE_COMMAND`
32
32
  # help:
33
- def rules(user, command, processed, id_user)
33
+ def rules(user, command, processed, dest)
34
34
  from = user.name
35
35
  if @testing
36
36
  puts "#{from}: #{command}"
@@ -48,7 +48,7 @@ def rules(user, command, processed, id_user)
48
48
  # help: repeats SOMETHING
49
49
  # help:
50
50
  when /^echo\s(.+)/i
51
- respond $1, id_user
51
+ respond $1, dest
52
52
 
53
53
  # help: ----------------------------------------------
54
54
  # help: `go to sleep`
@@ -56,26 +56,26 @@ def rules(user, command, processed, id_user)
56
56
  # help:
57
57
  when /^go\sto\ssleep/i
58
58
  unless @questions.keys.include?(from)
59
- ask("do you want me to take a siesta?", command, from, id_user)
59
+ ask("do you want me to take a siesta?", command, from, dest)
60
60
  else
61
61
  case @questions[from]
62
62
  when /yes/i, /yep/i, /sure/i
63
63
  @questions.delete(from)
64
- respond "zZzzzzzZZZZZZzzzzzzz!", id_user
65
- respond "I'll be sleeping for 5 secs... just for you", id_user
64
+ respond "zZzzzzzZZZZZZzzzzzzz!", dest
65
+ respond "I'll be sleeping for 5 secs... just for you", dest
66
66
  sleep 5
67
67
  when /no/i, /nope/i, /cancel/i
68
68
  @questions.delete(from)
69
- respond "Thanks, I'm happy to be awake", id_user
69
+ respond "Thanks, I'm happy to be awake", dest
70
70
  else
71
- respond "I don't understand", id_user
72
- ask("are you sure do you want me to sleep? (yes or no)", "go to sleep", from, id_user)
71
+ respond "I don't understand", dest
72
+ ask("are you sure do you want me to sleep? (yes or no)", "go to sleep", from, dest)
73
73
  end
74
74
  end
75
75
  else
76
76
  unless processed
77
77
  resp = %w{ what huh sorry }.sample
78
- respond "#{firstname}: #{resp}?", id_user
78
+ respond "#{firstname}: #{resp}?", dest
79
79
  end
80
80
  end
81
81
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-smart-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-28 00:00:00.000000000 Z
11
+ date: 2019-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-ruby-client