-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (55 loc) · 1.58 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const Koa = require('koa');
const parser = require('koa-bodyparser');
const request = require('request-promise-native');
if (!process.env.WEBHOOK || !process.env.WEBHOOK.startsWith || !process.env.WEBHOOK.startsWith('https://')) {
console.error('The environment variable WEBHOOK must be the Discord webhook URL');
process.exit(1);
}
const app = new Koa();
app.use(parser({
enableTypes: ['json'],
extendTypes: {
json: ['*/*'],
},
onerror: (err, ctx) => {
ctx.throw(400);
},
}));
app.use(async function(ctx) {
if (ctx.request.body && Array.isArray(ctx.request.body.alerts)) {
const data = {embeds: []};
ctx.request.body.alerts.forEach(alert => {
if (alert.annotations && (alert.annotations.summary || alert.annotations.description)) {
data.embeds.push({
title: '[' + alert.status.toUpperCase() + '] ' + alert.annotations.summary,
description: alert.annotations.description,
color: alert.status === 'resolved' ? 0x00c853 : 0xd50000,
});
}
});
if (data.embeds.length) {
await request({
url: process.env.WEBHOOK,
method: 'POST',
json: data,
}).then(() => {
ctx.status = 200;
console.log(data.embeds.length + ' alerts sent');
}).catch(err => {
ctx.status = 500;
console.error(err);
});
} else {
ctx.status = 400;
}
} else {
ctx.status = 400;
}
});
const port = process.env.PORT || 9094;
app.listen(port, (err) => {
if (err) {
return console.error(err);
}
console.info('listening on port ' + port);
});