Skip to content

Commit

Permalink
Merge pull request hubotio#34 from assaf/master
Browse files Browse the repository at this point in the history
HipChat Adapter
  • Loading branch information
atmos committed Oct 26, 2011
2 parents 2cd7331 + b7b735b commit ff2ca0e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bin/hubot
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ else
Hubot = require("hubot/irc").IrcBot
when "campfire"
Hubot = require("hubot/campfire").Campfire
when "hipchat"
Hubot = require("hubot/hipchat").HipChat
when "twilio"
Hubot = require("hubot/twilio").Twilio
else
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"scoped-http-client": "0.9.4",
"irc": "0.2",
"hiredis": "0.1.12",
"redis": "0.6.7"
"redis": "0.6.7",
"wobot": "0.3.0"
},

"directories": {
Expand Down
102 changes: 102 additions & 0 deletions src/hubot/hipchat.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
Robot = require "robot"
HTTPS = require "https"
EventEmitter = require("events").EventEmitter
Wobot = require("wobot").Bot

class HipChat extends Robot
send: (user, strings...) ->
console.log "Sending"
strings.forEach (str) =>
@bot.message user.room || user.id, str

reply: (user, strings...) ->
console.log "Replying"
strings.forEach (str) =>
@send user, "#{user.name}: #{str}"

run: ->
self = @
@options =
token: process.env.HUBOT_HIPCHAT_TOKEN
jid: process.env.HUBOT_HIPCHAT_JID
name: process.env.HUBOT_HIPCHAT_NAME || "Hubot, I"
password: process.env.HUBOT_HIPCHAT_PASSWORD

console.log "Options:", @options
bot = new Wobot(jid: @options.jid, name: @options.name, password: @options.password)
console.log "Bot:", bot

bot.onConnect =>
console.log "Connected to HipChat"
@get "/v1/rooms/list", (err, response)->
response.rooms.forEach (room)->
bot.join room.xmpp_jid
bot.onError (message, stanza)->
console.log "Received error from HipChat:", message, stanza
bot.onMessage /^\s*@hubot\s/i, (channel, from, message)->
author = self.userForId(from)
author.room = channel
self.receive new Robot.Message(author, message.replace(/^\s*@hubot\s+/, "Hubot: "))
bot.onPrivateMessage (from, message)=>
author = self.userForId(from)
self.receive new Robot.Message(author, "Hubot: #{message}")
bot.connect()

@bot = bot


# Convenience HTTP Methods for posting on behalf of the token"d user
get: (path, callback) ->
@request "GET", path, null, callback

post: (path, body, callback) ->
@request "POST", path, body, callback

request: (method, path, body, callback) ->
console.log method, path, body
headers = { "Host": "api.hipchat.com" }

options =
"agent" : false
"host" : "api.hipchat.com"
"port" : 443
"path" : path
"method" : method
"headers": headers

if method == "POST"
body.auth_token = @options.token
body = JSON.stringify(body)
headers["Content-Type"] = "application/json"

body = new Buffer(body)
options.headers["Content-Length"] = body.length
else
options.path += "?auth_token=#{@options.token}"

request = HTTPS.request options, (response) ->
data = ""
response.on "data", (chunk) ->
data += chunk
response.on "end", ->
if response.statusCode >= 400
console.log "hipchat error: #{response.statusCode}"

try
callback null, JSON.parse(data)
catch err
callback null, data || { }
response.on "error", (err) ->
callback err, { }

if method == "POST"
request.end(body, 'binary')
else
request.end()

request.on "error", (err) ->
console.log err
console.log err.stack
callback err

exports.HipChat = HipChat
1 change: 1 addition & 0 deletions src/hubot/scripts/help.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
module.exports = (robot) ->
robot.respond /help$/i, (msg) ->
msg.send robot.helpCommands().join("\n")

0 comments on commit ff2ca0e

Please sign in to comment.