brobbot
Version:
A simple helpful robot for your Company
86 lines (74 loc) • 1.95 kB
text/coffeescript
# Description:
# Generates help commands for Brobbot.
#
# Commands:
# brobbot help - Displays all of the help commands that Brobbot knows about.
# brobbot help <query> - Displays all help commands that match <query>.
#
# URLS:
# /brobbot/help
#
# Notes:
# These commands are grabbed from comment blocks at the top of each file.
helpContents = (name, commands) ->
"""
<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) ->
cmds = robot.helpCommands()
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
prefix = robot.alias or robot.name
cmds = cmds.map (cmd) ->
cmd = cmd.replace /brobbot/ig, robot.name
cmd.replace new RegExp("^#{robot.name}"), prefix
emit = cmds.join "\n"
msg.send emit
robot.router.get "/#{robot.name}/help", (req, res) ->
cmds = robot.helpCommands().map (cmd) ->
cmd.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')
emit = "<p>#{cmds.join '</p><p>'}</p>"
emit = emit.replace /brobbot/ig, "<b>#{robot.name}</b>"
res.setHeader 'content-type', 'text/html'
res.end helpContents robot.name, emit