forked from sklise/hubot-web
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.coffee
74 lines (53 loc) · 1.74 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
Robot = require('hubot').Robot
Adapter = require('hubot').Adapter
TextMessage = require('hubot').TextMessage
request = require('request')
string = require("string")
# sendmessageURL domain.com/messages/new/channel/ + user.channel
sendMessageUrl = process.env.HUBOT_REST_SEND_URL
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
class WebAdapter extends Adapter
toHTML: (message) ->
# message = string(message).escapeHTML().s
message.replace(/\n/g, "<br>")
createUser: (username, room) ->
user = @userForName username
unless user?
id = new Date().getTime().toString()
user = @userForId id
user.name = username
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()
console.log "[#{user.room}] => #{message}"
response = {
message:message
}
options = {
url: sendMessageUrl + user.room,
method:'POST',
body:response,
json:true
}
request(options)
@send user, strings...
reply: (user, strings...) ->
@send user, strings.map((str) -> "#{user.user}: #{str}")...
run: ->
self = @
options = {}
@robot.router.post '/receive/:room', (req, res) ->
user = self.createUser(req.body.from, req.params.room)
if req.body.options
user.options = JSON.parse(req.body.options)
else
user.options = {}
console.log "[#{req.params.room}] #{user.name} => #{req.body.message}"
res.setHeader 'content-type', 'text/html'
self.receive new TextMessage(user, req.body.message)
res.end 'received'
self.emit "connected"
exports.use = (robot) ->
new WebAdapter robot