-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwiki_bot.js
219 lines (162 loc) · 7.22 KB
/
wiki_bot.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Slack bot built with Botkit.
-> http://howdy.ai/botkit
-> https://github.com/erwanp/slack-wiki
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if (!process.env.token) {
console.log('Error: Specify token in environment');
process.exit(1);
}
if (!process.env.local_wiki) {
console.log('Error: Specify local wiki path');
process.exit(1);
}
const local_wiki_path = process.env.local_wiki
var Botkit = require('botkit');
var os = require('os');
// Interface messages (bilangual, because I want my bot to be a French-speaker
// but not everyone probably does...)
const lang = 'en'
var message_got_it = {
'fr': "Je regarde sur le Wiki",
'en': "I'm looking for that"
};
var message_host_not_found = {
'fr': "Je ne trouve pas le serveur",
'en': "Host not found"
};
var message_connection_failed = {
'fr': "Je n'arrive pas à me connecter à",
'en': "I can't connect to"
};
var message_search_failed = {
'fr': "Désolé je n'ai rien trouvé",
'en': "Sorry, I couldn't find anything"
};
var message_found_something = {
'fr': "J'ai trouvé ça",
'en': "This is what I found"
};
// Initialize bot
var controller = Botkit.slackbot({
debug: false,
});
var bot = controller.spawn({
token: process.env.token
}).startRTM();
// Get remote wiki URL
var server_wiki_path = ''
var exec = require('child_process').exec;
const command = 'git -C "'+local_wiki_path+'" remote get-url origin'
console.log('WIKI GET ORIGIN:', command)
exec(command,function(err, stdout, stderr) {
if(err) {
console.log('DEBUG','Host not found: '+local_wiki_path);
} else {
server_wiki_path = stdout.replace('.git','/')
server_wiki_path = server_wiki_path.replace(/^\s+|\s+$/g, ''); // trim
var gitregex = 'git@(.*?):(.*?).wiki/'; // in case format was git
server_wiki_path = server_wiki_path.replace(new RegExp(gitregex, 'g'), "https://$1/$2/wikis/");
}
});
// Update local wiki
exec('git -C "'+local_wiki_path+'" pull',function(err, stdout, stderr) {
if(err) {
console.log('DEBUG', 'connection to server failed: '+server_wiki_path);
}
});
controller.hears(["(.*)"],
'direct_message,direct_mention,mention', function(bot, message) {
const what = message.match[1]; // get user input
console.log('WIKI RECEIVED REQUEST', what)
// Command to look up wiki
var command = 'git -C "'+local_wiki_path+'" '
command += '-c color.grep.match=red ' // force grep to highlight found text in red (we use that later)
command += 'grep --color=always -in -e '+what.split(' ').join(' --and -e ')
command += " -- *.md" // filter on markdown files
command += " | less -R" // print raw control characters
//console.log('WIKI EXEC COMMAND:', command) //debug
bot.reply(message, message_got_it[lang])
// Update local wiki
// (note that exec doesnt wait to be done... but we're updating wiki for next time)
var exec = require('child_process').exec;
exec('git -C "'+local_wiki_path+'" pull',function(err, stdout, stderr) {
if(err) {
bot.reply(message, message_connection_failed[lang]+" "+server_wiki_path);
console.log('DEBUG','couldnt git pull wiki from '+server_wiki_path)
}
});
// Look up wiki
var child = exec(command,function(err, stdout, stderr) {
if(err) {
bot.reply(message, message_search_failed[lang]);
} else {
function formatResults(text) {
var lines = text
function escape(s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
};
// We asked grep to highlight the selected words in red
// here I get the highlighted words positions matching the
// ANSI escape codes >>> ESC[31m...ESC[m
var search_match = "\\033\\[31m(.+?)\\033\\[m"
var search_others = "\\033\\[[0-9]+m(.+?)\\033\\[m"
lines = lines.replace(new RegExp(search_match, 'g'), "`$1`");
lines = lines.replace(new RegExp(search_others, 'g'), "$1")
// Loop on Markdown files
var re = /(.+\.md):(.*)/g
results = {};
while (match = re.exec(lines)) {
// Replace markdown files with full path
var file = match[1].replace('.md','')
var file = '<'+server_wiki_path+file+'|'+file+'>'
var value = match[2]
if (!(file in results)) {
results[file] = "";
}
/*
// Highlight requested words
whatsplit = what.split(' ')
for (k in whatsplit) {
value = value.replace(whatsplit[k], " `"+whatsplit[k]+"` ");
}*/
// Aggregate results for the same Markdown file
results[file] += value + "\n";
}
var output = "";
var keys = [];
for (var key in results) {
output += "\n"+key+"\n"+results[key]
}
return output;
};
output = formatResults(stdout);
if (output.length>0) {
bot.reply(message, message_found_something[lang]+":\n"+output);
} else {
bot.reply(message, message_search_failed[lang]);
console.log('debug', 'found something but couldnt format it properly')
};
};
});
});
// Keep bot alive
function start_rtm() {
bot.startRTM(function(err,bot,payload) {
if (err) {
console.log('Failed to start RTM')
return setTimeout(start_rtm, 60000);
}
console.log("RTM started!");
});
};
controller.on('rtm_close', function(bot, err) {
start_rtm();
});