Skip to content

Commit

Permalink
Merge pull request #28 from MarioRuiz/v1.9.1
Browse files Browse the repository at this point in the history
V1.9.2
  • Loading branch information
MarioRuiz committed Apr 27, 2021
2 parents f415ae2 + d18e3ca commit dd23939
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 173 deletions.
1 change: 1 addition & 0 deletions lib/slack-smart-bot.rb
Expand Up @@ -61,6 +61,7 @@ def initialize(config)
Dir.mkdir("#{config.path}/logs") unless Dir.exist?("#{config.path}/logs")
Dir.mkdir("#{config.path}/shortcuts") unless Dir.exist?("#{config.path}/shortcuts")
Dir.mkdir("#{config.path}/routines") unless Dir.exist?("#{config.path}/routines")
File.delete("#{config.path}/config_tmp.status") if File.exist?("#{config.path}/config_tmp.status")

config.masters = MASTER_USERS if config.masters.to_s=='' and defined?(MASTER_USERS)
config.master_channel = MASTER_CHANNEL if config.master_channel.to_s=='' and defined?(MASTER_CHANNEL)
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/smart-bot/comm/event_hello.rb
Expand Up @@ -16,7 +16,7 @@ def event_hello()
version_message = ". There is a new available version: #{version_remote}."
end
if (!config[:silent] or ENV['BOT_SILENT'].to_s == 'false') and !config.simulate
ENV['BOT_SILENT'] = 'true' if config[:silent] == 'true' and ENV['BOT_SILENT'].to_s != 'true'
ENV['BOT_SILENT'] = 'true' if config[:silent] and ENV['BOT_SILENT'].to_s != 'true'
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."
end
@routines.each do |ch, rout|
Expand Down
35 changes: 28 additions & 7 deletions lib/slack/smart-bot/comm/respond.rb
Expand Up @@ -6,18 +6,27 @@ def respond(msg, dest = nil)
dest = @channels_id[dest] if @channels_id.key?(dest) #it is a name of channel
if !config.simulate #https://api.slack.com/docs/rate-limits
msg.to_s.size > 500 ? wait = 0.5 : wait = 0.1
sleep wait if Time.now <= (@last_respond+wait)
sleep wait if Time.now <= (@last_respond+wait)
else
wait = 0
end
msgs = msg.chars.each_slice(4000).map(&:join) # max of 4000 characters per message
if dest.nil?
if config[:simulate]
open("#{config.path}/buffer_complete.log", "a") { |f|
f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
}
else
if Thread.current[:on_thread]
client.message(channel: @channel_id, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts])
msgs.each do |msg|
client.message(channel: @channel_id, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts])
sleep wait
end
else
client.message(channel: @channel_id, text: msg, as_user: true)
msgs.each do |msg|
client.message(channel: @channel_id, text: msg, as_user: true)
sleep wait
end
end
end
if config[:testing] and config.on_master_bot
Expand All @@ -32,9 +41,15 @@ def respond(msg, dest = nil)
}
else
if Thread.current[:on_thread]
client.message(channel: dest, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts])
msgs.each do |msg|
client.message(channel: dest, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts])
sleep wait
end
else
client.message(channel: dest, text: msg, as_user: true)
msgs.each do |msg|
client.message(channel: dest, text: msg, as_user: true)
sleep wait
end
end
end
if config[:testing] and config.on_master_bot
Expand All @@ -43,11 +58,17 @@ def respond(msg, dest = nil)
}
end
elsif dest[0] == "D" or dest[0] == "U" or dest[0] == "W" # Direct message
send_msg_user(dest, msg)
msgs.each do |msg|
send_msg_user(dest, msg)
sleep wait
end
elsif dest[0] == "@"
begin
user_info = get_user_info(dest)
send_msg_user(user_info.user.id, msg)
msgs.each do |msg|
send_msg_user(user_info.user.id, msg)
sleep wait
end
rescue Exception => stack
@logger.warn("user #{dest} not found.")
@logger.warn stack
Expand Down
9 changes: 5 additions & 4 deletions lib/slack/smart-bot/commands/general/use_rules.rb
Expand Up @@ -16,12 +16,13 @@ def use_rules(dest, channel, user, dchannel)
else
#todo: add pagination for case more than 1000 channels on the workspace
channels = get_channels()

channel.gsub!('#','') # for the case the channel name is in plain text including #
channel_found = channels.detect { |c| c.name == channel }
members = get_channel_members(@channels_id[channel]) unless channel_found.nil?
get_channels_name_and_id() unless @channels_id.key?(channel)
members = get_channel_members(@channels_id[channel]) unless channel_found.nil? or !@channels_id.key?(channel)

if channel_found.nil?
respond "The channel you are trying to use doesn't exist", dest
if channel_found.nil? or !@channels_id.key?(channel)
respond "The channel you are trying to use doesn't exist or cannot be found.", dest
elsif channel_found.name == config.master_channel
respond "You cannot use the rules from Master Channel on any other channel.", dest
elsif !@bots_created.key?(@channels_id[channel])
Expand Down
13 changes: 11 additions & 2 deletions lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb
Expand Up @@ -99,12 +99,21 @@ def add_routine(dest, from, user, name, type, number_time, period, command_to_ru
http.get(files[0].url_private_download, save_data: file_path)
system("chmod +x #{file_path}")
end
channel = dest if channel.to_s == ''
get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
channel_id = nil
if @channels_name.key?(channel) #it is an id
channel_id = channel
channel = @channels_name[channel_id]
elsif @channels_id.key?(channel) #it is a channel name
channel_id = @channels_id[channel]
end

channel_id = dest if channel_id.to_s == ''
@routines[@channel_id] = {} unless @routines.key?(@channel_id)
@routines[@channel_id][name] = { channel_name: config.channel, creator: from, creator_id: user.id, status: :on,
every: every, every_in_seconds: every_in_seconds, at: at, dayweek: dayweek, file_path: file_path,
command: command_to_run.to_s.strip, silent: silent,
next_run: next_run.to_s, dest: channel, last_run: "", last_elapsed: "",
next_run: next_run.to_s, dest: channel_id, last_run: "", last_elapsed: "",
running: false }
update_routines
respond "Added routine *`#{name}`* to the channel", dest
Expand Down
7 changes: 5 additions & 2 deletions lib/slack/smart-bot/process.rb
Expand Up @@ -27,7 +27,7 @@ def process(user, command, dest, dchannel, rules_file, typem, files, ts)
command = command2
on_demand = true
end
if (on_demand or
if (on_demand or typem == :on_dm or
(@listening.key?(from) and (@listening[from].key?(dest) or @listening[from].key?(Thread.current[:thread_ts])) )) and
config.on_maintenance and !command.match?(/\A(set|turn)\s+maintenance\s+off\s*\z/)
respond config.on_maintenance_message, dest
Expand Down Expand Up @@ -83,8 +83,11 @@ def process(user, command, dest, dchannel, rules_file, typem, files, ts)
when /^\s*kill\s+bot\s+on\s+<#C\w+\|(.+)>\s*$/i, /^kill\s+bot\s+on\s+(.+)\s*$/i
channel = $1
kill_bot_on_channel(dest, from, channel)
when /^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+(every)\s+(\d+)\s*(days|hours|minutes|seconds|mins|min|secs|sec|d|h|m|s)\s*(\s<#(C\w+)\|.+>\s*)?(\s.+)?\s*$/i,
when /^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+(every)\s+(\d+)\s*(days|hours|minutes|seconds|mins|min|secs|sec|d|h|m|s)\s*(\s#(\w+)\s*)(\s.+)?\s*$/i,
/^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+(every)\s+(\d+)\s*(days|hours|minutes|seconds|mins|min|secs|sec|d|h|m|s)\s*(\s<#(C\w+)\|.+>\s*)?(\s.+)?\s*$/i,
/^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+on\s+(monday|tuesday|wednesday|thursday|friday|saturday|sunday)s?\s+at\s+(\d+:\d+:?\d+?)\s*()(\s#(\w+)\s*)(\s.+)?\s*$/i,
/^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+on\s+(monday|tuesday|wednesday|thursday|friday|saturday|sunday)s?\s+at\s+(\d+:\d+:?\d+?)\s*()(\s<#(C\w+)\|.+>\s*)?(\s.+)?\s*$/i,
/^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+(at)\s+(\d+:\d+:?\d+?)\s*()(\s#(\w+)\s*)(\s.+)?\s*$/i,
/^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+(at)\s+(\d+:\d+:?\d+?)\s*()(\s<#(C\w+)\|.+>\s*)?(\s.+)?\s*$/i
silent = $2.to_s!=''
name = $3.downcase
Expand Down
18 changes: 15 additions & 3 deletions lib/slack/smart-bot/treat_message.rb
Expand Up @@ -69,14 +69,24 @@ def treat_message(data, remove_blocks = true)
end
typem = :dont_treat
if !dest.nil? and !data.text.nil? and !data.text.to_s.match?(/\A\s*\z/)
#if data.text.match(/^\s*<@#{config[:nick_id]}>\s+(on\s+)?<#(\w+)\|([^>]+)>\s*:?\s*(.*)/im)
if data.text.match(/^\s*<@#{config[:nick_id]}>\s+(on\s+)?((<#\w+\|[^>]+>\s*)+)\s*:?\s*(.*)/im)
#todo: we need to add mixed channels: @smart-bot on private1 #bot1cm <#CXDDFRDDF|bot2cu>: echo A
if data.text.match(/^\s*<@#{config[:nick_id]}>\s+(on\s+)?((<#\w+\|[^>]+>\s*)+)\s*:?\s*(.*)/im) or
data.text.match(/^\s*<@#{config[:nick_id]}>\s+(on\s+)?((#[a-zA-Z0-9]+\s*)+)\s*:?\s*(.*)/im) or
data.text.match(/^\s*<@#{config[:nick_id]}>\s+(on\s+)?(([a-zA-Z0-9]+\s*)+)\s*:\s*(.*)/im)
channels_rules = $2 #multiple channels @smart-bot on #channel1 #channel2 echo AAA
data_text = $4
channel_rules_name = ''
channel_rules = ''
channels_arr = channels_rules.scan(/<#(\w+)\|([^>]+)>/)
if channels_arr.size == 0
channels_arr = []
channels_rules.scan(/([^\s]+)/).each do |cn|
cna = cn.join.gsub('#','')
channels_arr << [@channels_id[cna], cna]
end
end
# to be treated only on the bots of the requested channels
channels_rules.scan(/<#(\w+)\|([^>]+)>/).each do |tcid, tcname|
channels_arr.each do |tcid, tcname|
if @channel_id == tcid
data.text = data_text
typem = :on_call
Expand Down Expand Up @@ -234,6 +244,7 @@ def treat_message(data, remove_blocks = true)
file_cts = eval(file_cts)
if file_cts.is_a?(Hash) and file_cts.key?(:on_maintenance)
config.on_maintenance = file_cts.on_maintenance
config.on_maintenance_message = file_cts.on_maintenance_message
end
end
end
Expand All @@ -245,6 +256,7 @@ def treat_message(data, remove_blocks = true)
file_cts = eval(file_cts)
if file_cts.is_a?(Hash) and file_cts.key?(:on_maintenance)
config.on_maintenance = file_cts.on_maintenance
config.on_maintenance_message = file_cts.on_maintenance_message
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion slack-smart-bot.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'slack-smart-bot'
s.version = '1.9.0'
s.version = '1.9.2'
s.summary = "Create a Slack bot that is smart and so easy to expand, create new bots on demand, run ruby code on chat, create shortcuts..."
s.description = "Create a Slack bot that is smart and so easy to expand, create new bots on demand, run ruby code on chat, create shortcuts...
The main scope of this gem is to be used internally in the company so teams can create team channels with their own bot to help them on their daily work, almost everything is suitable to be automated!!
Expand Down

This file was deleted.

6 changes: 6 additions & 0 deletions whats_new.txt
@@ -1,3 +1,9 @@
*Version 1.9.2* Released 2021-Apr-27

- Bugfixing and small improvements

------------------------------

*Version 1.9.0* Released 2021-Mar-18

*For General users*
Expand Down

0 comments on commit dd23939

Please sign in to comment.