-
Notifications
You must be signed in to change notification settings - Fork 3
/
slack-smart-bot_rules_GNCU7JC6L_smartbotuser1.rb
153 lines (136 loc) · 5.03 KB
/
slack-smart-bot_rules_GNCU7JC6L_smartbotuser1.rb
1
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
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
#path to the project folder
# for example "#{`eval echo ~$USER`.chop}/projects/the_project"
def project_folder()
"#{Dir.pwd}/"
end
#link to the project
def git_project()
""
end
#for the case of testing, just run this file adding in the end a call to rules with the parameters you want
if defined?(respond)
@testing = false
else
@testing = true
@questions = Hash.new()
def respond(message, dest)
puts message
end
#context: previous message
#to: user that should answer
def ask(question, context, to, dest)
puts "Bot: #{question}"
@questions[to] = context
end
end
# 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:
def rules(user, command, processed, dest, files = [], rules_file = "")
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
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
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 you want me to sleep? (yes or no)", command, 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}: #{stdout} #{stderr}", dest
end
# 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(rules_file, command, user, dest)
end
end
rescue => exception
if defined?(@logger)
@logger.fatal exception
respond "Unexpected error!! Please contact an admin to solve it: <@#{config.admins.join(">, <@")}>", dest
else
puts exception
end
end
end
end
#for the case of testing just running this file, write the dialogue in here:
if @testing
require "nice_hash"
user = { name: "Peter Johnson", id: "Uxxxxxx" }
rules user, "go to sleep, you look tired", false, nil
rules user, "yes", false, nil
end