-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (91 loc) · 2.8 KB
/
index.js
File metadata and controls
97 lines (91 loc) · 2.8 KB
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
var botkit = require('botkit');
var exec = require('child_process').exec;
var path = require('path');
var sprintf = require('sprintf');
var fs = require('fs');
var Glob = require("glob").Glob;
var fields = [],
titles = [],
count = 0,
config = {
slack: {
token: process.env.SLACK_TOKEN,
icon_emoji: ':question:',
username: 'help bot',
},
color: {
red: '#ff0000',
}
}
var controller = botkit.slackbot({
debug: false
});
controller.spawn({
token: config.slack.token
}).startRTM();
controller.hears([/^bot\s+(help|usage)(\s+(\S+))?/], ['message_received', 'ambient'],
function(bot, message) {
var name = message.match[3];
if (typeof name !== 'undefined') {
var json = path.join(__dirname, '..', name, 'usage.json');
if (!isExists(json)) {
return bot.reply(message, {
attachments: [{
text: name + ': no such bot',
color: config.color.red,
}],
icon_emoji: config.slack.icon_emoji,
username: config.slack.username,
});
}
var attachments = require(json);
return bot.reply(message, {
attachments: attachments,
icon_emoji: config.slack.icon_emoji,
username: config.slack.username,
});
}
Glob('../*-bot/usage.json', {
cwd: __dirname,
silent: true,
}, function(err, matches) {
if (err) {
return bot.reply(message, {
attachments: [{
text: err,
color: config.color.red
}]
});
}
matches.forEach(function(match) {
json = require(match);
json.forEach(function(u) {
titles = [];
u.fields.forEach(function(f) {
titles.push(f.title);
});
fields[count++] = {
'title': u.author_name,
'value': titles.join('\n'),
'short': false,
};
});
});
return bot.reply(message, {
attachments: [{
pretext: 'For more info, type "bot help xxx-bot" to show usage!',
fields: fields,
}],
icon_emoji: config.slack.icon_emoji,
username: config.slack.username,
});
});
});
function isExists(file) {
try {
fs.statSync(file);
return true;
} catch (e) {
return false;
}
}