/
TelegramNotificationChannel.ts
133 lines (118 loc) · 3.26 KB
/
TelegramNotificationChannel.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable no-useless-escape */
import { Job } from "bullmq";
import { NotificationChannel } from "./NotificationChannel";
import axios from "axios";
import { render } from "mustache";
import {
NotificatonTransportLogItem,
TelegramNotificationChannelOptions
} from "../../types";
const basicTemplate = `{{emojiLevel}} *{{level}} log message* {{#timestamp}}⏱ _{{timestamp}}_{{/timestamp}}
{{#meta}}
\`[{{label}}]: {{value}}\`
{{/meta}}
{{#shrinked}}
>The message is shrinked as it's over {{shrinked}} characters length\\.
>Please, consider a more accurate handler for this log entry in your code\\.
{{/shrinked}}
\`\`\`json
{{{message}}}
\`\`\``;
const emojiLevels = [
{ level: "error", emoji: "🔴" },
{ level: "warn", emoji: "🟡" },
{ level: "info", emoji: "🟢" },
{ level: "http", emoji: "🔵" },
{ level: "verbose", emoji: "🟣" },
{ level: "debug", emoji: "🪲" },
{ level: "silly", emoji: "🐤" }
];
function getEmojiLevel(level: string) {
const item = emojiLevels.find((item) => item.level === level);
return item?.emoji || "🔵";
}
function renderLogMessage(template: string, log: NotificatonTransportLogItem) {
const { level, message, timestamp, meta = {} } = log;
const limit = 2048;
const { shrinked, shrinkedMessage } = shrinkStringSize(
JSON.stringify(message, null, 2),
limit
);
const unescaped: {
level: string;
message: string | string[];
timestamp?: string;
meta: {
label: string;
value: string;
}[];
} = {
level: unescape(level),
message: unescape(shrinkedMessage),
timestamp: unescape(timestamp),
meta: Object.keys(meta).map((label) => ({
label,
value: unescape(meta[label]?.toString())
}))
};
const rendered = render(template, {
...unescaped,
emojiLevel: getEmojiLevel(log.level),
shrinked: shrinked ? limit : undefined
});
return rendered;
function unescape(str?: string) {
return str?.replace(/([|{[\]*_~}+)(#>!=\-.])/gm, "\\$1") || "";
}
}
function shrinkStringSize(
str: string,
limit: number
): { shrinkedMessage: string; shrinked: boolean } {
const shrinked = str.length > limit;
return {
shrinkedMessage: str.substring(0, limit),
shrinked
};
}
export class TelegramNotificationChannel extends NotificationChannel {
constructor(opts: TelegramNotificationChannelOptions) {
super(opts);
const {
host = "https://api.telegram.org/bot",
token,
workerOptions = {},
chatId,
template = basicTemplate
} = opts;
const baseURL = new URL(`/bot${token}`, host).toString();
this.process({
queueName: `${token}:${chatId}`,
processor: async (job: Job) => {
try {
const log: NotificatonTransportLogItem = job.data;
await axios({
method: "post",
baseURL,
url: "sendMessage",
data: {
chat_id: chatId,
text: renderLogMessage(template, log),
parse_mode: "MarkdownV2"
}
});
} catch (error) {
console.error(error);
}
return job.data;
},
workerOptions: {
...workerOptions,
limiter: workerOptions?.limiter || {
max: 1,
duration: 5000
}
}
});
}
}