This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from aaronott/master
Add a call for commandlinefu
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Description: | ||
# Returns a command from commandlinefu.com | ||
# | ||
# Dependencies: | ||
# None | ||
# | ||
# Configuration: | ||
# None | ||
# | ||
# Commands: | ||
# hubot commandlinefu me - returns random command | ||
# hubot commandlinefu me <command> - random entry for the comand passed | ||
# | ||
# Author: | ||
# aaronott | ||
|
||
module.exports = (robot) -> | ||
robot.respond /commandlinefu(?: me)? *(.*)?/i, (msg) -> | ||
query = if msg.match[1] | ||
"matching/#{msg.match[1]}/#{new Buffer(msg.match[1]).toString('base64')}/json" | ||
else | ||
"random/json" | ||
command msg, "http://www.commandlinefu.com/commands/#{query}", (cmd) -> | ||
msg.send cmd | ||
|
||
command = (msg, uri, cb) -> | ||
msg.http(uri) | ||
.get() (err, res, body) -> | ||
# The random call passes back a 302 to redirect to a new page, if this | ||
# happens we redirect through a recursive function call passing the new | ||
# location to retrieve | ||
if res.statusCode == 302 | ||
command msg, res.headers.location, cb | ||
else | ||
# choose a random command from the returned list | ||
cc = msg.random JSON.parse(body) | ||
cb("-- #{cc.summary}\n#{cc.command}") |