-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.coffee
62 lines (45 loc) · 1.58 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
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
class WebAdapter extends Adapter
toHTML: (message) ->
# message = string(message).escapeHTML().s
message.replace(/\n/g, "<br>")
createUser: (username, room) ->
user = @robot.brain.userForName username
unless user?
id = new Date().getTime().toString()
user = @robot.brain.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()
request.post(sendMessageUrl+user.room).form({
message: message,
from: "#{@robot.name}"
})
@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