-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (33 loc) · 1.08 KB
/
server.js
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
'use strict'
import concat from 'concat-stream'
import edgarFacts from 'edgar-facts'
import { createServer } from 'http'
import { request } from 'https'
import { parse as queryStringParse } from 'querystring'
import { parse as urlParse } from 'url'
import xtend from 'xtend'
const tokensToUrl = JSON.parse(process.env.TOKENS_URLS)
const handleError = res => err => {
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end(err.message || err)
}
export default createServer((req, res) => {
const errorHandler = handleError(res)
req.pipe(concat(body => {
const parsed = queryStringParse(body.toString())
const token = parsed.token
const channel = `#${parsed.channel_name}`
const url = tokensToUrl[token]
const slackReq = request(
xtend(urlParse(url), { method: 'POST' }),
slackRes => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end()
}
)
slackReq.on('error', errorHandler)
slackReq.write(JSON.stringify({ channel: channel, text: edgarFacts() }))
slackReq.end()
}))
req.on('error', errorHandler)
})