-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.js
52 lines (38 loc) · 1.24 KB
/
notify.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
const axios = require('axios');
const tools = require('./index');
const NOTIFY_URL = 'https://bs1.konser.ru/notifybot/alert';
const TIMER_WAIT = 2000;
const DEBUG = false;
let job = null;
let array = {};
// ==============================================
function notify(param, url) {
if (DEBUG) console.log(`[ ] onEvent: ${param}`);
let address = NOTIFY_URL;
if (url && typeof url === 'string' && url.startsWith('https://')) address = url;
const txt = tools.textify(param, { colors: false });
array[txt] = array[txt] +1 || 1;
if (job) clearTimeout(job);
job = setTimeout(() => onTimer(address), TIMER_WAIT);
}
// ==============================================
function onTimer(url) {
if (DEBUG) console.log('[+] notify: sending array');
if (DEBUG) console.log(array);
Object.keys(array).forEach(async (key) => {
let text = key;
if (array[key] > 1) text += ` [count: ${array[key]}]`;
if (DEBUG) console.log(`[ ] sending text: ${text}`);
const payload = { txt: text };
// ready? send!
try {
await axios.post(url, payload);
if (DEBUG) console.log(`[+] sent text`);
}
catch (err) {
console.warn(` notify: ${err.message}`);
}
});
array = {};
}
module.exports.notify = notify;