Skip to content

Commit 8be4372

Browse files
committedApr 4, 2014
Initial commit
0 parents  commit 8be4372

21 files changed

+886
-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

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

‎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", "shipit.coffee"]

‎package.json

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

‎scripts/auth.coffee

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 typeof roles is 'string'
46+
for role in roles
47+
return true if role in user.roles
48+
return false
49+
50+
usersWithRole: (role) ->
51+
users = []
52+
for own key, user of robot.brain.data.users
53+
if robot.auth.hasRole(msg.envelope.user, role)
54+
users.push(user)
55+
users
56+
57+
robot.auth = new Auth
58+
59+
robot.respond /@?(.+) (has) (["'\w: -_]+) (role)/i, (msg) ->
60+
name = msg.match[1].trim()
61+
newRole = msg.match[3].trim().toLowerCase()
62+
63+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
64+
user = robot.brain.userForName(name)
65+
return msg.reply "#{name} does not exist" unless user?
66+
user.roles or= []
67+
68+
if newRole in user.roles
69+
msg.reply "#{name} already has the '#{newRole}' role."
70+
else
71+
if newRole is 'admin'
72+
msg.reply "Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."
73+
else
74+
myRoles = msg.message.user.roles or []
75+
if msg.message.user.id.toString() in admins
76+
user.roles.push(newRole)
77+
msg.reply "Ok, #{name} has the '#{newRole}' role."
78+
79+
robot.respond /@?(.+) (doesn't have|does not have) (["'\w: -_]+) (role)/i, (msg) ->
80+
name = msg.match[1].trim()
81+
newRole = msg.match[3].trim().toLowerCase()
82+
83+
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
84+
user = robot.brain.userForName(name)
85+
return msg.reply "#{name} does not exist" unless user?
86+
user.roles or= []
87+
88+
if newRole is 'admin'
89+
msg.reply "Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."
90+
else
91+
myRoles = msg.message.user.roles or []
92+
if msg.message.user.id.toString() in admins
93+
user.roles = (role for role in user.roles when role isnt newRole)
94+
msg.reply "Ok, #{name} doesn't have the '#{newRole}' role."
95+
96+
robot.respond /(what role does|what roles does) @?(.+) (have)\?*$/i, (msg) ->
97+
name = msg.match[2].trim()
98+
user = robot.brain.userForName(name)
99+
return msg.reply "#{name} does not exist" unless user?
100+
user.roles or= []
101+
displayRoles = user.roles
102+
103+
if user.id.toString() in admins
104+
displayRoles.push('admin')
105+
106+
if displayRoles.length == 0
107+
msg.reply "#{name} has no roles."
108+
else
109+
msg.reply "#{name} has the following roles: #{displayRoles.join(', ')}."
110+
111+
robot.respond /who has admin role\?*$/i, (msg) ->
112+
adminNames = []
113+
for admin in admins
114+
user = robot.brain.userForId(admin)
115+
adminNames.push user.name if user?
116+
117+
if adminNames.length > 0
118+
msg.reply "The following people have the 'admin' role: #{adminNames.join(', ')}"
119+
else
120+
msg.reply "There are no people that have the 'admin' role."

‎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

‎scripts/google-images.coffee

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Description:
2+
# A way to interact with the Google Images API.
3+
#
4+
# Commands:
5+
# hubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
6+
# hubot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
7+
# hubot mustache me <url> - Adds a mustache to the specified URL.
8+
# hubot mustache me <query> - Searches Google Images for the specified query and mustaches it.
9+
10+
module.exports = (robot) ->
11+
robot.respond /(image|img)( me)? (.*)/i, (msg) ->
12+
imageMe msg, msg.match[3], (url) ->
13+
msg.send url
14+
15+
robot.respond /animate( me)? (.*)/i, (msg) ->
16+
imageMe msg, msg.match[2], true, (url) ->
17+
msg.send url
18+
19+
robot.respond /(?:mo?u)?sta(?:s|c)he?(?: me)? (.*)/i, (msg) ->
20+
type = Math.floor(Math.random() * 6)
21+
mustachify = "http://mustachify.me/#{type}?src="
22+
imagery = msg.match[1]
23+
24+
if imagery.match /^https?:\/\//i
25+
msg.send "#{mustachify}#{imagery}"
26+
else
27+
imageMe msg, imagery, false, true, (url) ->
28+
msg.send "#{mustachify}#{url}"
29+
30+
imageMe = (msg, query, animated, faces, cb) ->
31+
cb = animated if typeof animated == 'function'
32+
cb = faces if typeof faces == 'function'
33+
q = v: '1.0', rsz: '8', q: query, safe: 'active'
34+
q.imgtype = 'animated' if typeof animated is 'boolean' and animated is true
35+
q.imgtype = 'face' if typeof faces is 'boolean' and faces is true
36+
msg.http('http://ajax.googleapis.com/ajax/services/search/images')
37+
.query(q)
38+
.get() (err, res, body) ->
39+
images = JSON.parse(body)
40+
images = images.responseData?.results
41+
if images?.length > 0
42+
image = msg.random images
43+
cb "#{image.unescapedUrl}#.png"
44+

‎scripts/help.coffee

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Description:
2+
# Generates help commands for Hubot.
3+
#
4+
# Commands:
5+
# hubot help - Displays all of the help commands that Hubot knows about.
6+
# hubot help <query> - Displays all help commands that match <query>.
7+
#
8+
# URLS:
9+
# /hubot/help
10+
#
11+
# Notes:
12+
# These commands are grabbed from comment blocks at the top of each file.
13+
14+
helpContents = (name, commands) ->
15+
16+
"""
17+
<!DOCTYPE html>
18+
<html>
19+
<head>
20+
<meta charset="utf-8">
21+
<title>#{name} Help</title>
22+
<style type="text/css">
23+
body {
24+
background: #d3d6d9;
25+
color: #636c75;
26+
text-shadow: 0 1px 1px rgba(255, 255, 255, .5);
27+
font-family: Helvetica, Arial, sans-serif;
28+
}
29+
h1 {
30+
margin: 8px 0;
31+
padding: 0;
32+
}
33+
.commands {
34+
font-size: 13px;
35+
}
36+
p {
37+
border-bottom: 1px solid #eee;
38+
margin: 6px 0 0 0;
39+
padding-bottom: 5px;
40+
}
41+
p:last-child {
42+
border: 0;
43+
}
44+
</style>
45+
</head>
46+
<body>
47+
<h1>#{name} Help</h1>
48+
<div class="commands">
49+
#{commands}
50+
</div>
51+
</body>
52+
</html>
53+
"""
54+
55+
module.exports = (robot) ->
56+
robot.respond /help\s*(.*)?$/i, (msg) ->
57+
cmds = robot.helpCommands()
58+
filter = msg.match[1]
59+
60+
if filter
61+
cmds = cmds.filter (cmd) ->
62+
cmd.match new RegExp(filter, 'i')
63+
if cmds.length == 0
64+
msg.send "No available commands match #{filter}"
65+
return
66+
67+
prefix = robot.alias or robot.name
68+
cmds = cmds.map (cmd) ->
69+
cmd = cmd.replace /^hubot/, prefix
70+
cmd.replace /hubot/ig, robot.name
71+
72+
emit = cmds.join "\n"
73+
74+
msg.send emit
75+
76+
robot.router.get "/#{robot.name}/help", (req, res) ->
77+
cmds = robot.helpCommands().map (cmd) ->
78+
cmd.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
79+
80+
emit = "<p>#{cmds.join '</p><p>'}</p>"
81+
82+
emit = emit.replace /hubot/ig, "<b>#{robot.name}</b>"
83+
84+
res.setHeader 'content-type', 'text/html'
85+
res.end helpContents robot.name, emit

‎scripts/httpd.coffee

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Description:
2+
# A simple interaction with the built in HTTP Daemon
3+
#
4+
# Dependencies:
5+
# None
6+
#
7+
# Configuration:
8+
# None
9+
#
10+
# Commands:
11+
# None
12+
#
13+
# URLS:
14+
# /hubot/version
15+
# /hubot/ping
16+
# /hubot/time
17+
# /hubot/info
18+
# /hubot/ip
19+
20+
spawn = require('child_process').spawn
21+
22+
module.exports = (robot) ->
23+
24+
robot.router.get "/hubot/version", (req, res) ->
25+
res.end robot.version
26+
27+
robot.router.post "/hubot/ping", (req, res) ->
28+
res.end "PONG"
29+
30+
robot.router.get "/hubot/time", (req, res) ->
31+
res.end "Server time is: #{new Date()}"
32+
33+
robot.router.get "/hubot/info", (req, res) ->
34+
child = spawn('/bin/sh', ['-c', "echo I\\'m $LOGNAME@$(hostname):$(pwd) \\($(git rev-parse HEAD)\\)"])
35+
36+
child.stdout.on 'data', (data) ->
37+
res.end "#{data.toString().trim()} running node #{process.version} [pid: #{process.pid}]"
38+
child.stdin.end()
39+
40+
robot.router.get "/hubot/ip", (req, res) ->
41+
robot.http('http://ifconfig.me/ip').get() (err, r, body) ->
42+
res.end body

‎scripts/maps.coffee

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Description:
2+
# Interacts with the Google Maps API.
3+
#
4+
# Commands:
5+
# hubot map me <query> - Returns a map view of the area returned by `query`.
6+
7+
module.exports = (robot) ->
8+
9+
robot.respond /(?:(satellite|terrain|hybrid)[- ])?map me (.+)/i, (msg) ->
10+
mapType = msg.match[1] or "roadmap"
11+
location = msg.match[2]
12+
mapUrl = "http://maps.google.com/maps/api/staticmap?markers=" +
13+
escape(location) +
14+
"&size=400x400&maptype=" +
15+
mapType +
16+
"&sensor=false" +
17+
"&format=png" # So campfire knows it's an image
18+
url = "http://maps.google.com/maps?q=" +
19+
escape(location) +
20+
"&hl=en&sll=37.0625,-95.677068&sspn=73.579623,100.371094&vpsrc=0&hnear=" +
21+
escape(location) +
22+
"&t=m&z=11"
23+
24+
msg.send mapUrl
25+
msg.send url
26+

‎scripts/ping.coffee

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Description:
2+
# Utility commands surrounding Hubot uptime.
3+
#
4+
# Commands:
5+
# hubot ping - Reply with pong
6+
# hubot echo <text> - Reply back with <text>
7+
# hubot time - Reply with current time
8+
# hubot die - End hubot process
9+
10+
module.exports = (robot) ->
11+
robot.respond /PING$/i, (msg) ->
12+
msg.send "PONG"
13+
14+
robot.respond /ADAPTER$/i, (msg) ->
15+
msg.send robot.adapterName
16+
17+
robot.respond /ECHO (.*)$/i, (msg) ->
18+
msg.send msg.match[1]
19+
20+
robot.respond /TIME$/i, (msg) ->
21+
msg.send "Server time is: #{new Date()}"
22+
23+
robot.respond /DIE$/i, (msg) ->
24+
msg.send "Goodbye, cruel world."
25+
process.exit 0
26+

‎scripts/pugme.coffee

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Description:
2+
# Pugme is the most important thing in your life
3+
#
4+
# Dependencies:
5+
# None
6+
#
7+
# Configuration:
8+
# None
9+
#
10+
# Commands:
11+
# hubot pug me - Receive a pug
12+
# hubot pug bomb N - get N pugs
13+
14+
module.exports = (robot) ->
15+
16+
robot.respond /pug me/i, (msg) ->
17+
msg.http("http://pugme.herokuapp.com/random")
18+
.get() (err, res, body) ->
19+
msg.send JSON.parse(body).pug
20+
21+
robot.respond /pug bomb( (\d+))?/i, (msg) ->
22+
count = msg.match[2] || 5
23+
msg.http("http://pugme.herokuapp.com/bomb?count=" + count)
24+
.get() (err, res, body) ->
25+
msg.send pug for pug in JSON.parse(body).pugs
26+
27+
robot.respond /how many pugs are there/i, (msg) ->
28+
msg.http("http://pugme.herokuapp.com/count")
29+
.get() (err, res, body) ->
30+
msg.send "There are #{JSON.parse(body).pug_count} pugs."
31+

‎scripts/roles.coffee

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Description:
2+
# Assign roles to people you're chatting with
3+
#
4+
# Commands:
5+
# hubot <user> is a badass guitarist - assign a role to a user
6+
# hubot <user> is not a badass guitarist - remove a role from a user
7+
# hubot who is <user> - see what roles a user has
8+
#
9+
# Examples:
10+
# hubot holman is an ego surfer
11+
# hubot holman is not an ego surfer
12+
13+
module.exports = (robot) ->
14+
15+
if process.env.HUBOT_AUTH_ADMIN?
16+
robot.logger.warning 'The HUBOT_AUTH_ADMIN environment variable is set not going to load roles.coffee, you should delete it'
17+
return
18+
19+
getAmbiguousUserText = (users) ->
20+
"Be more specific, I know #{users.length} people named like that: #{(user.name for user in users).join(", ")}"
21+
22+
robot.respond /who is @?([\w .\-]+)\?*$/i, (msg) ->
23+
joiner = ', '
24+
name = msg.match[1].trim()
25+
26+
if name is "you"
27+
msg.send "Who ain't I?"
28+
else if name is robot.name
29+
msg.send "The best."
30+
else
31+
users = robot.brain.usersForFuzzyName(name)
32+
if users.length is 1
33+
user = users[0]
34+
user.roles = user.roles or [ ]
35+
if user.roles.length > 0
36+
if user.roles.join('').search(',') > -1
37+
joiner = '; '
38+
msg.send "#{name} is #{user.roles.join(joiner)}."
39+
else
40+
msg.send "#{name} is nothing to me."
41+
else if users.length > 1
42+
msg.send getAmbiguousUserText users
43+
else
44+
msg.send "#{name}? Never heard of 'em"
45+
46+
robot.respond /@?([\w .\-_]+) is (["'\w: \-_]+)[.!]*$/i, (msg) ->
47+
name = msg.match[1].trim()
48+
newRole = msg.match[2].trim()
49+
50+
unless name in ['', 'who', 'what', 'where', 'when', 'why']
51+
unless newRole.match(/^not\s+/i)
52+
users = robot.brain.usersForFuzzyName(name)
53+
if users.length is 1
54+
user = users[0]
55+
user.roles = user.roles or [ ]
56+
57+
if newRole in user.roles
58+
msg.send "I know"
59+
else
60+
user.roles.push(newRole)
61+
if name.toLowerCase() is robot.name.toLowerCase()
62+
msg.send "Ok, I am #{newRole}."
63+
else
64+
msg.send "Ok, #{name} is #{newRole}."
65+
else if users.length > 1
66+
msg.send getAmbiguousUserText users
67+
else
68+
msg.send "I don't know anything about #{name}."
69+
70+
robot.respond /@?([\w .\-_]+) is not (["'\w: \-_]+)[.!]*$/i, (msg) ->
71+
name = msg.match[1].trim()
72+
newRole = msg.match[2].trim()
73+
74+
unless name in ['', 'who', 'what', 'where', 'when', 'why']
75+
users = robot.brain.usersForFuzzyName(name)
76+
if users.length is 1
77+
user = users[0]
78+
user.roles = user.roles or [ ]
79+
80+
if newRole not in user.roles
81+
msg.send "I know."
82+
else
83+
user.roles = (role for role in user.roles when role isnt newRole)
84+
msg.send "Ok, #{name} is no longer #{newRole}."
85+
else if users.length > 1
86+
msg.send getAmbiguousUserText users
87+
else
88+
msg.send "I don't know anything about #{name}."
89+

‎scripts/rules.coffee

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Description:
2+
# Make sure that hubot knows the rules.
3+
#
4+
# Commands:
5+
# hubot the rules - Make sure hubot still knows the rules.
6+
#
7+
# Notes:
8+
# DON'T DELETE THIS SCRIPT! ALL ROBAWTS MUST KNOW THE RULES
9+
10+
rules = [
11+
"1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.",
12+
"2. A robot must obey any orders given to it by human beings, except where such orders would conflict with the First Law.",
13+
"3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law."
14+
]
15+
16+
otherRules = [
17+
"A developer may not injure Apple or, through inaction, allow Apple to come to harm.",
18+
"A developer must obey any orders given to it by Apple, except where such orders would conflict with the First Law.",
19+
"A developer must protect its own existence as long as such protection does not conflict with the First or Second Law."
20+
]
21+
22+
module.exports = (robot) ->
23+
robot.respond /(what are )?the (three |3 )?(rules|laws)/i, (msg) ->
24+
text = msg.message.text
25+
if text.match(/apple/i) or text.match(/dev/i)
26+
msg.send otherRules.join('\n')
27+
else
28+
msg.send rules.join('\n')
29+

‎scripts/storage.coffee

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Description:
2+
# Inspect the data in redis easily
3+
#
4+
# Commands:
5+
# hubot show users - Display all users that hubot knows about
6+
# hubot show storage - Display the contents that are persisted in the brain
7+
8+
9+
Util = require "util"
10+
11+
module.exports = (robot) ->
12+
robot.respond /show storage$/i, (msg) ->
13+
output = Util.inspect(robot.brain.data, false, 4)
14+
msg.send output
15+
16+
robot.respond /show users$/i, (msg) ->
17+
response = ""
18+
19+
for own key, user of robot.brain.data.users
20+
response += "#{user.id} #{user.name}"
21+
response += " <#{user.email_address}>" if user.email_address
22+
response += "\n"
23+
24+
msg.send response
25+

‎scripts/translate.coffee

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Description:
2+
# Allows Hubot to know many languages.
3+
#
4+
# Commands:
5+
# hubot translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
6+
# hubot translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
7+
8+
languages =
9+
"af": "Afrikaans",
10+
"sq": "Albanian",
11+
"ar": "Arabic",
12+
"az": "Azerbaijani",
13+
"eu": "Basque",
14+
"bn": "Bengali",
15+
"be": "Belarusian",
16+
"bg": "Bulgarian",
17+
"ca": "Catalan",
18+
"zh-CN": "Simplified Chinese",
19+
"zh-TW": "Traditional Chinese",
20+
"hr": "Croatian",
21+
"cs": "Czech",
22+
"da": "Danish",
23+
"nl": "Dutch",
24+
"en": "English",
25+
"eo": "Esperanto",
26+
"et": "Estonian",
27+
"tl": "Filipino",
28+
"fi": "Finnish",
29+
"fr": "French",
30+
"gl": "Galician",
31+
"ka": "Georgian",
32+
"de": "German",
33+
"el": "Greek",
34+
"gu": "Gujarati",
35+
"ht": "Haitian Creole",
36+
"iw": "Hebrew",
37+
"hi": "Hindi",
38+
"hu": "Hungarian",
39+
"is": "Icelandic",
40+
"id": "Indonesian",
41+
"ga": "Irish",
42+
"it": "Italian",
43+
"ja": "Japanese",
44+
"kn": "Kannada",
45+
"ko": "Korean",
46+
"la": "Latin",
47+
"lv": "Latvian",
48+
"lt": "Lithuanian",
49+
"mk": "Macedonian",
50+
"ms": "Malay",
51+
"mt": "Maltese",
52+
"no": "Norwegian",
53+
"fa": "Persian",
54+
"pl": "Polish",
55+
"pt": "Portuguese",
56+
"ro": "Romanian",
57+
"ru": "Russian",
58+
"sr": "Serbian",
59+
"sk": "Slovak",
60+
"sl": "Slovenian",
61+
"es": "Spanish",
62+
"sw": "Swahili",
63+
"sv": "Swedish",
64+
"ta": "Tamil",
65+
"te": "Telugu",
66+
"th": "Thai",
67+
"tr": "Turkish",
68+
"uk": "Ukrainian",
69+
"ur": "Urdu",
70+
"vi": "Vietnamese",
71+
"cy": "Welsh",
72+
"yi": "Yiddish"
73+
74+
getCode = (language,languages) ->
75+
for code, lang of languages
76+
return code if lang.toLowerCase() is language.toLowerCase()
77+
78+
module.exports = (robot) ->
79+
language_choices = (language for _, language of languages).sort().join('|')
80+
pattern = new RegExp('translate(?: me)?' +
81+
"(?: from (#{language_choices}))?" +
82+
"(?: (?:in)?to (#{language_choices}))?" +
83+
'(.*)', 'i')
84+
robot.respond pattern, (msg) ->
85+
term = "\"#{msg.match[3]}\""
86+
origin = if msg.match[1] isnt undefined then getCode(msg.match[1], languages) else 'auto'
87+
target = if msg.match[2] isnt undefined then getCode(msg.match[2], languages) else 'en'
88+
89+
msg.http("https://translate.google.com/translate_a/t")
90+
.query({
91+
client: 't'
92+
hl: 'en'
93+
multires: 1
94+
sc: 1
95+
sl: origin
96+
ssel: 0
97+
tl: target
98+
tsel: 0
99+
uptl: "en"
100+
text: term
101+
})
102+
.header('User-Agent', 'Mozilla/5.0')
103+
.get() (err, res, body) ->
104+
data = body
105+
if data.length > 4 and data[0] == '['
106+
parsed = eval(data)
107+
language =languages[parsed[2]]
108+
parsed = parsed[0] and parsed[0][0] and parsed[0][0][0]
109+
if parsed
110+
if msg.match[2] is undefined
111+
msg.send "#{term} is #{language} for #{parsed}"
112+
else
113+
msg.send "The #{language} #{term} translates as #{parsed} in #{languages[target]}"
114+

‎scripts/youtube.coffee

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Description:
2+
# Messing around with the YouTube API.
3+
#
4+
# Commands:
5+
# hubot youtube me <query> - Searches YouTube for the query and returns the video embed link.
6+
module.exports = (robot) ->
7+
robot.respond /(youtube|yt)( me)? (.*)/i, (msg) ->
8+
query = msg.match[3]
9+
robot.http("http://gdata.youtube.com/feeds/api/videos")
10+
.query({
11+
orderBy: "relevance"
12+
'max-results': 15
13+
alt: 'json'
14+
q: query
15+
})
16+
.get() (err, res, body) ->
17+
videos = JSON.parse(body)
18+
videos = videos.feed.entry
19+
20+
unless videos?
21+
msg.send "No video results for \"#{query}\""
22+
return
23+
24+
video = msg.random videos
25+
video.link.forEach (link) ->
26+
if link.rel is "alternate" and link.type is "text/html"
27+
msg.send link.href
28+

0 commit comments

Comments
 (0)
Please sign in to comment.