-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (35 loc) · 1.39 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
const restify = require('restify');
const discord = require('discord.js');
const bot = new discord.Client({ autoReconnect: true });
const token = process.env.TOKEN;
const secret = process.env.SECRET;
const channelName = process.env.CHANNEL || 'general';
const port = process.env.PORT || 3333;
const jenkinsHost = process.env.JENKINS_URL || 'http://localhost'
const server = restify.createServer({ name: 'Discord Jenkins Notify' });
server.pre(restify.CORS());
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.listen(port, () => {
console.log(`${server.name} Listening at ${server.url}`);
bot.login(token)
.then(console.log('Bot Logged in.'))
.catch(error => console.log(error));
});
server.post('/jenkins/:secret', (req, res) => {
const job = req.body.name;
const build = req.body.build.number;
const reqsecret = req.params.secret;
if (reqsecret !== secret) {
return res.send(401, 'Unauthorized');
}
// Get all channels where
const channels = bot.channels;
channels.forEach(channel => {
const msg = `The build ${job} #${build} - **${req.body.build.status}**\n${jenkinsHost}/${req.body.build.url}\n++++++++++++++++++++++++++++++++++++++++++++`;
if (channel.type === 'text' && channel.name === channelName) {
channel.sendMessage(msg);
}
});
return res.send('message delivered');
});