forked from paullessing/demo-env-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.js
46 lines (40 loc) · 1 KB
/
slack.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
43
44
45
46
const https = require('https');
const config = require('./config/config');
exports.post = function post(text) {
const data = JSON.stringify({
channel: config.channel,
username: config.username,
text: text
});
const options = {
host: 'hooks.slack.com',
port: 443,
path: config.slackUrl,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
console.log('Making request', options);
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let response = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
response += chunk.toString();
});
res.on('end', function () {
console.log('Response');
console.log(response);
resolve();
});
});
req.on('error', (error) => {
console.error(error);
reject(error);
});
req.write(data);
req.end();
});
};