forked from hubotio/hubot
/
hipchat.coffee
121 lines (103 loc) · 3.67 KB
/
hipchat.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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.reply_to, 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 || "#{self.name}, I"
password: process.env.HUBOT_HIPCHAT_PASSWORD
rooms: process.env.HUBOT_HIPCHAT_ROOMS || "@All"
console.log "Options:", @options
bot = new Wobot(jid: @options.jid, name: @options.name, password: @options.password)
mention='@'+@options.name.split(' ')[0]
console.log "Bot:", bot
bot.onConnect =>
console.log "Connected to HipChat"
if @options.rooms == "@All"
@get "/v1/rooms/list", (err, response)->
if response
response.rooms.forEach (room)->
console.log "Joining #{room.xmpp_jid}"
bot.join room.xmpp_jid
else
console.log "Can't list rooms: #{err}"
else
@options.rooms.split(',').forEach (room_id)->
console.log "Joining #{room_id}"
bot.join room_id
@get "/v1/users/list", (err, response)->
if response
response.users.forEach (user)->
self.userForId user.user_id, user
else
console.log "Can't list rooms: #{err}"
bot.onError (message, stanza)->
console.log "Received error from HipChat:", message, stanza
bot.onMessage RegExp(mention,'i'), (channel, from, message)->
#console.log "#{from}@#{channel}: #{message}"
author = { name: from, reply_to: channel }
hubot_msg=message.replace(RegExp(mention,'i'),"#{self.name}: ")
self.receive new Robot.Message(author, hubot_msg)
bot.onPrivateMessage (from, message)=>
user = self.userForId(from.match(/_(\d+)@/)[1])
author = { name: user.name, reply_to: from }
self.receive new Robot.Message(author, "#{self.name}: #{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