/
send-slack-message.js
53 lines (46 loc) · 1.12 KB
/
send-slack-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
52
53
const POST_MESSAGE_API_URL = 'https://slack.com/api/chat.postMessage';
module.exports = {
inputs: {
markdown: {
type: 'string',
required: true,
},
},
async fn(inputs) {
const headers = {
Authorization: `Bearer ${sails.config.custom.slackBotToken}`,
'Content-Type': 'application/json; charset=utf-8',
};
const body = {
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: inputs.markdown,
},
},
],
channel: sails.config.custom.slackChannelId,
};
let response;
try {
response = await fetch(POST_MESSAGE_API_URL, {
headers,
method: 'POST',
body: JSON.stringify(body),
});
} catch (error) {
sails.log.error(`Error sending to Slack: ${error}`);
return;
}
if (!response.ok) {
sails.log.error(`Error sending to Slack: ${response.error}`);
return;
}
const responseJson = await response.json();
if (!responseJson.ok) {
sails.log.error(`Error sending to Slack: ${responseJson.error}`);
}
},
};