-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackApi.js
37 lines (30 loc) · 1.19 KB
/
slackApi.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
const request = require('request');
const messageTemplate = require('./message.json');
class SlackAPI {
constructor({ token }) {
this.url = `https://hooks.slack.com/services/${token}`
}
postMessage({ serviceName, version, previous, githubUrl, friendlyName }) {
const replace = message => message
.replace(/\${friendlyName}/, (friendlyName || serviceName))
.replace(/\${version}/, version)
.replace(/\${previous}/, (previous || 'N/A'))
.replace(/\${githubUrl}/, githubUrl);
const message = {
...messageTemplate,
attachments: messageTemplate.attachments.map(attachment => ({
...attachment,
fallback: replace(attachment.fallback),
title: replace(attachment.title),
title_link: replace(attachment.title_link),
fields: attachment.fields.map(field => ({
...field,
value: replace(field.value),
})),
ts: parseInt(Date.now() / 1000),
})),
};
request({ url: this.url, method: 'POST', json: message });
}
}
module.exports = SlackAPI;