Top Level Namespace

Defined Under Namespace

Classes: SlackSmartBot

Constant Summary collapse

SP_COMPARE_NUMBERS_AS_STRINGS =

nice_hash

false
ADMIN_USERS =

for bg compatibility

MASTER_USERS

Instance Method Summary collapse

Instance Method Details

#general_commands(user, command, dest, files = []) ⇒ Object

add here the general commands you will be using in any channel where The SmartBot is part of. Not necessary to use ! or ^, it will answer directly.



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
34
35
36
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
# File 'lib/slack-smart-bot_general_commands.rb', line 2

def general_commands(user, command, dest, files = [])

  begin
    team_id_user = "#{user.team_id}_#{user.name}"
    case command

        # help: ----------------------------------------------
        # help: `cls`
        # help: `clear`
        # help: `clear screen`
        # help: `NUMBER cls`
        # help:     It will send a big empty message.
        # help:        NUMBER (optional): number of lines. Default 100. Max 200.
        # help: command_id: :cls
        # help:
    when /\A\s*(\d*)\s*(clear|cls|clear\s+screen)\s*\z/i
      save_stats :cls
      $1.to_s == '' ? lines = 100 : lines = $1.to_i
      lines = 200 if lines > 200
      respond (">#{"\n"*lines}<")

        # help: ----------------------------------------------
        # help: `blink TEXT`
        # help: `INTEGER blink TEXT`
        # help:     It will blink the text supplied. One or more lines of text. Emoticons or text format are allowed.
        # help:        INTEGER (optional): number of times. Default 50. Max 200.
        # help:  Examples:
        # help:    blink Hello World!
        # help:    blink :moneybag: Pay attention! *Sales* are published!
        # help:    100 blink :new: *Party is about to start* :i_love_you_hand_sign:
        # help: command_id: :blink
        # help:
      when /\A\s*(\d+)?\s*blink\s+(.+)\s*\z/im
        save_stats :blink
        num_times = $1.to_s == '' ? 50 : $1.to_i
        text = $2
        @blinking ||= []
        if num_times > 200 or num_times < 1
          respond "The number of times must be between 1 and 200"
        elsif @blinking.include?(team_id_user)
          respond "I'm already blinking something for you. Please wait until I finish"
        elsif @blinking.size >= 3 # rate limit in theory update can be done only once per second
          respond "I'm already blinking something for too many people. Please wait until I finish at least one of them."
        else
          @blinking << team_id_user
          msg = respond(text, return_message: true)
          num_times.times do
            sleep 2
            update(dest, msg.ts, ' ')
            sleep 0.5
            update(dest, msg.ts, text)
          end
          @blinking.delete(team_id_user)
        end

      # this is a hidden command that it is not listed when calling bot help
    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 }
      responses = ['Thank YOU', "You're welcome", "You're very welcome", 'No problem', 'No worries', "Don't mention it", 'My pleasure',
        'Anytime', 'It was the least I could do', 'Glad to help', 'Sure', 'Pleasure', 'The pleasure is mine', 'It was nothing', 'Much obliged', "I'm happy to help",
        'Það var ekkert', 'De nada', 'No hay de qué', 'De rien',  'Bitte', 'Prego', 'मेरा सौभाग्य है', '不客氣', 'Παρακαλώ']
      respond "#{responses.sample}#{'!'*rand(4)}"

      # this is a hidden command that it is not listed when calling bot help
    when /\s*.*happy\s+birthday.*(<@\w+>)\s*.*$/i, /\s*.*(<@\w+>).*happy\s+birthday\s*.*$/i
      unless Thread.current[:on_thread]
        save_stats :happy_birthday
        happy_user = $1
        reactions = [:tada, :cake, :birthday]
        sleep 30
        reactions.sample(rand(3)+1).each {|rt| react rt }
        sleep (rand(10)+5)*60 # so SmartBot is not the first one
        responses = ['Happy birthday', "Very happy birthday", "Happy happy happy birthday", "Have a fabulous birthday", 'May all your wishes come true',
          'Many happy returns of the day', 'I wish you a wonderful birthday', 'Have a great one', 'I hope you have a fantastic day and a fantastic year to come',
          'To your happiness', "Don't count the candles. Enjoy the party", 'May your day be as awesome as you are', 'The best things in life are yet to come']
        respond "#{happy_user} #{responses.sample}#{'!'*rand(4)}", :on_thread
      end

    else
      return false
    end
    return true
  rescue => exception
    if defined?(@logger)
      @logger.fatal exception
      respond "Unexpected error!! Please contact an admin to solve it: <@#{config.admins.join(">, <@")}>"
    else
      puts exception
    end
    return false
  end
end

#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
34
35
36
# 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: `NUMBER echo SOMETHING`
        # help:     repeats SOMETHING. If NUMBER supplied then that number of times.
        # help:  Examples:
        # help:     _echo I am the Smart Bot_
        # help:     _100 echo :heart:_
        # help: command_id: :echo
        # help:        
      when /\A\s*(\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: <@#{config.admins.join(">, <@")}>"
      else
        puts exception
      end
      return false
    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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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"
  
  if general_rules(user, command, processed, dest, files, rules_file)
    return true
  else
    begin
      case command

        # help: ----------------------------------------------
        # help: `go to sleep`
        # help:   it will sleep the bot for 5 seconds
        # help: command_id: :go_to_sleep
        # help:
      when /\A\s*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: command_id: :run_something
        # help:
      when /\Arun something/i
        save_stats :run_something
        if has_access?(:run_something, user)
          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
        end
        
        # Emoticons you can use with `react` command https://www.webfx.com/tools/emoji-cheat-sheet/
        
        # Examples for respond, respond_thread 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"
        #   # same thing can be done:
        #   respond "the message", :direct
        #   # send 'the message' opening a thread
        #   respond_thread "the message"
        #   # same thing can be done:
        #   respond 'the message', :on_thread
        #   # 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 sending blocks https://api.slack.com/block-kit
        # my_blocks = [
        #   { type: "context",
        #     elements:
        #       [
        #         { type: "plain_text", :text=>"\tInfo: " },
        #         { type: "image", image_url: "https://avatars.slack-edge.com/2021-03-23/182815_e54abb1dd_24.jpg", alt_text: "mario" },
        #         { type: "mrkdwn", text: " *Mario Ruiz* (marior)  " }
        #       ]
        #   }
        # ]
        # respond blocks: my_blocks

        # 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
        return false
      end
      return true
    rescue => exception
      @logger.fatal exception
      respond "Unexpected error!! Please contact an admin to solve it: <@#{config.admins.join(">, <@")}>"
      return false
    end
  end
end