forked from wichon/hubot-json-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
61 lines (49 loc) · 1.78 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
Robot = require('hubot').Robot
Adapter = require('hubot').Adapter
TextMessage = require('hubot').TextMessage
fetch = require('node-fetch')
string = require("string")
# sendmessageURL domain.com/your/path/ + user.channel
sendMessageUrl = process.env.HUBOT_REST_SEND_URL
class WebAdapter extends Adapter
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
data = JSON.stringify({
message: strings.shift(),
from: "#{@robot.name}"
})
# The resulting url will be sendMessageUrl + user.room, so sendMessageUrl must include the slash in the end
url = "#{sendMessageUrl}#{user.room}"
options = {
url: url,
method: 'POST',
body: data,
headers: {
"Content-Type": "application/json"
}
}
fetch url, options # TODO: manage errors
@send user, strings...
reply: (user, strings...) ->
@send user, strings.map((str) -> "#{user.user.name}: #{str}")...
run: ->
self = @
@robot.router.post '/receive/:room', (req, res) ->
user = self.createUser(req.body.from, req.params.room)
console.log "[#{req.params.room}] #{user.name} => #{req.body.message}"
res.setHeader 'content-type', 'application/json'
res.setHeader 'Access-Control-Allow-Origin', '*'
res.setHeader 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'
self.receive new TextMessage(user, req.body.message)
res.end JSON.stringify({status: 'received'})
self.emit "connected"
exports.use = (robot) ->
new WebAdapter robot