Skip to content

Commit

Permalink
Merge pull request #45 from MarioRuiz/v1.11.0
Browse files Browse the repository at this point in the history
V1.11.0
  • Loading branch information
MarioRuiz committed Oct 6, 2021
2 parents 9ac2aec + 767c627 commit 43426ba
Show file tree
Hide file tree
Showing 78 changed files with 945 additions and 124 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ slack-smart-bot can create bots on demand, create shortcuts, run ruby code... ju
* [Share Messages](#share-messages)
* [See Statuses](#see-statuses)
* [Routines](#routines)
* [Limit who has access to a command](#limit-who-has-access-to-a-command)
* [Control who has access to a command](#control-who-has-access-to-a-command)
* [See favorite commands](#see-favorite-commands)
* [Tips](#tips)
+ [Send a file](#send-a-file)
Expand Down Expand Up @@ -309,6 +309,10 @@ If you are a Master Admin on a Direct Message with the Smart Bot you can call th

You can also get the bot logs of the bot channel you are using by calling `get bot logs`. You need to be a Master Admin user on a DM with the Smart Bot.

You can add, remove and list admins of any channel by using: `add admin @user`, `remove admin @user` and `see admins`. You need to be the creator of the channel, a Master admin or an admin.

To see the full list of available command ids on any channel call: `see command ids`

#### Cloud Bots
If you want to create a bot that will be running on a different machine: **_`create cloud bot on CHANNEL`_**. Even though the cloud bots are running on different machines, the management can be done through the MASTER CHANNEL. The new cloud bot will be managed by your Master Bot like the others, closing, pausing...

Expand Down Expand Up @@ -495,7 +499,11 @@ Other routine commands:
* **_`see routines`_**
* **_`see result routine NAME`_**

### Limit who has access to a command
### Control who has access to a command

You can add, remove and list admins of any channel by using: `add admin @user`, `remove admin @user` and `see admins`. You need to be the creator of the channel, a Master admin or an admin.

To see the full list of available command ids on any channel call: `see command ids`

If you want to define who has access to certain commands you can specify it on the settings when starting the Smart Bot:

Expand Down
7 changes: 5 additions & 2 deletions lib/slack-smart-bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def initialize(config)
default_rules = (__FILE__).gsub(/\.rb$/, "_rules.rb")
FileUtils.copy_file(default_rules, config.path + '/' + config.rules_file)
end
config.admins = config.masters unless config.admins.to_s!=''
config.admins = config.masters.dup unless config.admins.to_s!=''
config.channel = config.master_channel unless config.channel.to_s!=''
config.status_init = :on unless config.status_init.to_s!=''
else
Expand Down Expand Up @@ -179,7 +179,6 @@ def initialize(config)
@shares = Hash.new()
@last_status_change = Time.now


if File.exist?("#{config.path}/shortcuts/#{config.shortcuts_file}")
file_sc = IO.readlines("#{config.path}/shortcuts/#{config.shortcuts_file}").join
unless file_sc.to_s() == ""
Expand Down Expand Up @@ -270,13 +269,17 @@ def initialize(config)
@datetime_general_commands = 0
@channels_id = Hash.new()
@channels_name = Hash.new()
@channels_creator = Hash.new()
get_channels_name_and_id()
@channel_id = @channels_id[config.channel].dup
@master_bot_id = @channels_id[config.master_channel].dup

Dir.mkdir("#{config.path}/rules/#{@channel_id}") unless Dir.exist?("#{config.path}/rules/#{@channel_id}/")

get_routines()
get_repls()
get_shares()
get_admins_channels()

if @routines.key?(@channel_id)
@routines[@channel_id].each do |k, v|
Expand Down
2 changes: 1 addition & 1 deletion lib/slack-smart-bot_general_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def general_commands(user, command, dest, files = [])


# this is a hidden command that it is not listed when calling bot help
when /\A\s*(that's\s+)?(thanks|thank\s+you|I\s+love\s+you|nice|cool)\s+(#{@salutations.join("|")})\s*!*\s*$/i
when /\s*(that's\s+)?(thanks|thank\s+you|I\s+love\s+you|nice|cool)\s+(#{@salutations.join("|")})\s*!*\s*$/i
save_stats :thanks
reactions = [:heart, :heart_eyes, :blush, :relaxed, :simple_smile, :smiley, :two_hearts, :heartbeat, :green_heart ]
reactions.sample(rand(3)+1).each {|rt| react rt }
Expand Down
1 change: 1 addition & 0 deletions lib/slack/smart-bot/comm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
require_relative 'comm/event_hello'
require_relative 'comm/get_channel_members'
require_relative 'comm/get_channels'
require_relative 'comm/delete'
13 changes: 13 additions & 0 deletions lib/slack/smart-bot/comm/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class SlackSmartBot
def delete(channel, ts)
result = true
begin
resp = client.web_client.chat_delete(channel: channel, as_user: true, ts: ts)
result = resp.ok.to_s == 'true'
rescue Exception => exc
result = false
@logger.fatal exc.inspect
end
return result
end
end
12 changes: 7 additions & 5 deletions lib/slack/smart-bot/comm/respond.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class SlackSmartBot
def respond(msg = "", dest = nil, unfurl_links: true, unfurl_media: true, thread_ts: "", web_client: true, blocks: [], dont_share: false)
def respond(msg = "", dest = nil, unfurl_links: true, unfurl_media: true, thread_ts: "", web_client: true, blocks: [], dont_share: false, return_message: false)
result = true
resp = nil
if (msg.to_s != "" or !msg.to_s.match?(/^A\s*\z/) or !blocks.empty?) and Thread.current[:routine_type].to_s != "bgroutine"
if !web_client.is_a?(TrueClass) and !web_client.is_a?(FalseClass)
(!unfurl_links or !unfurl_media) ? web_client = true : web_client = false
Expand Down Expand Up @@ -119,14 +120,14 @@ def respond(msg = "", dest = nil, unfurl_links: true, unfurl_media: true, thread
end
elsif dest[0] == "D" or dest[0] == "U" or dest[0] == "W" # Direct message
msgs.each do |msg|
send_msg_user(dest, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = send_msg_user(dest, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
sleep wait
end
elsif dest[0] == "@"
begin
user_info = @users.select { |u| u.id == dest[1..-1] or u.name == dest[1..-1] or (u.key?(:enterprise_user) and u.enterprise_user.id == dest[1..-1]) }[-1]
msgs.each do |msg|
send_msg_user(user_info.id, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = send_msg_user(user_info.id, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
sleep wait
end
rescue Exception => stack
Expand Down Expand Up @@ -193,14 +194,14 @@ def respond(msg = "", dest = nil, unfurl_links: true, unfurl_media: true, thread
end
elsif dest[0] == "D" or dest[0] == "U" or dest[0] == "W" # Direct message
blocks.each_slice(40).to_a.each do |blockstmp|
send_msg_user(dest, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media, blocks: blockstmp)
resp = send_msg_user(dest, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media, blocks: blockstmp)
sleep wait
end
elsif dest[0] == "@"
begin
user_info = @users.select { |u| u.id == dest[1..-1] or (u.key?(:enterprise_user) and u.enterprise_user.id == dest[1..-1]) }[-1]
blocks.each_slice(40).to_a.each do |blockstmp|
send_msg_user(user_info.id, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media, blocks: blockstmp)
resp = send_msg_user(user_info.id, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media, blocks: blockstmp)
sleep wait
end
rescue Exception => stack
Expand All @@ -225,6 +226,7 @@ def respond(msg = "", dest = nil, unfurl_links: true, unfurl_media: true, thread
if Thread.current.key?(:routine) and Thread.current[:routine]
File.write("#{config.path}/routines/#{@channel_id}/#{Thread.current[:routine_name]}_output.txt", msg, mode: "a+")
end
result = resp if return_message
return result
end
end
23 changes: 12 additions & 11 deletions lib/slack/smart-bot/comm/send_msg_user.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class SlackSmartBot

#to send messages without listening for a response to users
def send_msg_user(id_user, msg='', on_thread=nil, unfurl_links: true, unfurl_media: true, blocks: [])
def send_msg_user(id_user, msg='', on_thread=nil, unfurl_links: true, unfurl_media: true, blocks: [], web_client: true)
resp = nil
unless msg == "" and blocks.empty?
begin
on_thread = Thread.current[:on_thread] if on_thread.nil?
(!unfurl_links or !unfurl_media) ? web_client = true : web_client = false
web_client = true if !blocks.empty?
web_client = true if !blocks.empty? or !unfurl_links or !unfurl_media
if id_user[0] == "D"
if config[:simulate]
open("#{config.path}/buffer_complete.log", "a") { |f|
Expand All @@ -15,15 +15,15 @@ def send_msg_user(id_user, msg='', on_thread=nil, unfurl_links: true, unfurl_med
else
if web_client
if on_thread
client.web_client.chat_postMessage(channel: id_user, text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: Thread.current[:thread_ts])
resp = client.web_client.chat_postMessage(channel: id_user, text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: Thread.current[:thread_ts])
else
client.web_client.chat_postMessage(channel: id_user, text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = client.web_client.chat_postMessage(channel: id_user, text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
end
else
if on_thread
client.message(channel: id_user, as_user: true, text: msg, thread_ts: Thread.current[:thread_ts], unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = client.message(channel: id_user, as_user: true, text: msg, thread_ts: Thread.current[:thread_ts], unfurl_links: unfurl_links, unfurl_media: unfurl_media)
else
client.message(channel: id_user, as_user: true, text: msg, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = client.message(channel: id_user, as_user: true, text: msg, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
end
end
end
Expand All @@ -42,15 +42,15 @@ def send_msg_user(id_user, msg='', on_thread=nil, unfurl_links: true, unfurl_med
im = client.web_client.conversations_open(users: id_user)
if web_client
if on_thread
client.web_client.chat_postMessage(channel: im["channel"]["id"], text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: Thread.current[:thread_ts])
resp = client.web_client.chat_postMessage(channel: im["channel"]["id"], text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: Thread.current[:thread_ts])
else
client.web_client.chat_postMessage(channel: im["channel"]["id"], text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = client.web_client.chat_postMessage(channel: im["channel"]["id"], text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
end
else
if on_thread
client.message(channel: im["channel"]["id"], as_user: true, text: msg, thread_ts: Thread.current[:thread_ts], unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = client.message(channel: im["channel"]["id"], as_user: true, text: msg, thread_ts: Thread.current[:thread_ts], unfurl_links: unfurl_links, unfurl_media: unfurl_media)
else
client.message(channel: im["channel"]["id"], as_user: true, text: msg, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
resp = client.message(channel: im["channel"]["id"], as_user: true, text: msg, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
end
end
end
Expand All @@ -65,6 +65,7 @@ def send_msg_user(id_user, msg='', on_thread=nil, unfurl_links: true, unfurl_med
@logger.warn stack
end
end
return resp
end

end
19 changes: 12 additions & 7 deletions lib/slack/smart-bot/commands.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require_relative "commands/general/whats_new"
require_relative "commands/general/hi_bot"
require_relative "commands/general/bye_bot"
require_relative "commands/general/bot_help"
require_relative "commands/general/suggest_command"
require_relative "commands/on_bot/general/suggest_command"
require_relative "commands/on_bot/ruby_code"
require_relative "commands/on_bot/repl"
require_relative "commands/on_bot/get_repl"
require_relative "commands/on_bot/run_repl"
require_relative "commands/on_bot/delete_repl"
require_relative "commands/on_bot/see_repls"
require_relative "commands/general/use_rules"
require_relative "commands/general/stop_using_rules"
require_relative "commands/on_bot/general/whats_new"
require_relative "commands/on_bot/general/use_rules"
require_relative "commands/on_bot/general/stop_using_rules"
require_relative "commands/on_master/admin_master/exit_bot"
require_relative "commands/on_master/admin_master/notify_message"
require_relative "commands/on_master/admin/kill_bot_on_channel"
Expand All @@ -26,16 +26,17 @@
require_relative "commands/on_bot/admin/see_routines"
require_relative "commands/on_bot/admin/extend_rules"
require_relative "commands/on_bot/admin/stop_using_rules_on"
require_relative "commands/general/bot_status"
require_relative "commands/on_bot/general/bot_status"
require_relative "commands/on_bot/add_shortcut"
require_relative "commands/on_bot/delete_shortcut"
require_relative "commands/on_bot/see_shortcuts"
require_relative "commands/on_extended/bot_rules"
require_relative "commands/on_bot/admin_master/get_bot_logs"
require_relative "commands/on_bot/admin_master/send_message"
require_relative "commands/on_bot/admin_master/delete_message"
require_relative "commands/on_bot/admin_master/react_to"
require_relative "commands/general/bot_stats"
require_relative "commands/general/leaderboard"
require_relative "commands/on_bot/general/bot_stats"
require_relative "commands/on_bot/general/leaderboard"
require_relative "commands/general/add_announcement"
require_relative "commands/general/delete_announcement"
require_relative "commands/general/see_announcements"
Expand All @@ -48,3 +49,7 @@
require_relative "commands/general/share_messages"
require_relative "commands/general/see_shares"
require_relative "commands/general/delete_share"
require_relative "commands/general/see_admins"
require_relative "commands/general/add_admin"
require_relative "commands/general/remove_admin"
require_relative "commands/general/see_command_ids"
51 changes: 51 additions & 0 deletions lib/slack/smart-bot/commands/general/add_admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class SlackSmartBot
def add_admin(user, admin_user)
save_stats(__method__)
if Thread.current[:dest][0]=='D'
respond "This command cannot be called from a DM"
else
if Thread.current[:typem] == :on_call
channel = Thread.current[:dchannel]
elsif Thread.current[:using_channel].to_s==''
channel = Thread.current[:dest]
else
channel = Thread.current[:using_channel]
end
messages = []
admins = config.masters.dup
channels = get_channels()
channel_found = channels.detect { |c| c.id == channel }
if !channel_found.nil? and channel_found.creator.to_s != ''
creator_info = @users.select{|u| u.id == channel_found.creator or (u.key?(:enterprise_user) and u.enterprise_user.id == channel_found.creator)}[-1]
admins << creator_info.name
end
if Thread.current[:typem] == :on_bot or Thread.current[:typem] == :on_master
admins << config.admins.dup
end
if @admins_channels.key?(channel) and @admins_channels[channel].size > 0
admins << @admins_channels[channel]
end
admins.flatten!
admins.uniq!
admins.delete(nil)
if admins.include?(user.name)
admin_info = @users.select{|u| u.id == admin_user or (u.key?(:enterprise_user) and u.enterprise_user.id == admin_user)}[-1]
if admins.include?(admin_info.name)
messages << "This user is already an admin of this channel."
else
@admins_channels[channel] ||= []
@admins_channels[channel] << admin_info.name
update_admins_channels()
messages << "The user is an admin of this channel from now on."
admins << admin_info.name
end
messages << "*Admins*: <@#{admins.join('>, <@')}>"
else
messages << "Only the creator of the channel, Master admins or admins can add a new admin for this channel."
messages << "*Admins*: <@#{admins.join('>, <@')}>"
end

respond messages.join("\n")
end
end
end
58 changes: 58 additions & 0 deletions lib/slack/smart-bot/commands/general/remove_admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class SlackSmartBot
def remove_admin(user, admin_user)
save_stats(__method__)
if Thread.current[:dest][0]=='D'
respond "This command cannot be called from a DM"
else

if Thread.current[:typem] == :on_call
channel = Thread.current[:dchannel]
elsif Thread.current[:using_channel].to_s==''
channel = Thread.current[:dest]
else
channel = Thread.current[:using_channel]
end
messages = []
admins = config.masters.dup
channels = get_channels()
channel_found = channels.detect { |c| c.id == channel }
if !channel_found.nil? and channel_found.creator.to_s != ''
creator_info = @users.select{|u| u.id == channel_found.creator or (u.key?(:enterprise_user) and u.enterprise_user.id == channel_found.creator)}[-1]
admins << creator_info.name
end
if Thread.current[:typem] == :on_bot or Thread.current[:typem] == :on_master
admins << config.admins.dup
end
if @admins_channels.key?(channel) and @admins_channels[channel].size > 0
admins << @admins_channels[channel]
end
admins.flatten!
admins.uniq!
admins.delete(nil)
if admins.include?(user.name)
admin_info = @users.select{|u| u.id == admin_user or (u.key?(:enterprise_user) and u.enterprise_user.id == admin_user)}[-1]
if creator_info.name == admin_info.name
messages << "This user created the channel and cannot be removed as an admin."
elsif config.masters.include?(admin_info.name) or config.masters.include?(admin_user)
messages << "Master admins cannot be removed as admins of this channel."
elsif config.admins.include?(admin_info.name) or config.admins.include?(admin_user)
messages << "This user is a defaulted admin for this channel and cannot be removed using this command."
elsif !admins.include?(admin_info.name)
messages << "This user is not an admin of this channel."
else
@admins_channels[channel] ||= []
@admins_channels[channel].delete(admin_info.name)
update_admins_channels()
messages << "The user is not an admin of this channel from now on."
admins.delete(admin_info.name)
end
messages << "*Admins*: <@#{admins.join('>, <@')}>"
else
messages << "Only the creator of the channel, Master admins or admins can remove an admin of this channel."
messages << "*Admins*: <@#{admins.join('>, <@')}>"
end

respond messages.join("\n")
end
end
end

0 comments on commit 43426ba

Please sign in to comment.