Skip to content

Commit

Permalink
close #24
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Oct 16, 2020
1 parent 821708a commit bd1b961
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 28 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -386,7 +386,7 @@ Example:
To see available shortcuts: **_`see shortcuts`_** and to delete a particular shortcut: **_`delete shortcut NAME`_**

### Routines
To add specific commands to be run automatically every certain amount of time or a specific time: **_`add routine NAME every NUMBER PERIOD COMMAND`_** or **_`add routine NAME at TIME COMMAND`_**
To add specific commands to be run automatically every certain amount of time or a specific time: **_`add routine NAME every NUMBER PERIOD COMMAND`_** or **_`add routine NAME at TIME COMMAND`_**. Also just before the command you can supply the channel where you want to publish the results, if not channel supplied then it would be the SmartBot Channel or on the DM if the command is run from there. Remember the SmartBot needs to have access to the channel where you want to publish.

If you want to hide the routine executions use `add silent routine`. It won't show the routine name when executing.

Expand All @@ -395,6 +395,7 @@ Examples:
>**_`add routine clean_db at 17:05 !clean customers temp db`_**
>**_`add silent routine clean_db at 17:05 !clean customers temp db`_**
>**_`add routine clean_custdb on Mondays at 05:00 !clean customers db`_**
>**_`add routine clean_custdb on Tuesdays at 09:00 #SREChannel !clean customers db`_**
Also instead of adding a Command to be executed, you can attach a file, then the routine will be created and the attached file will be executed on the criteria specified. Only Master Admins are allowed to use it this way.

Expand Down
13 changes: 9 additions & 4 deletions lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb
@@ -1,11 +1,14 @@
class SlackSmartBot
# helpadmin: ----------------------------------------------
# helpadmin: `add routine NAME every NUMBER PERIOD COMMAND`
# helpadmin: `add routine NAME every NUMBER PERIOD #CHANNEL COMMAND`
# helpadmin: `add routine NAME every NUMBER PERIOD`
# helpadmin: `add silent routine NAME every NUMBER PERIOD`
# helpadmin: `create routine NAME every NUMBER PERIOD`
# helpadmin: `add routine NAME at TIME COMMAND`
# helpadmin: `add routine NAME at TIME #CHANNEL COMMAND`
# helpadmin: `add routine NAME on DAYWEEK at TIME COMMAND`
# helpadmin: `add routine NAME on DAYWEEK at TIME #CHANNEL COMMAND`
# helpadmin: `add routine NAME at TIME`
# helpadmin: `add silent routine NAME at TIME`
# helpadmin: `create routine NAME at TIME`
Expand All @@ -17,15 +20,17 @@ class SlackSmartBot
# helpadmin: PERIOD: days, d, hours, h, minutes, mins, min, m, seconds, secs, sec, s
# helpadmin: TIME: time at format HH:MM:SS
# helpadmin: DAYWEEK: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. And their plurals.
# helpadmin: #CHANNEL: the destination channel where the results will be published. If not supplied then the bot channel by default or a DM if the command is run from a DM.
# helpadmin: COMMAND: any valid smart bot command or rule
# helpadmin: Examples:
# helpadmin: _add routine example every 30s ruby puts 'a'_
# helpadmin: _add routine example every 3 days ruby puts 'a'_
# helpadmin: _add routine example at 17:05 ruby puts 'a'_
# helpadmin: _create silent routine every 12 hours !Run customer tests_
# helpadmin: _create silent routine Example every 12 hours !Run customer tests_
# helpadmin: _add routine example on Mondays at 05:00 !run customer tests_
# helpadmin: _add routine example on Tuesdays at 09:00 #SREChannel !run db cleanup_
# helpadmin:
def add_routine(dest, from, user, name, type, number_time, period, command_to_run, files, silent)
def add_routine(dest, from, user, name, type, number_time, period, command_to_run, files, silent, channel)
save_stats(__method__)
if files.nil? or files.size == 0 or (files.size > 0 and config.masters.include?(from))
if config.admins.include?(from)
Expand Down Expand Up @@ -94,12 +99,12 @@ 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 == ''
@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: dest, last_run: "", last_elapsed: "",
next_run: next_run.to_s, dest: channel, last_run: "", last_elapsed: "",
running: false }
update_routines
respond "Added routine *`#{name}`* to the channel", dest
Expand Down
11 changes: 6 additions & 5 deletions lib/slack/smart-bot/process.rb
Expand Up @@ -69,16 +69,17 @@ def process(user, command, dest, dchannel, rules_file, typem, files, ts)
when /^\s*kill\sbot\son\s<#C\w+\|(.+)>\s*$/i, /^kill\sbot\son\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.+)?\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.+)?\s*$/i,
/^\s*(add|create)\s+(silent\s+)?routine\s+(\w+)\s+(at)\s+(\d+:\d+:?\d+?)\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<#(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<#(C\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
type = $4
number_time = $5
period = $6
command_to_run = $7
add_routine(dest, from, user, name, type, number_time, period, command_to_run, files, silent)
channel = $8
command_to_run = $9
add_routine(dest, from, user, name, type, number_time, period, command_to_run, files, silent, channel)
when /^\s*(kill|delete|remove)\s+routine\s+(\w+)\s*$/i
name = $2.downcase
remove_routine(dest, from, name)
Expand Down
30 changes: 16 additions & 14 deletions lib/slack/smart-bot/treat_message.rb
Expand Up @@ -42,7 +42,9 @@ def treat_message(data, remove_blocks = true)
f.puts "|#{data.channel}|#{data.user}|#{data.text}"
}
end
if data.channel[0] == "D" or data.channel[0] == "C" or data.channel[0] == "G" #Direct message or Channel or Private Channel
if data.key?(:dest) and data.dest.to_s!='' # for run routines and publish on different channels
dest = data.dest
elsif data.channel[0] == "D" or data.channel[0] == "C" or data.channel[0] == "G" #Direct message or Channel or Private Channel
dest = data.channel
else # not treated
dest = nil
Expand All @@ -65,15 +67,15 @@ def treat_message(data, remove_blocks = true)
data.text = $4
typem = :on_call
end
elsif dest == @master_bot_id
elsif data.channel == @master_bot_id
if config.on_master_bot #only to be treated on master bot channel
typem = :on_master
end
elsif @bots_created.key?(dest)
if @channel_id == dest #only to be treated by the bot on the channel
elsif @bots_created.key?(data.channel)
if @channel_id == data.channel #only to be treated by the bot on the channel
typem = :on_bot
end
elsif dest[0] == "D" #Direct message
elsif data.channel[0] == "D" #Direct message
get_rules_imported()
if @rules_imported.key?(data.user) && @rules_imported[data.user].key?(data.user) and
@bots_created.key?(@rules_imported[data.user][data.user])
Expand All @@ -85,21 +87,21 @@ def treat_message(data, remove_blocks = true)
#only to be treated by master bot
typem = :on_dm
end
elsif dest[0] == "C" or dest[0] == "G"
elsif data.channel[0] == "C" or data.channel[0] == "G"
#only to be treated on the channel of the bot. excluding running ruby
if !config.on_master_bot and @bots_created.key?(@channel_id) and @bots_created[@channel_id][:extended].include?(@channels_name[dest]) and
if !config.on_master_bot and @bots_created.key?(@channel_id) and @bots_created[@channel_id][:extended].include?(@channels_name[data.channel]) and
!data.text.match?(/^!?\s*(ruby|code)\s+/) and !data.text.match?(/^!?!?\s*(ruby|code)\s+/) and !data.text.match?(/^\^?\s*(ruby|code)\s+/)
typem = :on_extended
elsif config.on_master_bot and (data.text.match?(/^!?\s*(ruby|code)\s+/) or data.text.match?(/^!?!?\s*(ruby|code)\s+/) or data.text.match?(/^\^?\s*(ruby|code)\s+/) )
#or in case of running ruby, the master bot
@bots_created.each do |k, v|
if v.key?(:extended) and v[:extended].include?(@channels_name[dest])
if v.key?(:extended) and v[:extended].include?(@channels_name[data.channel])
typem = :on_extended
break
end
end
end
if dest[0] == "G" and config.on_master_bot and typem != :on_extended #private group
if data.channel[0] == "G" and config.on_master_bot and typem != :on_extended #private group
typem = :on_pg
end
end
Expand Down Expand Up @@ -127,7 +129,7 @@ def treat_message(data, remove_blocks = true)
command = @questions[user_info.user.name]
@questions[user_info.user.name] = data.text
end
elsif @repl_sessions.key?(user_info.user.name) and dest==@repl_sessions[user_info.user.name][:dest] and
elsif @repl_sessions.key?(user_info.user.name) and data.channel==@repl_sessions[user_info.user.name][:dest] and
((@repl_sessions[user_info.user.name][:on_thread] and data.thread_ts == @repl_sessions[user_info.user.name][:thread_ts]) or
(!@repl_sessions[user_info.user.name][:on_thread] and data.thread_ts.to_s == '' ))

Expand Down Expand Up @@ -172,13 +174,13 @@ def treat_message(data, remove_blocks = true)
if channel_found.nil?
@logger.fatal "Not possible to find the channel #{channel_rules_name}"
elsif channel_found.name == config.master_channel
respond "You cannot use the rules from Master Channel on any other channel.", dest
respond "You cannot use the rules from Master Channel on any other channel.", data.channel
elsif @status != :on
respond "The bot in that channel is not :on", dest
respond "The bot in that channel is not :on", data.channel
elsif data.user == channel_found.creator or members.include?(data.user)
process_first(user_info.user, command, dest, channel_rules, typem, data.files, data.ts, data.thread_ts)
else
respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", data.channel
end
elsif config.on_master_bot and typem == :on_extended and
command.size > 0 and command[0] != "-"
Expand All @@ -197,7 +199,7 @@ def treat_message(data, remove_blocks = true)
@logger.fatal stack
end
else
if !config.on_master_bot and !dest.nil? and (dest == @master_bot_id or dest[0] == "D") and
if !config.on_master_bot and !dest.nil? and (data.channel == @master_bot_id or dest[0] == "D") and
data.text.match?(/^\s*bot\s+status\s*$/i) and @admin_users_id.include?(data.user)
respond "ping from #{config.channel}", dest
elsif !config.on_master_bot and !dest.nil? and data.user == config[:nick_id] and dest == @master_bot_id
Expand Down
15 changes: 12 additions & 3 deletions lib/slack/smart-bot/utils/create_routine_thread.rb
Expand Up @@ -23,7 +23,11 @@ def create_routine_thread(name)
stdout, stderr, status = Open3.capture3(process_to_run)
if !@routines[@channel_id][name][:silent] or (@routines[@channel_id][name][:silent] and
(!stderr.match?(/\A\s*\z/) or !stdout.match?(/\A\s*\z/)))
respond "routine *`#{name}`*: #{@routines[@channel_id][name][:file_path]}", @routines[@channel_id][name][:dest]
if @routines[@channel_id][name][:dest]!=@channel_id
respond "routine from <##{@channel_id}> *`#{name}`*: #{@routines[@channel_id][name][:file_path]}", @routines[@channel_id][name][:dest]
else
respond "routine *`#{name}`*: #{@routines[@channel_id][name][:file_path]}", @routines[@channel_id][name][:dest]
end
end
if stderr == ""
unless stdout.match?(/\A\s*\z/)
Expand All @@ -34,10 +38,15 @@ def create_routine_thread(name)
end
else #command
if !@routines[@channel_id][name][:silent]
respond "routine *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
if @routines[@channel_id][name][:dest]!=@channel_id
respond "routine from <##{@channel_id}> *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
else
respond "routine *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
end
end
started = Time.now
data = { channel: @routines[@channel_id][name][:dest],
data = { channel: @channel_id,
dest: @routines[@channel_id][name][:dest],
user: @routines[@channel_id][name][:creator_id],
text: @routines[@channel_id][name][:command],
files: nil }
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.8.1'
s.version = '1.8.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
Expand Up @@ -36,6 +36,15 @@
expect(res[1]).to match(/^Sam$/)
end

it "creates the routine every 2 sc and publish on specified channel" do
send_message "add routine example every 2s <##{CBOT2CU}|bot2cu> !ruby puts 'Sam'", from: user, to: channel
expect(buffer(to: channel, from: :ubot).join).to match(/Added routine \*`example`\* to the channel/)
sleep 4
res = buffer(to: :cbot2cu, from: :ubot)
expect(res[0]).to match(/^routine from <##{CBOT1CM}> \*`example`\*: !ruby puts 'Sam'$/)
expect(res[1]).to match(/^Sam$/)
end

it 'creates a silent routine and is displayed only when returns message' do
routine = "!test silent #{Time.now+10}"

Expand Down

0 comments on commit bd1b961

Please sign in to comment.