-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
62 lines (58 loc) · 1.99 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const Notifier = require('@runnerty/module-core').Notifier;
const { WebClient } = require('@slack/web-api');
const { createReadStream } = require('fs');
const path = require('path');
class slackNotifier extends Notifier {
constructor(notification) {
super(notification);
}
async send(notification) {
try {
if (!notification.token) throw new Error('should have required property token');
if (!notification.channel) throw new Error('should have required property channel');
const token = notification.token;
const web = new WebClient(token);
// Messages:
if (notification.message || notification.attachments) {
const data = {
text: notification.title,
channel: notification.channel,
icon_emoji: notification.bot_emoji,
username: notification.bot_name,
attachments: [
{
text: notification.message,
color: notification.color
}
]
};
if (notification.attachments) data.attachments = notification.attachments;
if (notification.markdown) data.mrkdwn = true;
if (notification.link_names) data.link_names = true;
if (notification.parse) data.parse = notification.parse;
await web.chat.postMessage(data);
} else {
if (!notification.file) throw new Error('should have required property message');
}
// File upload:
try {
if (notification.file) {
await web.files.upload({
filename: path.basename(notification.file),
file: createReadStream(notification.file),
channels: notification.channel,
title: notification.title
});
}
} catch (err) {
this.logger.log('warn', `Slack Notifier error uploading file: ${err}`);
}
this.end();
} catch (err) {
this.logger.log('warn', `Slack Notifier error: ${err}`);
this.end();
}
}
}
module.exports = slackNotifier;