class Object

Constants

ADMIN_USERS

Public Instance Methods

general_rules(user, command, processed, dest, files = [], rules_file = "") click to toggle source

add here the general rules you will be using in all Smart Bots

# File lib/slack-smart-bot_general_rules.rb, line 2
def general_rules(user, command, processed, dest, files = [], rules_file = "")
    from = user.name
    display_name = user.profile.display_name
  
    begin
      case command

        # help: ----------------------------------------------
        # help: `echo SOMETHING`
        # help: `INTEGER echo SOMETHING`
        # help:     repeats SOMETHING. If INTEGER supplied then that number of times.
        # help:  Examples:
        # help:     _echo I am the Smart Bot_
        # help:     _100 echo :heart:_
      when /^(\d*)\s*echo\s(.+)/i
        save_stats :echo
        $1.to_s == '' ? times = 1 : times = $1.to_i
        respond ($2*times).to_s

      else
        return false
      end
      return true
    rescue => exception
      if defined?(@logger)
        @logger.fatal exception
        respond "Unexpected error!! Please contact an admin to solve it: <@#{ADMIN_USERS.join(">, <@")}>"
      else
        puts exception
      end
    end
end
get_channel_members(channel_id) click to toggle source
# File lib/slack/smart-bot/comm/get_channel_members.rb, line 1
def get_channel_members(channel_id)
    if config.simulate and config.key?(:client)
        client.web_client.conversations_members[channel_id.to_sym].members
    else
        client.web_client.conversations_members(channel: channel_id).members
    end

end
get_channels(bot_is_in: false) click to toggle source
# File lib/slack/smart-bot/comm/get_channels.rb, line 1
def get_channels(bot_is_in: false)
    if config.simulate and config.key?(:client)
        if bot_is_in
            client.web_client.conversations_members.reject {|r,v| !v.members.include?(config.nick_id)}.values
        else
           client.web_client.conversations_members.values
        end
    else
        if bot_is_in
            client.web_client.users_conversations(exclude_archived: true, limit: 100, types: "im, public_channel,private_channel").channels
        else
            #todo: add pagination for case more than 1000 channels on the workspace
            client.web_client.conversations_list(
            types: "private_channel,public_channel",
            limit: "1000",
            exclude_archived: "true",
            ).channels        
        end
    end
end
git_project() click to toggle source

link to the project

# File lib/slack-smart-bot_rules.rb, line 9
def git_project()
  ""
end
project_folder() click to toggle source

path to the project folder

for example "#{`eval echo ~$USER`.chop}/projects/the_project"
# File lib/slack-smart-bot_rules.rb, line 4
def project_folder()
  "#{Dir.pwd}/"
end
rules(user, command, processed, dest, files = [], rules_file = "") click to toggle source

user: user slack object command: command to run processed: in case the command has been already processed on Bot class, by default false dest: channel_id files: files attached rules_file: rules_file name

About the Help:

Add as a comment starting by "help:" the help you need to add to the `bot help` and `bot rules` commands.
The command logic needs to be added with ``, and the parameters to supply need to be in capital for example: `echo SOMETHING`

help: =================================== help: help: *These are specific commands for this bot on this Channel.* help: They will be accessible only when the bot is listening to you just writing the command help: or the bot is not listening to you but requested on demand, or in a private conversation with the Smart Bot. help: To run a command on demand: help: `!THE_COMMAND` help: `@NAME_OF_BOT THE_COMMAND` help: `NAME_OF_BOT THE_COMMAND` help: To run a command on demand and add the respond on a thread: help: `^THE_COMMAND` help: `!!THE_COMMAND` help:

# File lib/slack-smart-bot_rules.rb, line 37
def rules(user, command, processed, dest, files = [], rules_file = "")
  from = user.name
  display_name = user.profile.display_name

  load "#{config.path}/rules/general_rules.rb"
  
  unless general_rules(user, command, processed, dest, files, rules_file)
    begin
      case command

        # help: ----------------------------------------------
        # help: `go to sleep`
        # help:   it will sleep the bot for 5 seconds
        # help:
      when /^go\sto\ssleep/i
        save_stats :go_to_sleep
        if answer.empty?
          ask "do you want me to take a siesta?"
        else
          case answer
          when /yes/i, /yep/i, /sure/i
            answer_delete
            respond "I'll be sleeping for 5 secs... just for you"
            respond "zZzzzzzZZZZZZzzzzzzz!"
            react :sleeping
            sleep 5
            unreact :sleeping
            react :sunny
          when /no/i, /nope/i, /cancel/i
            answer_delete
            respond "Thanks, I'm happy to be awake"
          else
            respond "I don't understand"
            ask "are you sure you want me to sleep? (yes or no)"
          end
        end

        # help: ----------------------------------------------
        # help: `run something`
        # help:   It will run the process and report the results when done
        # help:
      when /^run something/i
        save_stats :run_something
        react :runner

        process_to_run = "ruby -v"
        process_to_run = ("cd #{project_folder} &&" + process_to_run) if defined?(project_folder)
        stdout, stderr, status = Open3.capture3(process_to_run)
        unreact :runner
        if stderr == ""
          if stdout == ""
            respond "#{display_name}: Nothing returned."
          else
            respond "#{display_name}: #{stdout}"
          end
        else
          respond "#{display_name}: #{stdout} #{stderr}"
        end
        
        # Emoticons you can use with `react` command https://www.webfx.com/tools/emoji-cheat-sheet/
        
        # Examples for respond and respond_direct
        #   # send 'the message' to the channel or direct message where the command was written
        #   respond "the message"
        #   # send 'the message' privately as a direct message to the user that sent the command
        #   respond_direct "the message"
        #   # send 'the message' to a specific channel name
        #   respond "the message", 'my_channel'
        #   # send 'the message' to a specific channel id
        #   respond "the message", 'CSU34D33'
        #   # send 'the message' to a specific user as direct message
        #   respond "the message", '@theuser'
        #   # send 'the message' to a specific user id as direct message
        #   respond "the message", 'US3344D3'

        # Example downloading a file from slack
        #  if !files.nil? and files.size == 1 and files[0].filetype == 'yaml'
        #    require 'nice_http'
        #    http = NiceHttp.new(host: "https://files.slack.com", headers: { "Authorization" => "Bearer #{config[:token]}" })
        #    http.get(files[0].url_private_download, save_data: './tmp/')
        #  end

        # Examples sending a file to slack:
        #   send_file(to, msg, filepath, title, format, type = "text")
        #   send_file(dest, 'the message', "#{project_folder}/temp/logs_ptBI.log", 'title', 'text/plain', "text")
        #   send_file(dest, 'the message', "#{project_folder}/temp/example.jpeg", 'title', 'image/jpeg', "jpg")


      else
        unless processed
          dont_understand()
        end
      end
    rescue => exception
      @logger.fatal exception
      respond "Unexpected error!! Please contact an admin to solve it: <@#{config.admins.join(">, <@")}>"
    end
  end
end