forked from sklise/hubot-web
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.coffee
137 lines (104 loc) · 4.05 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Robot = require('hubot').Robot
Adapter = require('hubot').Adapter
TextMessage = require('hubot').TextMessage
request = require('request')
string = require("string")
exec = require('sync-exec')
path = require('path')
port = 8081
class HangoutsAdapter extends Adapter
createUser: (userData, room) ->
user = @robot.brain.userForName userData.fullName
unless user?
id = userData.userId
user = @robot.brain.userForId id
user.name = userData.fullName
user.room = room
user
send: (user, strings...) ->
if strings.length > 0
message = if process.env.HUBOT_HTML_RESPONSE then @toHTML(strings.shift()) else strings.shift()
response = {
fullName:user.user.name,
conversationId:user.user.options.conversationId,
userId:user.user.options.userId,
message:message
}
response.message = response.fullName + ': ' + response.message
options = {
url:"http://localhost:#{port}/proxy/",
method:'POST',
body:response,
json:true
}
request(options)
@send user, strings...
reply: (user, strings...) ->
@send user, strings.map((str) -> "#{str}")...
# @send user, strings.map((str) -> "#{user.user.name}: #{str}")...
run: ->
pythonPath = process.env.HUBOT_HANGUPS_PYTHON
if ((pythonPath?) && (pythonPath.length > 0))
console.log "Python path is " + pythonPath
else
console.log 'ERROR: Set the HUBOT_HANGUPS_PYTHON env variable to your version of python 3.3 (eg python, python3, python3.3, etc'
process.exit(1)
checkInstallScript = path.resolve(__dirname, 'checkInstall.py')
installScript = path.resolve(__dirname, 'setup.py')
console.log "checkInstall script " + pythonPath + " " + checkInstallScript
result = exec(pythonPath + " " + checkInstallScript)
console.log "result " + JSON.stringify(result)
if (result.status > 0)
console.log "ERROR: Some python modules missing." + result.stdout + "Attempting automated install, please wait...."
installCmd = pythonPath + " " + installScript + " install"
result = exec(installCmd)
if (result.status > 0)
console.error "Could not automatically run " + installCmd
process.exit(1)
else
console.log "Install succesful. Starting hubot-hangouts"
self = @
options = {}
hangoutsBotPath = __dirname+'/HangoutsBot/Main.py'
@hangoutsBot = require('child_process').spawn(pythonPath, [hangoutsBotPath])
@hangoutsBot.stdout.pipe(process.stdout,{ end: false })
process.stdin.resume()
process.stdin.pipe(@hangoutsBot.stdin,{ end: false })
@hangoutsBot.stdin.on 'end', ->
process.stdout.write('Hangouts stream ended.')
@hangoutsBot.on 'exit', (code) ->
process.exit(code)
@robot.router.post '/receive/:room', (req, res) ->
req.setEncoding('utf8')
rawData = ''
req.on 'data', (data) ->
rawData += data
req.on 'end', () ->
data = JSON.parse(rawData)
user = self.createUser(data, req.params.room)
if data.conversationId && data.userId
user.options = {
'conversationId' : data.conversationId,
'userId' : data.userId,
'email' : data.email #not populated by hangups currently
}
#console.log "[#{req.params.room}] #{user.name} => #{data.message}"
res.setHeader 'content-type', 'text/html'
self.receive new TextMessage(user, data.message)
res.end 'received'
else
console.log 'Invalid user options'
res.setHeader 'content-type', 'text/html'
res.end 'received'
@hangoutsBot.on 'exit', (code) =>
@robot.logger.error "Lost connection with HangoutsBot... Exiting"
process.nextTick -> process.exit(1)
@hangoutsBot.on "uncaughtException", (err) =>
@robot.logger.error "#{err}"
process.on "uncaughtException", (err) =>
@robot.logger.error "#{err}"
process.on "exit", =>
@hangoutsBot.kill()
self.emit "connected"
exports.use = (robot) ->
new HangoutsAdapter robot