slack-smart-bot 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/slack-smart-bot.rb +70 -69
- data/lib/slack-smart-bot_rules.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a1d9b4bd841702898c4e1d360d02feddb1925978384737ca004465c99d142b4
|
4
|
+
data.tar.gz: 7dcf3a38e80192839a80a661cfac41ce07e8a4e8e5727fbd01a983d7198b9e50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 402028ab7ccdbaceca81ea0e3c41f05abe63e460911c302891f7b62166115adb64be63a4f9fd7565b5a5b1b075d0f5eb6ba5549d8ee88550438f722638feb76a
|
7
|
+
data.tar.gz: b5c20e75ddb9fa8c180893dd436116a2b3bfa85d104b6a91479b8ebc18e64c70e233be18e77858e5600427a829cb073c44594780e30d9ff615dea1195d63864d
|
data/lib/slack-smart-bot.rb
CHANGED
@@ -6,7 +6,6 @@ require "logger"
|
|
6
6
|
require "fileutils"
|
7
7
|
require "open3"
|
8
8
|
|
9
|
-
|
10
9
|
if ARGV.size == 0
|
11
10
|
CHANNEL = MASTER_CHANNEL
|
12
11
|
ON_MASTER_BOT = true
|
@@ -29,7 +28,8 @@ SHORTCUTS_FILE = "slack-smart-bot_shortcuts_#{CHANNEL}.rb".gsub(" ", "_")
|
|
29
28
|
|
30
29
|
class SlackSmartBot
|
31
30
|
attr_accessor :config, :client, :wclient
|
32
|
-
VERSION = Gem.loaded_specs.values.select {|x| x.name==
|
31
|
+
VERSION = Gem.loaded_specs.values.select { |x| x.name == "slack-smart-bot" }[0].version.to_s
|
32
|
+
|
33
33
|
def initialize(config)
|
34
34
|
Dir.mkdir("./logs") unless Dir.exist?("./logs")
|
35
35
|
Dir.mkdir("./shortcuts") unless Dir.exist?("./shortcuts")
|
@@ -37,6 +37,7 @@ class SlackSmartBot
|
|
37
37
|
@logger = Logger.new("./logs/#{logfile}")
|
38
38
|
config_log = config.dup
|
39
39
|
config_log.delete(:token)
|
40
|
+
config[:silent] = false unless config.key?(:silent)
|
40
41
|
@logger.info "Initializing bot: #{config_log.inspect}"
|
41
42
|
|
42
43
|
config[:channel] = CHANNEL
|
@@ -101,11 +102,13 @@ class SlackSmartBot
|
|
101
102
|
@logger.info m
|
102
103
|
gems_remote = `gem list slack-smart-bot --remote`
|
103
104
|
version_remote = gems_remote.to_s().scan(/slack-smart-bot \((\d+\.\d+\.\d+)/).join
|
104
|
-
version_message =
|
105
|
+
version_message = ""
|
105
106
|
if version_remote != VERSION
|
106
107
|
version_message = ". There is a new available version: #{version_remote}."
|
107
108
|
end
|
108
|
-
|
109
|
+
unless config[:silent]
|
110
|
+
respond "Smart Bot started v#{VERSION}#{version_message}\nIf you want to know what I can do for you: `bot help`.\n`bot rules` if you want to display just the specific rules of this channel.\nYou can talk to me privately if you prefer it."
|
111
|
+
end
|
109
112
|
end
|
110
113
|
|
111
114
|
@status = STATUS_INIT
|
@@ -153,7 +156,7 @@ class SlackSmartBot
|
|
153
156
|
#help: *Commands from Channels without a bot:*
|
154
157
|
#help:
|
155
158
|
#help: ----------------------------------------------
|
156
|
-
#help:
|
159
|
+
#help:
|
157
160
|
#help: `@BOT_NAME on #CHANNEL_NAME COMMAND`
|
158
161
|
#help: `@BOT_NAME #CHANNEL_NAME COMMAND`
|
159
162
|
#help: It will run the supplied command using the rules on the channel supplied.
|
@@ -161,60 +164,58 @@ class SlackSmartBot
|
|
161
164
|
def listen
|
162
165
|
@salutations = [config[:nick], config[:nick_id], "bot", "smart"]
|
163
166
|
client.on :message do |data|
|
164
|
-
if data.channel[0] ==
|
165
|
-
|
167
|
+
if data.channel[0] == "D" or data.channel[0] == "C" #Direct message or Channel
|
168
|
+
dest = data.channel
|
166
169
|
else # not treated
|
167
170
|
dest = nil
|
168
171
|
end
|
169
172
|
#todo: sometimes data.user is nil, check the problem.
|
170
173
|
@logger.warn "!dest is nil. user: #{data.user}, channel: #{data.channel}, message: #{data.text}" if dest.nil?
|
171
174
|
# Direct messages are treated only on the master bot
|
172
|
-
if !dest.nil? and ((dest[0] ==
|
175
|
+
if !dest.nil? and ((dest[0] == "D" and ON_MASTER_BOT) or (dest[0] == "C")) and data.user.to_s != ""
|
173
176
|
begin
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
elsif data.user == channel_found.creator or members.include?(data.user)
|
204
|
-
res = process_first(user_info.user, command, dest, channel_rules)
|
205
|
-
else
|
206
|
-
respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
|
177
|
+
#todo: when changed @questions user_id then move user_info inside the ifs to avoid calling it when not necessary
|
178
|
+
user_info = wclient.users_info(user: data.user)
|
179
|
+
#todo: check to remove user_info.user.name == config[:nick] since I think we will never get messages from the bot on slack
|
180
|
+
# if Direct message or we are in the channel of the bot
|
181
|
+
if data.text.size >= 2 and
|
182
|
+
((data.text[0] == "`" and data.text[-1] == "`") or (data.text[0] == "*" and data.text[-1] == "*") or (data.text[0] == "_" and data.text[-1] == "_"))
|
183
|
+
data.text = data.text[1..-2]
|
184
|
+
end
|
185
|
+
if data.text.match(/^<@#{config[:nick_id]}>\s(on\s)?<#(\w+)\|(.+)>\s*:?\s*(.+)$/i)
|
186
|
+
channel_rules = $2
|
187
|
+
channel_rules_name = $3
|
188
|
+
command = $4
|
189
|
+
command = "!" + command unless command[0] == "!"
|
190
|
+
if @channels_id[CHANNEL] == channel_rules #to be treated only on the bot of the requested channel
|
191
|
+
dest = data.channel
|
192
|
+
|
193
|
+
channels = wclient.channels_list.channels
|
194
|
+
channel_found = channels.detect { |c| c.name == channel_rules_name }
|
195
|
+
members = wclient.conversations_members(channel: @channels_id[channel_rules_name]).members unless channel_found.nil?
|
196
|
+
|
197
|
+
if channel_found.nil?
|
198
|
+
@logger.fatal "Not possible to find the channel #{channel_rules_name}"
|
199
|
+
elsif channel_found.name == MASTER_CHANNEL
|
200
|
+
respond "You cannot use the rules from Master Channel on any other channel.", dest
|
201
|
+
elsif data.user == channel_found.creator or members.include?(data.user)
|
202
|
+
res = process_first(user_info.user, command, dest, channel_rules)
|
203
|
+
else
|
204
|
+
respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
|
205
|
+
end
|
207
206
|
end
|
207
|
+
elsif @questions.keys.include?(user_info.user.name)
|
208
|
+
#todo: @questions key should be the id not the name. change it everywhere
|
209
|
+
dest = data.channel
|
210
|
+
res = process_first(user_info.user, data.text, dest, @channels_id[CHANNEL])
|
211
|
+
elsif (dest[0] == "D" or @channels_id[CHANNEL] == data.channel or data.user == config[:nick_id]) and
|
212
|
+
data.text.size > 0 and data.text[0] != "-"
|
213
|
+
res = process_first(user_info.user, data.text, dest, data.channel)
|
214
|
+
# if @botname on #channel_rules: do something
|
208
215
|
end
|
209
|
-
|
210
|
-
|
211
|
-
dest = data.channel
|
212
|
-
res = process_first(user_info.user, data.text, dest, @channels_id[CHANNEL])
|
216
|
+
rescue Exception => stack
|
217
|
+
@logger.fatal stack
|
213
218
|
end
|
214
|
-
rescue Exception => stack
|
215
|
-
@logger.fatal stack
|
216
|
-
end
|
217
|
-
|
218
219
|
end
|
219
220
|
end
|
220
221
|
|
@@ -284,10 +285,10 @@ class SlackSmartBot
|
|
284
285
|
if @status == :on and
|
285
286
|
(@questions.keys.include?(nick) or
|
286
287
|
@listening.include?(nick) or
|
287
|
-
dest[0]==
|
288
|
+
dest[0] == "D" or on_demand)
|
288
289
|
@logger.info "command: #{nick}> #{command}" unless processed
|
289
290
|
#todo: verify this
|
290
|
-
if dest[0]==
|
291
|
+
if dest[0] == "C" #only for channels, not for DM
|
291
292
|
rules_file = RULES_FILE
|
292
293
|
if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
|
293
294
|
unless @bots_created.key?(@rules_imported[user.id][dchannel])
|
@@ -378,9 +379,9 @@ class SlackSmartBot
|
|
378
379
|
if @status == :on
|
379
380
|
greetings = ["Hello", "Hallo", "Hi", "Hola", "What's up", "Hey", "Hæ"].sample
|
380
381
|
respond "#{greetings} #{firstname}", dest
|
381
|
-
if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0]==
|
382
|
+
if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0] == "D"
|
382
383
|
respond "You are using specific rules for channel: <##{@rules_imported[user.id][user.id]}>", dest
|
383
|
-
elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and dest[0]==
|
384
|
+
elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and dest[0] == "C"
|
384
385
|
respond "You are using specific rules for channel: <##{@rules_imported[user.id][dchannel]}>", dest
|
385
386
|
end
|
386
387
|
@listening << from unless @listening.include?(from)
|
@@ -485,7 +486,7 @@ class SlackSmartBot
|
|
485
486
|
when /^bot\sstatus/i
|
486
487
|
gems_remote = `gem list slack-smart-bot --remote`
|
487
488
|
version_remote = gems_remote.to_s().scan(/slack-smart-bot \((\d+\.\d+\.\d+)/).join
|
488
|
-
version_message =
|
489
|
+
version_message = ""
|
489
490
|
if version_remote != VERSION
|
490
491
|
version_message = " There is a new available version: #{version_remote}."
|
491
492
|
end
|
@@ -512,12 +513,12 @@ class SlackSmartBot
|
|
512
513
|
all = $1
|
513
514
|
message = $2
|
514
515
|
if all.nil?
|
515
|
-
@bots_created.each do |k,v|
|
516
|
+
@bots_created.each do |k, v|
|
516
517
|
respond message, k
|
517
518
|
end
|
518
519
|
respond "Bot channels have been notified", dest
|
519
520
|
else
|
520
|
-
myconv = wclient.users_conversations(exclude_archived: true, limit: 100, types:
|
521
|
+
myconv = wclient.users_conversations(exclude_archived: true, limit: 100, types: "im, public_channel").channels
|
521
522
|
myconv.each do |c|
|
522
523
|
respond message, c.id unless c.name == MASTER_CHANNEL
|
523
524
|
end
|
@@ -658,7 +659,7 @@ class SlackSmartBot
|
|
658
659
|
else
|
659
660
|
if user.id == channel_found.creator or channel_found.members.include?(user.id)
|
660
661
|
@rules_imported[user.id] = {} unless @rules_imported.key?(user.id)
|
661
|
-
if dest[0]=="C" #todo: take in consideration bots that are not master
|
662
|
+
if dest[0] == "C" #todo: take in consideration bots that are not master
|
662
663
|
@rules_imported[user.id][dchannel] = channel_found.id
|
663
664
|
else
|
664
665
|
@rules_imported[user.id][user.id] = channel_found.id
|
@@ -682,7 +683,7 @@ class SlackSmartBot
|
|
682
683
|
else
|
683
684
|
channel_id = channel
|
684
685
|
end
|
685
|
-
if dest[0]==
|
686
|
+
if dest[0] == "C" #channel
|
686
687
|
if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
|
687
688
|
if @rules_imported[user.id][dchannel] != channel_id
|
688
689
|
respond "You are not using those rules.", dest
|
@@ -725,12 +726,12 @@ class SlackSmartBot
|
|
725
726
|
if ADMIN_USERS.include?(from) #admin user
|
726
727
|
respond "*Commands for administrators:*\n#{help_message.scan(/#\s*help\s*admin:(.*)/).join("\n")}", dest
|
727
728
|
end
|
728
|
-
if ON_MASTER_BOT and dest[0]=="C"
|
729
|
+
if ON_MASTER_BOT and dest[0] == "C"
|
729
730
|
respond "*Commands only on Master Channel <##{@channels_id[MASTER_CHANNEL]}>:*\n#{help_message.scan(/#\s*help\s*master:(.*)/).join("\n")}", dest
|
730
731
|
end
|
731
732
|
respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), dest
|
732
733
|
end
|
733
|
-
if dest[0]=="C" # on a channel
|
734
|
+
if dest[0] == "C" # on a channel
|
734
735
|
rules_file = RULES_FILE
|
735
736
|
|
736
737
|
if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
|
@@ -747,7 +748,7 @@ class SlackSmartBot
|
|
747
748
|
end
|
748
749
|
help_message_rules = IO.readlines(rules_file).join
|
749
750
|
respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
|
750
|
-
elsif dest[0]==
|
751
|
+
elsif dest[0] == "D" and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
|
751
752
|
unless @bots_created.key?(@rules_imported[user.id][user.id])
|
752
753
|
file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
|
753
754
|
unless file_conf.to_s() == ""
|
@@ -778,7 +779,7 @@ class SlackSmartBot
|
|
778
779
|
if @status == :on and
|
779
780
|
(@questions.keys.include?(from) or
|
780
781
|
@listening.include?(from) or
|
781
|
-
dest[0]==
|
782
|
+
dest[0] == "D" or on_demand)
|
782
783
|
processed2 = true
|
783
784
|
|
784
785
|
#help: ===================================
|
@@ -961,9 +962,9 @@ class SlackSmartBot
|
|
961
962
|
def respond(msg, dest = nil)
|
962
963
|
if dest.nil?
|
963
964
|
client.message(channel: @channels_id[CHANNEL], text: msg, as_user: true)
|
964
|
-
elsif dest[0]=="C" # channel
|
965
|
-
client.message(channel: dest, text: msg, as_user: true)
|
966
|
-
elsif dest[0]==
|
965
|
+
elsif dest[0] == "C" # channel
|
966
|
+
client.message(channel: dest, text: msg, as_user: true)
|
967
|
+
elsif dest[0] == "D" # Direct message
|
967
968
|
send_msg_user(dest, msg)
|
968
969
|
else
|
969
970
|
@logger.warn("method respond not treated correctly: msg:#{msg} dest:#{dest}")
|
@@ -975,9 +976,9 @@ class SlackSmartBot
|
|
975
976
|
def ask(question, context, to, dest = nil)
|
976
977
|
if dest.nil?
|
977
978
|
client.message(channel: @channels_id[CHANNEL], text: "#{to}: #{question}", as_user: true)
|
978
|
-
elsif dest[0]=="C" # channel
|
979
|
-
client.message(channel: dest, text: "#{to}: #{question}", as_user: true)
|
980
|
-
elsif dest[0]==
|
979
|
+
elsif dest[0] == "C" # channel
|
980
|
+
client.message(channel: dest, text: "#{to}: #{question}", as_user: true)
|
981
|
+
elsif dest[0] == "D" #private message
|
981
982
|
send_msg_user(dest, "#{to}: #{question}")
|
982
983
|
end
|
983
984
|
@questions[to] = context
|
@@ -1002,7 +1003,7 @@ class SlackSmartBot
|
|
1002
1003
|
#to send messages without listening for a response to users
|
1003
1004
|
def send_msg_user(id_user, msg)
|
1004
1005
|
unless msg == ""
|
1005
|
-
if id_user[0]=="D"
|
1006
|
+
if id_user[0] == "D"
|
1006
1007
|
client.message(channel: id_user, as_user: true, text: msg)
|
1007
1008
|
else
|
1008
1009
|
im = wclient.im_open(user: id_user)
|
@@ -82,8 +82,8 @@ end
|
|
82
82
|
|
83
83
|
#for the case of testing just running this file, write the dialogue in here:
|
84
84
|
if @testing
|
85
|
-
require
|
86
|
-
user = {name: "Peter Johson", id: "Uxxxxxx"}
|
85
|
+
require "nice_hash"
|
86
|
+
user = { name: "Peter Johson", id: "Uxxxxxx" }
|
87
87
|
|
88
88
|
rules user, "go to sleep, you look tired", false, nil
|
89
89
|
rules user, "yes", false, nil
|
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.6.
|
4
|
+
version: 0.6.3
|
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-06-
|
11
|
+
date: 2019-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: slack-ruby-client
|