-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification-service.js
94 lines (78 loc) · 2.92 KB
/
notification-service.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
91
92
93
94
/**
* Sends messages to Slack or Telegram, if configured for the message's log level, prefixing each message with an icon
* matching its log level.
*
* This assumes notifications just work. If we can get proper errors below, we could buffer (or at least count) failed
* notifications, but that has not been implemented; see the error handlers in the code below.
*/
'use strict';
const Logger = require('winston').Logger;
const WinstonSlack = require('slack-winston').Slack;
const WinstonTelegram = require('winston-telegram').Telegram;
/**
* Icons to be prefixed to the Slack and Telegram messages.
*/
const icons = {
// Telegram does not support codes such as `:warning:`, so use Unicode; see https://www.fileformat.info/info/emoji
error: '\u274C ',
warn: '\u26A0 ',
info: '\u2139 '
};
class NotificationService {
constructor(config) {
if (!config) {
return;
}
this.logger = new Logger();
if (config.slack) {
// For slack-winston, the metadata is passed to a Lodash template as a JSON string
config.slack.message = '{{ JSON.parse(meta).icon }}{{message}}';
this.logger.add(WinstonSlack, config.slack);
}
if (config.telegram) {
// winston-telegram uses a basic string formatter (or a custom function in `formatMessage`)
config.telegram.template = '{metadata.icon}{message}';
this.logger.add(WinstonTelegram, config.telegram);
}
this.logger.on('error', err => {
// It seems this is not invoked when Slack or Telegram fails (like due to invalid tokens)? Not tested with
// a failing network connection.
console.error(`Error while sending notification; err: ${err}`);
});
}
/**
* Enhances the message with meta data to specify an icon which is used in the logger's template/formatter above,
* and passes the message to the Winston logger(s), if any.
*/
log(level, msg) {
if (!this.logger) {
return;
}
this.logger.log(level, msg,
{
icon: icons[level]
},
(err, level, msg, meta) => {
// When the message is not included due to its level, all parameters will be null.
if (err) {
// When using an invalid configuration, this is only called when Slack fails, but not when Telegram
// fails? Not tested with a failing network connection.
console.error(`Error while sending notification; err: ${err}; level: ${level}; msg: ${msg}; meta: ${meta}`);
}
}
);
}
debug(msg) {
this.log('debug', msg);
}
info(msg) {
this.log('info', msg);
}
warn(msg) {
this.log('warn', msg);
}
error(msg) {
this.log('error', msg);
}
}
module.exports = NotificationService;