forked from AlexanderWert/action-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.js
51 lines (40 loc) · 1.06 KB
/
message.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
/**
@description
Message payload to be sent with Slack webhook
*/
const { selectAvatar, getMessage, parsePayload } = require("./handlers");
const {
SLACK_USERNAME,
SLACK_CHANNEL,
SLACK_CUSTOM_PAYLOAD,
SLACK_UNFURL_LINKS,
} = process.env;
const messageSingleton = (() => {
let instance;
function createInstance() {
if (SLACK_CUSTOM_PAYLOAD) return parsePayload();
const message = {};
message.text = getMessage(); // Args || DEFAULT_MESSAGE
if(SLACK_UNFURL_LINKS === 'true'){
message.unfurl_links = true;
}
// override username
if (SLACK_USERNAME) message.username = SLACK_USERNAME;
// override channel
if (SLACK_CHANNEL) message.channel = SLACK_CHANNEL;
/*
If provided avatar add it,
also can use use sender, or repository image.
defaults to webhook slack app;
*/
if (selectAvatar()) message.icon_url = selectAvatar();
return message;
}
return {
get() {
if (!instance) instance = createInstance();
return instance;
}
};
})();
module.exports = messageSingleton;