-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
383 lines (323 loc) · 9.31 KB
/
index.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
'use strict';
let core = require('botkit').core
let Path = require('path')
let request = require('request')
let express = require('express')
let chalk = require('chalk')
let cline = require('cline')
let ansi = require('ansi')
var bodyParser = require('body-parser');
function worker (botkit, config) {
let bot = {
botkit: botkit,
config: config || {},
utterances: botkit.utterances,
identity: { // default identity values
id: 'shellbot',
name: 'ShellBot'
}
}
botkit.startTicking()
let cl = cline()
cl.on('close', () => { process.exit() })
let cursor = ansi(process.stdout)
let channel = '##'
function setChannel (newChannel) {
channel = newChannel
cl.interact(chalk.gray(`${channel}> `))
}
cl.command('/dm', 'switch to DM context', () => { setChannel('DM') })
cl.command('/ch', 'switch to channel context', () => { setChannel('##') })
cl.command('*', (input) => {
botkit.debug(`GOT '${input}'`)
botkit.receiveMessage(bot, {
event: {
type: 'message',
},
channel: channel,
user: 'SHELLUSER',
text: input,
ts: new Date().getTime()
})
return false
})
setTimeout(() => { setChannel('##') }, 10)
/**
* Convenience method for creating a DM convo.
*/
bot.startPrivateConversation = function (message, cb) {
botkit.startTask(this, message, function (task, convo) {
bot._startDM(task, message.user, function (err, dm) {
convo.stop()
cb(err, dm)
})
})
}
bot.startConversation = function (message, cb) {
botkit.startConversation(this, message, cb)
}
/**
* Convenience method for creating a DM convo.
*/
bot._startDM = function (task, userId, cb) {
bot.api.im.open({
user: userId
}, function (err, channel) {
if (err) return cb(err)
cb(null, task.startConversation({
channel: channel.channel.id,
user: userId
}))
})
}
bot.send = function (message, cb) {
botkit.debug('SAY', message)
/**
* Construct a valid slack message.
*/
var slackMessage = {
type: message.type || 'message',
channel: message.channel,
text: message.text || null,
username: message.username || null,
parse: message.parse || null,
link_names: message.link_names || null,
attachments: message.attachments
? JSON.stringify(message.attachments)
: null,
unfurl_links: typeof message.unfurl_links !== 'undefined' ? message.unfurl_links : null,
unfurl_media: typeof message.unfurl_media !== 'undefined' ? message.unfurl_media : null,
icon_url: message.icon_url || null,
icon_emoji: message.icon_emoji || null
}
bot.msgcount++
if (message.icon_url || message.icon_emoji || message.username) {
slackMessage.as_user = false
} else {
slackMessage.as_user = message.as_user || true
}
/**
* These options are not supported by the RTM
* so if they are specified, we use the web API to send messages.
*/
cursor.horizontalAbsolute(0).eraseLine()
if (message.attachments || message.icon_emoji || message.username || message.icon_url) {
console.log(chalk.blue(JSON.stringify(message, null, 2)))
} else {
console.log(chalk.blue(message.text))
}
cl.prompt(chalk.grey(`${channel}> `))
}
bot.replyPublic = function (src, resp, cb) {
if (!bot.res) {
cb && cb('No web response object found')
} else {
var msg = {}
if (typeof resp === 'string') {
msg.text = resp
} else {
msg = resp
}
msg.channel = src.channel
msg.response_type = 'in_channel'
bot.res.json(msg)
cb && cb()
}
}
bot.replyPublicDelayed = function (src, resp, cb) {
if (!src.response_url) {
cb && cb('No response_url found')
} else {
var msg = {}
if (typeof resp === 'string') {
msg.text = resp
} else {
msg = resp
}
msg.channel = src.channel
msg.response_type = 'in_channel'
var requestOptions = {
uri: src.response_url,
method: 'POST',
json: msg
}
request(requestOptions, function (err, resp, body) {
/**
* Do something?
*/
if (err) {
botkit.log.error('Error sending slash command response:', err)
cb && cb(err)
} else {
cb && cb()
}
})
}
}
bot.replyPrivate = function (src, resp, cb) {
if (!bot.res) {
cb && cb('No web response object found')
} else {
var msg = {}
if (typeof resp === 'string') {
msg.text = resp
} else {
msg = resp
}
msg.channel = src.channel
msg.response_type = 'ephemeral'
bot.res.json(msg)
cb && cb()
}
}
bot.replyPrivateDelayed = function (src, resp, cb) {
if (!src.response_url) {
cb && cb('No response_url found')
} else {
var msg = {}
if (typeof resp === 'string') {
msg.text = resp
} else {
msg = resp
}
msg.channel = src.channel
msg.response_type = 'ephemeral'
var requestOptions = {
uri: src.response_url,
method: 'POST',
json: msg
}
request(requestOptions, function (err, resp, body) {
/**
* Do something?
*/
if (err) {
botkit.log.error('Error sending slash command response:', err)
cb && cb(err)
} else {
cb && cb()
}
})
}
}
bot.reply = function (src, resp, cb) {
var msg = {}
if (typeof resp === 'string') {
msg.text = resp
} else {
msg = resp
}
msg.channel = src.channel
bot.say(msg, cb)
}
bot.replyInThread = function (src, resp, cb) {
var msg = {}
if (typeof resp === 'string') {
msg.text = resp
} else {
msg = resp
}
msg.channel = src.channel
bot.say(msg, cb)
}
/**
* sends a typing message to the source channel
*
* @param {Object} src message source
*/
bot.startTyping = function (src) {
bot.reply(src, {
type: 'typing'
})
}
/**
* replies with message after typing delay
*
* @param {Object} src message source
* @param {(string|Object)} resp string or object
* @param {function} cb optional request callback
*/
bot.replyWithTyping = function (src, resp, cb) {
var text
if (typeof resp === 'string') {
text = resp
} else {
text = resp.text
}
var typingLength = 1200 / 60 * text.length
typingLength = typingLength > 2000 ? 2000 : typingLength
bot.startTyping(src)
setTimeout(function () {
bot.reply(src, resp, cb)
}, typingLength)
}
/**
* This handles the particulars of finding an existing conversation or
* topic to fit the message into...
*/
bot.findConversation = function (message, cb) {
botkit.debug('CUSTOM FIND CONVO', message.user, message.channel)
if (message.type === 'message' || message.type === 'slash_command' ||
message.type === 'outgoing_webhook') {
for (var t = 0; t < botkit.tasks.length; t++) {
for (var c = 0; c < botkit.tasks[t].convos.length; c++) {
if (
botkit.tasks[t].convos[c].isActive() &&
botkit.tasks[t].convos[c].source_message.user === message.user &&
botkit.tasks[t].convos[c].source_message.channel === message.channel
) {
botkit.debug('FOUND EXISTING CONVO!')
// modify message text to prune off the bot's name (@bot hey -> hey)
// and trim whitespace that is sometimes added
// this would otherwise happen in the handleSlackEvents function
// which does not get called for messages attached to conversations.
if (message.text) {
message.text = message.text.trim()
}
var directMention = new RegExp(`\<\@${bot.identity.id}\>`, 'i')
message.text = message.text.replace(directMention, '')
.replace(/^\s+/, '').replace(/^:\s+/, '').replace(/^\s+/, '')
cb(botkit.tasks[t].convos[c])
return
}
}
}
}
cb()
}
bot.res = {
json: (obj) => { console.log(chalk.blue(JSON.stringify(obj, null, 2))) }
}
return bot
}
module.exports = function Shellbot (configuration) {
let corebot = core(configuration || {})
corebot.defineBot(worker)
corebot.setupWebserver = function (port, cb) {
corebot.webserver = express()
corebot.webserver.use(bodyParser.json())
corebot.webserver.use(bodyParser.urlencoded({ extended: true }))
corebot.webserver.use(express.static(Path.resolve(__dirname, '/public')))
corebot.webserver.listen(port, () => {
corebot.log('** Starting webserver on port ' + port)
if (cb) { cb(null, corebot.webserver) }
})
return corebot
}
corebot.on('message_received', (bot, msg) => {
let evt
if (msg.channel === 'DM') {
evt = 'direct_message'
} else {
if (RegExp(`^@${bot.identity.name}`, 'i').test(msg.text)) {
msg.text = msg.text.replace(RegExp(`^@${bot.identity.name}:?\\s*`, 'i'), '')
console.log(chalk.red(msg.text))
evt = 'direct_mention'
} else if (RegExp(`@${bot.identity.name}`, 'i').test(msg.text)) {
evt = 'mention'
} else evt = 'ambient'
}
corebot.trigger(evt, [bot, msg])
})
return corebot
}