Top Level Namespace

Defined Under Namespace

Classes: SlackSmartBot

Constant Summary collapse

ADMIN_USERS =

for bg compatibility

MASTER_USERS

Instance Method Summary collapse

Instance Method Details

#general_rules(user, command, processed, dest, files = [], rules_file = "") ⇒ Object

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



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



1
2
3
4
5
6
7
8
# 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) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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_projectObject

link to the project



9
10
11
# File 'lib/slack-smart-bot_rules.rb', line 9

def git_project()
  ""
end

#project_folderObject

path to the project folder for example "#echo ~$USER.chop/projects/the_project"



4
5
6
# File 'lib/slack-smart-bot_rules.rb', line 4

def project_folder()
  "#{Dir.pwd}/"
end

#rules(user, command, processed, dest, files = [], rules_file = "") ⇒ Object

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:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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