-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.js
148 lines (134 loc) · 4.69 KB
/
init.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const pm2 = require('pm2');
const dayjs = require('dayjs');
const { get } = require('radash');
const wxwork = require('./bots/wxwork_bot');
const bark = require('./bots/bark');
const dingtalk = require('./bots/dingtalk');
const flybook = require('./bots/flybook');
const slack = require('./bots/slack');
const telegram = require('./bots/telegram');
const email = require('./bots/email');
const { md5 } = require('./lib');
const cache = {}
let currentPmID = -1;
global.debug = false;
/**
* 入口
* @param {object} conf
* @param {object} conf.module_conf
*/
module.exports = function (conf) {
global.debug = conf.module_conf.debug || false;
if (global.debug) console.log(conf.module_conf)
const delay = Number(conf.module_conf.delay) || 15;
const botCofnig = new ModelConfig(conf.module_conf);
if (global.debug) console.log(botCofnig)
// 获取当前进程pm_id
pm2.list((err, list) => {
if (err) throw err;
for (let item of list) {
if (item.name == conf.module_name) {
currentPmID = item.pm_id;
}
}
})
pm2.launchBus((err, bus) => {
if (err) throw err; // 如果启动错误,则直接抛出错误
bus.on('log:err', (info) => {
if (info.process.pm_id == currentPmID || currentPmID < 0) return; // 如果是当前进程的错误,则不处理,有可能会造成递归
let find = botCofnig.get(info.process.name, 'find') || botCofnig.find;
let data = extra(find, info.data);
if (!data) return;
let key = md5(info.process.name + JSON.stringify(data))
if (cache[key]) return cache[key].times += 1;
cache[key] = {
name: info.process.name,
id: info.process.pm_id,
time: dayjs(info.at).format('YYYY-MM-DD HH:mm:ss'),
msg: info.data,
times: 1,
delay,
};
setTimeout(() => {
wxwork(botCofnig.get(info.process.name, 'wxwork'), cache[key]);
bark(botCofnig.get(info.process.name, 'bark'), cache[key]);
dingtalk(botCofnig.get(info.process.name, 'dingtalk'), cache[key]);
flybook(botCofnig.get(info.process.name, 'feishu'), cache[key]);
slack(botCofnig.get(info.process.name, 'slack'), cache[key]);
telegram(botCofnig.get(info.process.name, 'telegram'), cache[key]);
email(conf.smtp, botCofnig.get(info.process.name, 'email'), cache[key]);
cache[key] = undefined;
}, delay * 1000)
})
});
}
function extra(find, data) {
try {
if (!find) return data;
if (typeof find === "string") {
let obj = JSON.parse(data)
let msg = get(obj, find)
if (msg) return msg;
return data; // 如果没有找到对应的 key 则返回全部对象
} else if (find.test(data)) {
return data // 如果正则匹配则返回整条信息, 正则不匹配的信息不做通知
}
} catch (e) {
if (global.debug) console.log(e)
return data
}
}
/**
*
* @param {string} find
* @returns
*/
function parseFind(find) {
if (!find) return null;
if (find[0] === '$') {
return find.substring(2)
}
if (/^\/.*\/[igmu]*$/i.test(find)) {
return eval(find)
}
return new RegExp(find)
}
class ModelConfig {
find;
conf = {};
data = {};
types = ['wxwork', 'bark', 'slack', 'feishu', 'dingtalk', 'telegram', 'email'];
constructor(conf) {
this.conf = conf || {};
for (let k of this.types) {
this.data[k] = (conf[k] || '').split(',').filter(v => v) || [];
}
this.find = parseFind(conf.find);
this._getAll();
}
_getAll() {
for (let key in this.conf) {
var keys = key.split('_');
if (keys.length === 1) continue;
var name = keys.slice(0, keys.length - 1).join('_');
if (!this.data[name]) this.data[name] = {};
var typ = keys[keys.length - 1];
if (typ === 'find') {
this.data[name][typ] = parseFind(this.conf[key])
continue;
}
if (this.types.includes(typ)) {
this.data[name][typ] = [...(this.data[name][typ] || []), this.conf[key]]
}
}
}
_getfind(name) {
if (!this.data[name]) return this.find;
return this.data[name]['find']
}
get(name, type) {
if (type === 'find') return this._getfind(name);
if (this.data[name] && this.data[name][type]) return this.data[name][type] || [];
return this.data[type] || [];
}
}