forked from hubotio/hubot-help
-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.js
126 lines (108 loc) · 3.41 KB
/
help.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
'use strict'
// Description:
// Generates help commands for Hubot.
//
// Commands:
// hubot help - Displays all of the help commands that this bot knows about.
// hubot help <query> - Displays all help commands that match <query>.
//
// URLS:
// /hubot/help
//
// Configuration:
// HUBOT_HELP_REPLY_IN_PRIVATE - if set to any value, all `hubot help` replies are sent in private
// HUBOT_HELP_DISABLE_HTTP - if set, no web entry point will be declared
// HUBOT_HELP_HIDDEN_COMMANDS - comma-separated list of commands that will not be displayed in help
//
// Notes:
// These commands are grabbed from comment blocks at the top of each file.
const helpContents = (name, commands) => `\
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>${name} Help</title>
<style type="text/css">
body {
background: #d3d6d9;
color: #636c75;
text-shadow: 0 1px 1px rgba(255, 255, 255, .5);
font-family: Helvetica, Arial, sans-serif;
}
h1 {
margin: 8px 0;
padding: 0;
}
.commands {
font-size: 13px;
}
p {
border-bottom: 1px solid #eee;
margin: 6px 0 0 0;
padding-bottom: 5px;
}
p:last-child {
border: 0;
}
</style>
</head>
<body>
<h1>${name} Help</h1>
<div class="commands">
${commands}
</div>
</body>
</html>\
`
module.exports = (robot) => {
robot.respond(/help(?:\s+(.*))?$/i, (msg) => {
let cmds = getHelpCommands(robot)
const filter = msg.match[1]
if (filter) {
cmds = cmds.filter(cmd => cmd.match(new RegExp(filter, 'i')))
if (cmds.length === 0) {
msg.send(`No available commands match ${filter}`)
return
}
}
const emit = cmds.join('\n')
if (process.env.HUBOT_HELP_REPLY_IN_PRIVATE && msg.message && msg.message.user && msg.message.user.name && msg.message.user.name !== msg.message.room) {
msg.reply('I just replied to you in private.')
return msg.sendPrivate(emit)
} else {
return msg.send(emit)
}
})
if (process.env.HUBOT_HELP_DISABLE_HTTP == null) {
return robot.router.get(`/${robot.name}/help`, (req, res) => {
let cmds = getHelpCommands(robot).map(cmd => cmd.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'))
if (req.query.q != null) {
cmds = cmds.filter(cmd => cmd.match(new RegExp(req.query.q, 'i')))
}
let emit = `<p>${cmds.join('</p><p>')}</p>`
emit = emit.replace(new RegExp(`${robot.name}`, 'ig'), `<b>${robot.name}</b>`)
res.setHeader('content-type', 'text/html')
res.end(helpContents(robot.name, emit))
})
}
}
var getHelpCommands = function getHelpCommands (robot) {
let helpCommands = robot.helpCommands()
const robotName = robot.alias || robot.name
if (hiddenCommandsPattern()) {
helpCommands = helpCommands.filter(command => !hiddenCommandsPattern().test(command))
}
helpCommands = helpCommands.map((command) => {
if (robotName.length === 1) {
return command.replace(/^hubot\s*/i, robotName)
}
return command.replace(/^hubot/i, robotName)
})
return helpCommands.sort()
}
var hiddenCommandsPattern = function hiddenCommandsPattern () {
const hiddenCommands = process.env.HUBOT_HELP_HIDDEN_COMMANDS != null ? process.env.HUBOT_HELP_HIDDEN_COMMANDS.split(',') : undefined
if (hiddenCommands) {
return new RegExp(`^hubot (?:${hiddenCommands != null ? hiddenCommands.join('|') : undefined}) - `)
}
}