Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jarib committed Mar 9, 2013
0 parents commit 6c41dd5
Show file tree
Hide file tree
Showing 18 changed files with 605 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
.DS_Store*
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: bin/hubot --adapter hipchat -n hubot
164 changes: 164 additions & 0 deletions README.md
@@ -0,0 +1,164 @@
# Hubot

This is a version of GitHub's Campfire bot, hubot. He's pretty cool.

This version is designed to be deployed on [Heroku][heroku].

[heroku]: http://www.heroku.com

## Playing with Hubot

You'll need to install the necessary dependencies for hubot. All of
those dependencies are provided by [npm][npmjs].

[npmjs]: http://npmjs.org

## HTTP Listener

Hubot has a HTTP listener which listens on the port specified by the `PORT`
environment variable.

You can specify routes to listen on in your scripts by using the `router`
property on `robot`.

```coffeescript
module.exports = (robot) ->
robot.router.get "/hubot/version", (req, res) ->
res.end robot.version
```

There are functions for GET, POST, PUT and DELETE, which all take a route and
callback function that accepts a request and a response.

### Redis

If you are going to use the `redis-brain.coffee` script from `hubot-scripts`
you will need to add the Redis to Go addon on Heroku which requires a verified
account or you can create an account at [Redis to Go][redistogo] and manually
set the `REDISTOGO_URL` variable.

% heroku config:add REDISTOGO_URL="..."

If you don't require any persistence feel free to remove the
`redis-brain.coffee` from `hubot-scripts.json` and you don't need to worry
about redis at all.

[redistogo]: https://redistogo.com/

### Testing Hubot Locally

You can test your hubot by running the following.

% bin/hubot

You'll see some start up output about where your scripts come from and a
prompt.

[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading adapter shell
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading scripts from /home/tomb/Development/hubot/scripts
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading scripts from /home/tomb/Development/hubot/src/scripts
Hubot>

Then you can interact with hubot by typing `hubot help`.

Hubot> hubot help

Hubot> animate me <query> - The same thing as `image me`, except adds a few
convert me <expression> to <units> - Convert expression to given units.
help - Displays all of the help commands that Hubot knows about.
...

Take a look at the scripts in the `./scripts` folder for examples.
Delete any scripts you think are silly. Add whatever functionality you
want hubot to have.

## Adapters

Adapters are the interface to the service you want your hubot to run on. This
can be something like Campfire or IRC. There are a number of third party
adapters that the community have contributed. Check the
[hubot wiki][hubot-wiki] for the available ones.

If you would like to run a non-Campfire or shell adapter you will need to add
the adapter package as a dependency to the `package.json` file in the
`dependencies` section.

Once you've added the dependency and run `npm install` to install it you can
then run hubot with the adapter.

% bin/hubot -a <adapter>

Where `<adapter>` is the name of your adapter without the `hubot-` prefix.

[hubot-wiki]: https://github.com/github/hubot/wiki

## hubot-scripts

There will inevitably be functionality that everyone will want. Instead
of adding it to hubot itself, you can submit pull requests to
[hubot-scripts][hubot-scripts].

To enable scripts from the hubot-scripts package, add the script name with
extension as a double quoted string to the hubot-scripts.json file in this
repo.

[hubot-scripts]: https://github.com/github/hubot-scripts

## Deployment

% heroku create --stack cedar
% git push heroku master
% heroku ps:scale app=1

If your Heroku account has been verified you can run the following to enable
and add the Redis to Go addon to your app.

% heroku addons:add redistogo:nano

If you run into any problems, checkout Heroku's [docs][heroku-node-docs].

You'll need to edit the `Procfile` to set the name of your hubot.

More detailed documentation can be found on the
[deploying hubot onto Heroku][deploy-heroku] wiki page.

### Deploying to UNIX or Windows

If you would like to deploy to either a UNIX operating system or Windows.
Please check out the [deploying hubot onto UNIX][deploy-unix] and
[deploying hubot onto Windows][deploy-windows] wiki pages.

[heroku-node-docs]: http://devcenter.heroku.com/articles/node-js
[deploy-heroku]: https://github.com/github/hubot/wiki/Deploying-Hubot-onto-Heroku
[deploy-unix]: https://github.com/github/hubot/wiki/Deploying-Hubot-onto-UNIX
[deploy-windows]: https://github.com/github/hubot/wiki/Deploying-Hubot-onto-Windows

## Campfire Variables

If you are using the Campfire adapter you will need to set some environment
variables. Refer to the documentation for other adapters and the configuraiton
of those, links to the adapters can be found on the [hubot wiki][hubot-wiki].

Create a separate Campfire user for your bot and get their token from the web
UI.

% heroku config:add HUBOT_CAMPFIRE_TOKEN="..."

Get the numeric IDs of the rooms you want the bot to join, comma delimited. If
you want the bot to connect to `https://mysubdomain.campfirenow.com/room/42`
and `https://mysubdomain.campfirenow.com/room/1024` then you'd add it like this:

% heroku config:add HUBOT_CAMPFIRE_ROOMS="42,1024"

Add the subdomain hubot should connect to. If you web URL looks like
`http://mysubdomain.campfirenow.com` then you'd add it like this:

% heroku config:add HUBOT_CAMPFIRE_ACCOUNT="mysubdomain"

[hubot-wiki]: https://github.com/github/hubot/wiki

## Restart the bot

You may want to get comfortable with `heroku logs` and `heroku restart`
if you're having issues.

7 changes: 7 additions & 0 deletions bin/hubot
@@ -0,0 +1,7 @@
#!/bin/sh

npm install
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"

exec node_modules/.bin/hubot "$@"

1 change: 1 addition & 0 deletions hubot-scripts.json
@@ -0,0 +1 @@
["tweet.coffee", "shipit.coffee", "gif-me.coffee"]
24 changes: 24 additions & 0 deletions package.json
@@ -0,0 +1,24 @@
{
"name": "hosted-hubot",
"version": "2.2.0",
"author": "GitHub Inc.",
"keywords": "github hubot campfire bot",
"description": "A simple helpful Robot for your Company",
"licenses": [{
"type": "MIT",
"url": "http://github.com/github/hubot/raw/master/LICENSE"
}],

"repository" : {
"type" : "git",
"url" : "http://github.com/github/hubot.git"
},

"dependencies": {
"hubot": "2.2.0",
"hubot-scripts": ">=2.0.8",
"optparse": "1.0.3",
"hubot-hipchat": ">= 2.4.5"
}
}

39 changes: 39 additions & 0 deletions scripts/google-images.coffee
@@ -0,0 +1,39 @@
# A way to interact with the Google Images API.
#
# image me <query> - The Original. Queries Google Images for <query> and
# returns a random top result.
# animate me <query> - The same thing as `image me`, except adds a few
# parameters to try to return an animated GIF instead.
# mustache me <url> - Adds a mustache to the specified URL.
# mustache me <query> - Searches Google Images for the specified query and
# mustaches it.
module.exports = (robot) ->
robot.respond /(image|img)( me)? (.*)/i, (msg) ->
imageMe msg, msg.match[3], (url) ->
msg.send url

robot.respond /animate me (.*)/i, (msg) ->
imageMe msg, "animated #{msg.match[1]}", (url) ->
msg.send url

robot.respond /(?:mo?u)?sta(?:s|c)he?(?: me)? (.*)/i, (msg) ->
type = Math.floor(Math.random() * 3)
mustachify = "http://mustachify.me/#{type}?src="
imagery = msg.match[1]

if imagery.match /^https?:\/\//i
msg.send "#{mustachify}#{imagery}"
else
imageMe msg, imagery, (url) ->
msg.send "#{mustachify}#{url}"

imageMe = (msg, query, cb) ->
msg.http('http://ajax.googleapis.com/ajax/services/search/images')
.query(v: "1.0", rsz: '8', q: query, safe: 'active')
.get() (err, res, body) ->
images = JSON.parse(body)
images = images.responseData.results
if images.length > 0
image = msg.random images
cb "#{image.unescapedUrl}#.png"

16 changes: 16 additions & 0 deletions scripts/help.coffee
@@ -0,0 +1,16 @@
# Generates help commands for Hubot.
#
# These commands are grabbed from comment blocks at the top of each file.
#
# help - Displays all of the help commands that Hubot knows about.
# help <query> - Displays all help commands that match <query>.

module.exports = (robot) ->
robot.respond /help\s*(.*)?$/i, (msg) ->
cmds = robot.helpCommands()
if msg.match[1]
cmds = cmds.filter (cmd) -> cmd.match(new RegExp(msg.match[1], 'i'))
emit = cmds.join("\n")
unless robot.name is 'Hubot'
emit = emit.replace(/(H|h)ubot/g, robot.name)
msg.send emit
20 changes: 20 additions & 0 deletions scripts/httpd.coffee
@@ -0,0 +1,20 @@
# A simple interaction with the built in HTTP Daemon
spawn = require('child_process').spawn

module.exports = (robot) ->

robot.router.get "/hubot/version", (req, res) ->
res.end robot.version

robot.router.post "/hubot/ping", (req, res) ->
res.end "PONG"

robot.router.get "/hubot/time", (req, res) ->
res.end "Server time is: #{new Date()}"

robot.router.get "/hubot/info", (req, res) ->
child = spawn('/bin/sh', ['-c', "echo I\\'m $LOGNAME@$(hostname):$(pwd) \\($(git rev-parse HEAD)\\)"])

child.stdout.on 'data', (data) ->
res.end "#{data.toString().trim()} running node #{process.version} [pid: #{process.pid}]"
child.stdin.end()
24 changes: 24 additions & 0 deletions scripts/maps.coffee
@@ -0,0 +1,24 @@
# Interacts with the Google Maps API.
#
# map me <query> - Returns a map view of the area returned by `query`.

module.exports = (robot) ->

robot.respond /(?:(satellite|terrain|hybrid)[- ])?map me (.+)/i, (msg) ->
mapType = msg.match[1] or "roadmap"
location = msg.match[2]
mapUrl = "http://maps.google.com/maps/api/staticmap?markers=" +
escape(location) +
"&size=400x400&maptype=" +
mapType +
"&sensor=false" +
"&format=png" # So campfire knows it's an image
url = "http://maps.google.com/maps?q=" +
escape(location) +
"&hl=en&sll=37.0625,-95.677068&sspn=73.579623,100.371094&vpsrc=0&hnear=" +
escape(location) +
"&t=m&z=11"

msg.send mapUrl
msg.send url

20 changes: 20 additions & 0 deletions scripts/math.coffee
@@ -0,0 +1,20 @@
# Allows Hubot to do mathematics.
#
# math me <expression> - Calculate the given expression.
# convert me <expression> to <units> - Convert expression to given units.
module.exports = (robot) ->
robot.respond /(calc|calculate|convert|math)( me)? (.*)/i, (msg) ->
msg
.http('http://www.google.com/ig/calculator')
.query
hl: 'en'
q: msg.match[3]
.headers
'Accept-Language': 'en-us,en;q=0.5',
'Accept-Charset': 'utf-8',
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
.get() (err, res, body) ->
# Response includes non-string keys, so we can't use JSON.parse here.
json = eval("(#{body})")
msg.send json.rhs || 'Could not compute.'

15 changes: 15 additions & 0 deletions scripts/ping.coffee
@@ -0,0 +1,15 @@
# Utility commands surrounding Hubot uptime.
module.exports = (robot) ->
robot.respond /PING$/i, (msg) ->
msg.send "PONG"

robot.respond /ECHO (.*)$/i, (msg) ->
msg.send msg.match[1]

robot.respond /TIME$/i, (msg) ->
msg.send "Server time is: #{new Date()}"

robot.respond /DIE$/i, (msg) ->
msg.send "Goodbye, cruel world."
process.exit 0

23 changes: 23 additions & 0 deletions scripts/pugme.coffee
@@ -0,0 +1,23 @@
# Pugme is the most important thing in your life
#
# pug me - Receive a pug
# pug bomb N - get N pugs

module.exports = (robot) ->

robot.respond /pug me/i, (msg) ->
msg.http("http://pugme.herokuapp.com/random")
.get() (err, res, body) ->
msg.send JSON.parse(body).pug

robot.respond /pug bomb( (\d+))?/i, (msg) ->
count = msg.match[2] || 5
msg.http("http://pugme.herokuapp.com/bomb?count=" + count)
.get() (err, res, body) ->
msg.send pug for pug in JSON.parse(body).pugs

robot.respond /how many pugs are there/i, (msg) ->
msg.http("http://pugme.herokuapp.com/count")
.get() (err, res, body) ->
msg.send "There are #{JSON.parse(body).pug_count} pugs."

0 comments on commit 6c41dd5

Please sign in to comment.