forked from phillipuniverse/gitter-to-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitter-to-slack.js
46 lines (41 loc) · 1.35 KB
/
gitter-to-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
var https = require('https');
var request = require('request');
var moment = require('moment');
var roomId = process.env.GITTER_ROOM_ID;
var token = process.env.GITTER_TOKEN;
var gitterRoomSlug = process.env.GITTER_ROOM_SLUG;
var slackHookUrl = process.env.SLACK_HOOK_URL;
var heartbeat = " \n";
var options = {
hostname: 'stream.gitter.im',
port: 443,
path: '/v1/rooms/' + roomId + '/chatMessages',
method: 'GET',
headers: {'Authorization': 'Bearer ' + token}
};
var req = https.request(options, function(res) {
res.on('data', function(chunk) {
var msg = chunk.toString();
if (msg !== heartbeat) {
console.log('Received Gitter payload: ' + msg + ' forwarding to Slack');
var gitterData = JSON.parse(msg);
var slackMessage = '<https://gitter.im/' + gitterRoomSlug + '?at=' + gitterData.id;
slackMessage += '|' + gitterData.fromUser.displayName;
var sentDate = moment(gitterData.sent).format('MMM-D h:mm A')
slackMessage += ' [' + sentDate + ']>';
slackMessage += ': ' + gitterData.text;
request.post(slackHookUrl,
{ json: { text: slackMessage} },
function (err, resp, body) {
if (err) {
console.log(err);
}
}
)
}
});
});
req.on('error', function(e) {
console.log('Something went wrong: ' + e.message);
});
req.end();