-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (41 loc) · 1.32 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
const fetch = require('node-fetch');
const tools = require('tools');
const config = require('./config');
const DEBUG = false;
let job = null;
let array = {};
// ==============================================
function onEvent(param) {
if (DEBUG) console.debug(`onEvent: ${param}`);
const txt = tools.textify(param, { colors: false });
array[txt] = array[txt] +1 || 1;
if (job) clearTimeout(job);
job = setTimeout(onTimer, 1500);
}
// ==============================================
function onTimer() {
if (DEBUG) console.debug('[+] notify: sending array');
if (DEBUG) console.debug(array);
Object.keys(array).forEach((key) => {
let text = key;
if (array[key] > 1) text += ` [count: ${array[key]}]`;
if (DEBUG) console.debug(`[ ] sending text: ${text}`);
// ready? send!
fetch(config.notify_url, {
method: 'POST',
body: JSON.stringify({ txt: text }),
headers: { 'Content-Type': 'application/json' }
})
.then((response) => response.text())
// .then((data) => {
// if (callback) return callback(data);
// console.debug(`[+] notify response: ${data}`);
// })
.catch((e) => {
console.error(`[-] notify error: ${e.message}`);
});
});
if (DEBUG) console.debug('[+] notify: clear array');
array = {};
}
module.exports = onEvent;