This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SlackNotifier.ts
68 lines (61 loc) · 2.31 KB
/
SlackNotifier.ts
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
import * as request from 'request';
interface SlackMessage {
channel: string;
text: string;
username: string;
icon_emoji: string;
attachments?: SlackMessageAttachement[];
}
interface SlackMessageAttachement {
title: string;
text: string;
}
export class SlackNotifier {
private readonly slackBaseUrl: string = 'https://hooks.slack.com';
private webHookUrl: string;
private defaultChannel: string;
private userName: string;
private icon: string;
constructor(webHookUrl: string, defaultChannel: string, userName: string, icon?: string) {
if (typeof webHookUrl !== 'string' || !webHookUrl.startsWith(this.slackBaseUrl)) {
throw new Error(`webHookUrl must be a valid string starting with "${this.slackBaseUrl}"`);
} else if (typeof defaultChannel !== 'string' || !defaultChannel.startsWith('#')) {
throw new Error('defaultChannel must be a valid string starting with "#"');
} else if (typeof userName !== 'string' || userName.trim() === '') {
throw new Error('userName is required');
}
this.webHookUrl = webHookUrl.trim();
this.defaultChannel = defaultChannel.trim();
this.userName = userName.trim();
this.icon = icon || ':ghost:';
}
public send(message: string, detail: string = null, channel: string = null): Promise<void> {
return new Promise<void>((resolve, reject) => {
const slackMessage: SlackMessage = {
channel: channel ? channel : this.defaultChannel,
text: message,
username: this.userName,
icon_emoji: this.icon
};
if (detail) {
slackMessage.attachments = [{
title: 'Detail',
text: detail
}];
}
request({
url: this.webHookUrl,
method: 'POST',
json: true,
body: slackMessage
}, (err, res, body) => {
if (err) {
return reject(err);
} else if (res.statusCode > 399) {
return reject(new Error(`An error occurred while sending slack notification: ${body}`));
}
return resolve();
});
});
}
}