-
Notifications
You must be signed in to change notification settings - Fork 3
/
slack.js
90 lines (76 loc) · 2.88 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
const { WebClient } = require('@slack/web-api');
const logger = require('screwdriver-logger');
const webClients = {};
/**
* Parse slack config
* @method parseSlackConfig
* @param {Object} config slack config
* @param {String} config.defaultWorkspace default slack workspace
* @param {Object} config.workspaces slack workspaces config
* @param {String} channelConfig slack channel name (ex. channel1, workspace1:channel2)
* @return {Object}
*/
function parseSlackConfig(config, channelConfig) {
let workspace = config.defaultWorkspace;
let channel = channelConfig;
if (channel.includes(':')) {
[workspace, channel] = channel.split(':');
}
const workspaceConfig = config.workspaces[workspace];
if (!workspaceConfig) {
logger.error(`Cannot find Slack token for ${workspace}.`);
return {};
}
return { workspace, channel, token: workspaceConfig.token };
}
/**
* Post message to a specific channel
* @method postMessage
* @param {String} channel name of the channel
* @param {Object} web slack web client
* @param {Object} payload payload of the slack message
* @return {Promise}
*/
function postMessage(channel, web, payload) {
// Can post to channel name directly https://api.slack.com/methods/chat.postMessage#channels
return web.chat
.postMessage({
channel,
text: payload.message,
as_user: true,
attachments: payload.attachments
})
.catch(err => logger.error(`postMessage: failed to notify Slack channel ${channel}: ${err.message}`));
}
/**
* Sends slack message to slack channels
* @param {Object} config slack config
* @param {String} config.defaultWorkspace default slack workspace
* @param {Object} config.workspaces slack workspaces config
* @param {String[]} channels slack channel names
* @param {Object} payload slack message payload
* @return {Promise}
*/
function slacker(config, channels, payload) {
return Promise.all(
channels.map(channelConfig => {
const { workspace, channel, token } = parseSlackConfig(config, channelConfig);
if (!workspace || !token) {
return Promise.resolve();
}
if (!webClients[workspace]) {
webClients[workspace] = new WebClient(token, {
retryConfig: {
retries: 5,
factor: 3.86,
maxRetryTime: 30 * 60 * 1000 // Set maximum time to 30 min that the retried operation is allowed to run
}
});
}
const web = webClients[workspace];
return postMessage(channel, web, payload);
})
);
}
module.exports = slacker;