-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (177 loc) · 5.15 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const https = require('https');
const dateformat = require('dateformat');
const template = require('./template.js');
module.exports = (function (){
//#region PRIVATE
const defaultOpt = {
token: '',
receivers: [],
projectName: '',
mode: 'text',
dateformat: 'd/mm/yyyy, HH:MM:ss',
disableLinkPreview: false,
silent: false,
template: 'default'
};
const send = (data) => {
let p = [];
for (let i = 0, l = data.options.receivers.length; i < l; i++) {
p.push(new Promise((res,rej) => {
let req = https.request({
host: 'api.telegram.org',
method: 'GET',
path: data.path + '&chat_id=' + data.options.receivers[i]
});
req.on('error', e => rej(e) );
req.on('close',() => res() );
req.end();
}));
}
return Promise.all(p);
};
const typeToEmoji = (type) => {
switch(type){
case 'info':
return '\ud83d\udcac';
case 'warning':
return '\u26a0';
case 'error':
return '\u203c';
case 'debug':
return '\ud83d\udc1e';
}
return '';
};
const logFormatter = (text, code, options) => {
let r = '';
const templateValue = {
text: text,
code: code,
projectName: options.projectName,
date: dateformat(new Date(), options.dateformat),
type: options.type,
emojiType: typeToEmoji(options.type)
}
if(options.template){
let t = typeof template[options.template] !== "undefined"
? template[options.template][options.mode]
: options.template;
for(tv in templateValue){
t = t.replace(
new RegExp('\{\{' + tv + '\}\}','g'),
(templateValue[tv]).replace(/{/g,'\\{').replace(/}/g,'\\}')
);
}
t = t.replace(/\\{/g,'{').replace(/\\}/g,'}');
return encodeURI(t);
}else{
return encodeURI(text);
}
};
const pathMaker = (text, code, options) => {
const initPath = '/bot' + options.token + '/sendMessage?text=' + logFormatter(text, code, options);
const additionalBlock = [{
cond: options.disableLinkPreview,
val: 'disable_web_page_preview=1'
},{
cond: options.silent,
val: 'disable_notification=1'
},{
cond: options.mode === 'markdown',
val: 'parse_mode=Markdown'
},{
cond: options.mode === 'html',
val: 'parse_mode=HTML'
}];
const restOfPath = additionalBlock.filter(x=>x.cond).map(x=>x.val).join('&');
return {
path: restOfPath.length > 0 ? initPath + '&' + restOfPath : initPath,
options
};
};
const takeParameters = (code, options, type) => {
let finalC = '';
let customOpt = {};
let codeIsOption = false;
if(code !== null){
if(typeof code === 'object'){
customOpt = code;
codeIsOption = true;
}else{
finalC = code + '';
}
}
if(options!=null && !codeIsOption){
customOpt = options;
}
let finalOpt = Object.assign({}, this.options, customOpt, { type: type } );
let lowMode = finalOpt.mode.toLowerCase();
if(lowMode === 'html') finalOpt.mode = 'html';
else if(lowMode === 'markdown') finalOpt.mode = 'markdown';
else finalOpt.mode = 'text';
return {
code: finalC,
options: finalOpt
};
};
//#endregion
//#region PUBBLIC
/**
* initialize logger
* @param {object} options options for the logger, options.token and options.receivers are required
* @return {telegram-log}
*/
this.init = (options) => {
if(typeof options.token === 'undefined') throw 'token required!'
if(typeof options.receivers === 'undefined') throw 'receivers required!'
if(typeof options.receivers === 'string') options.receivers = [options.receivers];
this.options = Object.assign( {}, defaultOpt, options);
return this;
};
/**
* send log to bot whit type set to info
* @param {string} text message to log
* @param {number} code log code (optional)
* @param {object} options custom options for single-use (optional)
* @return {Promise}
*/
this.info = (text, code=null, options=null) => {
const param = takeParameters(code, options, 'info');
//console.log(decodeURI(textFormatter(text, param.code, param.options)));
return send(pathMaker(text, param.code, param.options));
};
/**
* send log to bot whit type set to warning
* @param {string} text message to log
* @param {number} code log code (optional)
* @param {object} options custom options for single-use (optional)
* @return {Promise}
*/
this.warning = (text, code=null, options=null) => {
const param = takeParameters(code, options, 'warning');
return send(pathMaker(text, param.code, param.options));
};
/**
* send log to bot with type set to error
* @param {string} text message to log
* @param {number} code log code (optional)
* @param {object} options custom options for single-use (optional)
* @return {Promise}
*/
this.error = (text, code=null, options=null) => {
const param = takeParameters(code, options, 'error');
return send(pathMaker(text, param.code, param.options));
};
/**
* send log to bot with type set to debug
* @param {string} text message to log
* @param {number} code log code (optional)
* @param {object} options custom options for single-use (optional)
* @return {Promise}
*/
this.debug = (text, code=null, options=null) => {
const param = takeParameters(code, options, 'debug');
return send(pathMaker(text, param.code, param.options));
};
return this;
})();