Skip to content

Commit 16f67d2

Browse files
committed
Initial commit.
0 parents  commit 16f67d2

22 files changed

+898
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store*

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bin/hubot -a campfire -n Hubot

README.md

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Hubot
2+
3+
This is a version of GitHub's Campfire bot, hubot. He's pretty cool.
4+
5+
This version is designed to be deployed on [Heroku][heroku].
6+
7+
[heroku]: http://www.heroku.com
8+
9+
## Playing with Hubot
10+
11+
You'll need to install the necessary dependencies for hubot. All of
12+
those dependencies are provided by [npm][npmjs].
13+
14+
[npmjs]: http://npmjs.org
15+
16+
## HTTP Listener
17+
18+
Hubot has a HTTP listener which listens on the port specified by the `PORT`
19+
environment variable.
20+
21+
You can specify routes to listen on in your scripts by using the `router`
22+
property on `robot`.
23+
24+
```coffeescript
25+
module.exports = (robot) ->
26+
robot.router.get "/hubot/version", (req, res) ->
27+
res.end robot.version
28+
```
29+
30+
There are functions for GET, POST, PUT and DELETE, which all take a route and
31+
callback function that accepts a request and a response.
32+
33+
### Redis
34+
35+
If you are going to use the `redis-brain.coffee` script from `hubot-scripts`
36+
you will need to add the Redis to Go addon on Heroku which requires a verified
37+
account or you can create an account at [Redis to Go][redistogo] and manually
38+
set the `REDISTOGO_URL` variable.
39+
40+
% heroku config:add REDISTOGO_URL="..."
41+
42+
If you don't require any persistence feel free to remove the
43+
`redis-brain.coffee` from `hubot-scripts.json` and you don't need to worry
44+
about redis at all.
45+
46+
[redistogo]: https://redistogo.com/
47+
48+
### Testing Hubot Locally
49+
50+
You can test your hubot by running the following.
51+
52+
% bin/hubot
53+
54+
You'll see some start up output about where your scripts come from and a
55+
prompt.
56+
57+
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading adapter shell
58+
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading scripts from /home/tomb/Development/hubot/scripts
59+
[Sun, 04 Dec 2011 18:41:11 GMT] INFO Loading scripts from /home/tomb/Development/hubot/src/scripts
60+
Hubot>
61+
62+
Then you can interact with hubot by typing `hubot help`.
63+
64+
Hubot> hubot help
65+
66+
Hubot> animate me <query> - The same thing as `image me`, except adds a few
67+
convert me <expression> to <units> - Convert expression to given units.
68+
help - Displays all of the help commands that Hubot knows about.
69+
...
70+
71+
Take a look at the scripts in the `./scripts` folder for examples.
72+
Delete any scripts you think are silly. Add whatever functionality you
73+
want hubot to have.
74+
75+
## Adapters
76+
77+
Adapters are the interface to the service you want your hubot to run on. This
78+
can be something like Campfire or IRC. There are a number of third party
79+
adapters that the community have contributed. Check the
80+
[hubot wiki][hubot-wiki] for the available ones.
81+
82+
If you would like to run a non-Campfire or shell adapter you will need to add
83+
the adapter package as a dependency to the `package.json` file in the
84+
`dependencies` section.
85+
86+
Once you've added the dependency and run `npm install` to install it you can
87+
then run hubot with the adapter.
88+
89+
% bin/hubot -a <adapter>
90+
91+
Where `<adapter>` is the name of your adapter without the `hubot-` prefix.
92+
93+
[hubot-wiki]: https://github.com/github/hubot/wiki
94+
95+
## hubot-scripts
96+
97+
There will inevitably be functionality that everyone will want. Instead
98+
of adding it to hubot itself, you can submit pull requests to
99+
[hubot-scripts][hubot-scripts].
100+
101+
To enable scripts from the hubot-scripts package, add the script name with
102+
extension as a double quoted string to the `hubot-scripts.json` file in this
103+
repo.
104+
105+
[hubot-scripts]: https://github.com/github/hubot-scripts
106+
107+
## external-scripts
108+
109+
Tired of waiting for your script to be merged into `hubot-scripts`? Want to
110+
maintain the repository and package yourself? Then this added functionality
111+
maybe for you!
112+
113+
Hubot is now able to load scripts from third-party `npm` packages! To enable
114+
this functionality you can follow the following steps.
115+
116+
1. Add the packages as dependencies into your `package.json`
117+
2. `npm install` to make sure those packages are installed
118+
119+
To enable third-party scripts that you've added you will need to add the package
120+
name as a double quoted string to the `external-scripts.json` file in this repo.
121+
122+
## Deployment
123+
124+
% heroku create --stack cedar
125+
% git push heroku master
126+
% heroku ps:scale app=1
127+
128+
If your Heroku account has been verified you can run the following to enable
129+
and add the Redis to Go addon to your app.
130+
131+
% heroku addons:add redistogo:nano
132+
133+
If you run into any problems, checkout Heroku's [docs][heroku-node-docs].
134+
135+
You'll need to edit the `Procfile` to set the name of your hubot.
136+
137+
More detailed documentation can be found on the
138+
[deploying hubot onto Heroku][deploy-heroku] wiki page.
139+
140+
### Deploying to UNIX or Windows
141+
142+
If you would like to deploy to either a UNIX operating system or Windows.
143+
Please check out the [deploying hubot onto UNIX][deploy-unix] and
144+
[deploying hubot onto Windows][deploy-windows] wiki pages.
145+
146+
[heroku-node-docs]: http://devcenter.heroku.com/articles/node-js
147+
[deploy-heroku]: https://github.com/github/hubot/wiki/Deploying-Hubot-onto-Heroku
148+
[deploy-unix]: https://github.com/github/hubot/wiki/Deploying-Hubot-onto-UNIX
149+
[deploy-windows]: https://github.com/github/hubot/wiki/Deploying-Hubot-onto-Windows
150+
151+
## Campfire Variables
152+
153+
If you are using the Campfire adapter you will need to set some environment
154+
variables. Refer to the documentation for other adapters and the configuraiton
155+
of those, links to the adapters can be found on the [hubot wiki][hubot-wiki].
156+
157+
Create a separate Campfire user for your bot and get their token from the web
158+
UI.
159+
160+
% heroku config:add HUBOT_CAMPFIRE_TOKEN="..."
161+
162+
Get the numeric IDs of the rooms you want the bot to join, comma delimited. If
163+
you want the bot to connect to `https://mysubdomain.campfirenow.com/room/42`
164+
and `https://mysubdomain.campfirenow.com/room/1024` then you'd add it like this:
165+
166+
% heroku config:add HUBOT_CAMPFIRE_ROOMS="42,1024"
167+
168+
Add the subdomain hubot should connect to. If you web URL looks like
169+
`http://mysubdomain.campfirenow.com` then you'd add it like this:
170+
171+
% heroku config:add HUBOT_CAMPFIRE_ACCOUNT="mysubdomain"
172+
173+
[hubot-wiki]: https://github.com/github/hubot/wiki
174+
175+
## Restart the bot
176+
177+
You may want to get comfortable with `heroku logs` and `heroku restart`
178+
if you're having issues.
179+

bin/hubot

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
npm install
4+
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"
5+
6+
exec node_modules/.bin/hubot "$@"
7+

bin/hubot.cmd

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
3+
npm install && node_modules\.bin\hubot.cmd %*

external-scripts.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

hubot-scripts.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["redis-brain.coffee", "tweet.coffee", "shipit.coffee"]

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "hosted-hubot",
3+
"version": "2.5.0",
4+
5+
"author": "GitHub Inc.",
6+
7+
"keywords": [
8+
"github",
9+
"hubot",
10+
"campfire",
11+
"bot"
12+
],
13+
14+
"description": "A simple helpful robot for your Company",
15+
16+
"licenses": [{
17+
"type": "MIT",
18+
"url": "https://github.com/github/hubot/raw/master/LICENSE"
19+
}],
20+
21+
"repository" : {
22+
"type": "git",
23+
"url": "https://github.com/github/hubot.git"
24+
},
25+
26+
"dependencies": {
27+
"hubot": ">= 2.5.0",
28+
"hubot-scripts": ">= 2.4.2"
29+
},
30+
31+
"engines": {
32+
"node": ">= 0.8.x",
33+
"npm": ">= 1.1.x"
34+
}
35+
}

scripts/auth.coffee

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Description:
2+
# Auth allows you to assign roles to users which can be used by other scripts
3+
# to restrict access to Hubot commands
4+
#
5+
# Dependencies:
6+
# None
7+
#
8+
# Configuration:
9+
# HUBOT_AUTH_ADMIN - A comma separate list of user IDs
10+
#
11+
# Commands:
12+
# hubot <user> has <role> role - Assigns a role to a user
13+
# hubot <user> doesn't have <role> role - Removes a role from a user
14+
# hubot what role does <user> have - Find out what roles are assigned to a specific user
15+
# hubot who has admin role - Find out who's an admin and can assign roles
16+
#
17+
# Notes:
18+
# * Call the method: robot.auth.hasRole(msg.envelope.user,'<role>')
19+
# * returns bool true or false
20+
#
21+
# * the 'admin' role can only be assigned through the environment variable
22+
# * roles are all transformed to lower case
23+
#
24+
# * The script assumes that user IDs will be unique on the service end as to
25+
# correctly identify a user. Names were insecure as a user could impersonate
26+
# a user
27+
#
28+
# Author:
29+
# alexwilliamsca, tombell
30+
31+
module.exports = (robot) ->
32+
33+
unless process.env.HUBOT_AUTH_ADMIN?
34+
robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable not set'
35+
36+
if process.env.HUBOT_AUTH_ADMIN?
37+
admins = process.env.HUBOT_AUTH_ADMIN.split ','
38+
else
39+
admins = []
40+
41+
class Auth
42+
hasRole: (user, roles) ->
43+
user = robot.brain.userForId(user.id)
44+
if user? and user.roles?
45+
roles = [roles] if roles typeof String
46+
for role in roles
47+
return true if role in user.roles
48+
return false
49+
50+
robot.auth = new Auth
51+
52+
robot.respond /@?(.+) (has) (["'\w: -_]+) (role)/i, (msg) ->
53+
name = msg.match[1].trim()
54+
newRole = msg.match[3].trim().toLowerCase()
55+
56+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
57+
user = robot.brain.userForName(name)
58+
return msg.reply "#{name} does not exist" unless user?
59+
user.roles or= []
60+
61+
if newRole in user.roles
62+
msg.reply "#{name} already has the '#{newRole}' role."
63+
else
64+
if newRole is 'admin'
65+
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."
66+
else
67+
myRoles = msg.message.user.roles or []
68+
if msg.message.user.id.toString() in admins
69+
user.roles.push(newRole)
70+
msg.reply "Ok, #{name} has the '#{newRole}' role."
71+
72+
robot.respond /@?(.+) (doesn't have|does not have) (["'\w: -_]+) (role)/i, (msg) ->
73+
name = msg.match[1].trim()
74+
newRole = msg.match[3].trim().toLowerCase()
75+
76+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
77+
user = robot.brain.userForName(name)
78+
return msg.reply "#{name} does not exist" unless user?
79+
user.roles or= []
80+
81+
if newRole is 'admin'
82+
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."
83+
else
84+
myRoles = msg.message.user.roles or []
85+
if msg.message.user.id.toString() in admins
86+
user.roles = (role for role in user.roles when role isnt newRole)
87+
msg.reply "Ok, #{name} doesn't have the '#{newRole}' role."
88+
89+
robot.respond /(what role does|what roles does) @?(.+) (have)\?*$/i, (msg) ->
90+
name = msg.match[2].trim()
91+
user = robot.brain.userForId(msg.message.user.id)
92+
return msg.reply "#{name} does not exist" unless user?
93+
user.roles or= []
94+
95+
if user.id.toString() in admins
96+
isAdmin = ' and is also an admin'
97+
else
98+
isAdmin = ''
99+
msg.reply "#{name} has the following roles: #{user.roles.join(', ')}#{isAdmin}."
100+
101+
robot.respond /who has admin role\?*$/i, (msg) ->
102+
adminNames = []
103+
for admin in admins
104+
user = robot.brain.userForId(admin)
105+
adminNames.push user.name if user?
106+
107+
msg.reply "The following people have the 'admin' role: #{adminNames.join(', ')}"

scripts/events.coffee

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Description:
2+
# Event system related utilities
3+
#
4+
# Commands:
5+
# hubot fake event <event> - Triggers the <event> event for debugging reasons
6+
#
7+
# Events:
8+
# debug - {user: <user object to send message to>}
9+
10+
util = require 'util'
11+
12+
module.exports = (robot) ->
13+
14+
robot.respond /FAKE EVENT (.*)/i, (msg) ->
15+
msg.send "fake event '#{msg.match[1]}' triggered"
16+
robot.emit msg.match[1], {user: msg.message.user}
17+
18+
robot.on 'debug', (event) ->
19+
robot.send event.user, util.inspect event

0 commit comments

Comments
 (0)