Top Level Namespace

Defined Under Namespace

Classes: SlackSmartBot

Constant Summary collapse

CHANNEL =
ARGV[0]
ON_MASTER_BOT =
false
ADMIN_USERS =
ARGV[1].split(",")
RULES_FILE =
ARGV[2]
STATUS_INIT =
ARGV[3].to_sym
SHORTCUTS_FILE =
"slack-smart-bot_shortcuts_#{CHANNEL}.rb".gsub(" ", "_")

Instance Method Summary collapse

Instance Method Details

#ask(question, context, to, dest) ⇒ Object

context: previous message to: user that should answer


26
27
28
29
# File 'lib/slack-smart-bot_rules.rb', line 26

def ask(question, context, to, dest)
  puts "Bot: #{question}"
  @questions[to] = context
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()
  "#{`eval echo ~$USER`.chop}/"
end

#respond(message, dest) ⇒ Object


20
21
22
# File 'lib/slack-smart-bot_rules.rb', line 20

def respond(message, dest)
  puts message
end

#rules(user, command, processed, dest) ⇒ Object

from: Full name of the person sending the message command: command to run processed: in case the command has been already processed on Bot class, by default false 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:


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
# File 'lib/slack-smart-bot_rules.rb', line 45

def rules(user, command, processed, dest)
  from = user.name
  display_name = user.profile.display_name

  if @testing
    puts "#{from}: #{command}"
    if @questions.keys.include?(from)
      context = @questions[from]
      @questions[from] = command
      command = context
    end
  end
  begin
    case command

    # help: ----------------------------------------------
    # help: `echo SOMETHING`
    # help:     repeats SOMETHING
    # help:
    when /^echo\s(.+)/i
      respond $1, dest

      # help: ----------------------------------------------
      # help: `go to sleep`
      # help:   it will sleep the bot for 5 seconds
      # help:
    when /^go\sto\ssleep/i
      unless @questions.keys.include?(from)
        ask("do you want me to take a siesta?", command, from, dest)
      else
        case @questions[from]
        when /yes/i, /yep/i, /sure/i
          @questions.delete(from)
          respond "I'll be sleeping for 5 secs... just for you", dest
          respond "zZzzzzzZZZZZZzzzzzzz!", dest
          sleep 5
        when /no/i, /nope/i, /cancel/i
          @questions.delete(from)
          respond "Thanks, I'm happy to be awake", dest
        else
          respond "I don't understand", dest
          ask("are you sure do you want me to sleep? (yes or no)", "go to sleep", from, dest)
        end
      end

      # help: ----------------------------------------------
      # help: `run something`
      # help:   It will run the process and report the results when done
      # help:
    when /^run something/i
      respond "Running", dest

      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)
      if stderr == ""
        if stdout == ""
          respond "#{display_name}: Nothing returned.", dest
        else
          respond "#{display_name}: #{stdout}", dest
        end
      else
        respond "#{display_name}: #{stderr}", dest
      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
        if @channel_id == dest or dest[0]=="D" or dest[0] == "G" #not on extended channels
          resp = %w{ what huh sorry }.sample
          respond "#{display_name}: #{resp}?", dest
        end
      end
    end

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