proper last version
This commit is contained in:
parent
9e2ab3ccea
commit
6d468acd6f
1 changed files with 64 additions and 50 deletions
|
|
@ -58,57 +58,71 @@ const logger = log4js.getLogger();
|
|||
const monitor = log4js.getLogger("monitorLogs");
|
||||
|
||||
exports.install = function(instance) {
|
||||
process.on('uncaughtException', function (err) {
|
||||
errLogger.error('uncaughtException:', err.message);
|
||||
errLogger.error(err.stack);
|
||||
instance.error(err);
|
||||
});
|
||||
var can = false;
|
||||
|
||||
instance.on('data', function(data) {
|
||||
let str = String(data); // Ensuring data get converted to string
|
||||
let message = `{
|
||||
channel: ${instance.options.slack_channel},
|
||||
username: ${instance.options.bot_name},
|
||||
icon_emoji: ${instance.options.bot_icon},
|
||||
text: ${str}
|
||||
}`;
|
||||
let headers = {
|
||||
'Content-type': `application/json`,
|
||||
'Authorization': `Bearer ${instance.options.api_key}`
|
||||
process.on('uncaughtException', function (err) {
|
||||
errLogger.error('uncaughtException:', err.message);
|
||||
errLogger.error(err.stack);
|
||||
instance.error(err);
|
||||
});
|
||||
|
||||
instance.on('data', function(data) {
|
||||
if (!can) return;
|
||||
|
||||
let str = String(data.data); // Ensuring data get converted to string
|
||||
let message = {
|
||||
'channel': instance.options.slack_channel,
|
||||
'username': instance.options.bot_name,
|
||||
'icon_emoji': instance.options.bot_icon,
|
||||
'text': str
|
||||
};
|
||||
let headers = {
|
||||
'Content-type': `application/json`,
|
||||
'Authorization': `Bearer ${instance.options.api_key}`
|
||||
};
|
||||
|
||||
if (F.is4) {
|
||||
instance.send2("is F4");
|
||||
let opt = {
|
||||
'method': 'post',
|
||||
'url': 'https://slack.com/api/chat.postMessage',
|
||||
'headers': headers,
|
||||
'body': JSON.stringify(message),
|
||||
'type': 'json',
|
||||
'callback': function(err, response) {
|
||||
if (response && !err) {
|
||||
var msg = { data: response.body, status: response.status, headers: response.headers, host: response.host, cookies: response.cookies };
|
||||
instance.send2(msg);
|
||||
} else if (err) {
|
||||
errLogger.error('Slack post failed - err:', err, '\n - response was:', response);
|
||||
instance.error(err, response);
|
||||
}
|
||||
}
|
||||
};
|
||||
instance.send2(opt);
|
||||
REQUEST(opt);
|
||||
|
||||
} else {
|
||||
instance.send2("is NOT F4");
|
||||
|
||||
U.request('https://slack.com/api/chat.postMessage', ['json', 'post'], message, function(err, data, status, headers, host) {
|
||||
if (response && !err) {
|
||||
response.data = { data: data, status: status, headers: headers, host: host };
|
||||
instance.send2(response);
|
||||
} else if (err) {
|
||||
errLogger.error('Slack post failed - err:', err, '\n - response was:', response);
|
||||
instance.error(err, response);
|
||||
}
|
||||
}, null, headers);
|
||||
}
|
||||
});
|
||||
|
||||
instance.reconfigure = function() {
|
||||
var options = instance.options;
|
||||
can = options.slack_channel && options.bot_name && options.bot_icon && options.api_key ? true : false;
|
||||
instance.status(can ? '' : 'Not configured', can ? undefined : 'red');
|
||||
};
|
||||
|
||||
if (F.is4) {
|
||||
let opt = {
|
||||
'method': 'post',
|
||||
'url': 'https://slack.com/api/chat.postMessage',
|
||||
'headers': headers,
|
||||
'cookies': null,
|
||||
'body': JSON.stringify(message),
|
||||
'type': 'json',
|
||||
'callback': function(err, response) {
|
||||
if (response && !err) {
|
||||
var msg = { data: response.body, status: response.status, headers: response.headers, host: response.host, cookies: response.cookies };
|
||||
instance.send2(msg);
|
||||
} else if (err)
|
||||
errLogger.error('Slack post failed - err:', err, '\n - response was:', response);
|
||||
instance.error(err, response);
|
||||
}
|
||||
};
|
||||
REQUEST(opt);
|
||||
|
||||
} else {
|
||||
|
||||
U.request('https://slack.com/api/chat.postMessage', ['json', 'post'], JSON.stringify(message), function(err, data, status, headers, host) {
|
||||
console.log("err: ", err);
|
||||
console.log("resp: ", response);
|
||||
if (response && !err) {
|
||||
response.data = { data: data, status: status, headers: headers, host: host };
|
||||
instance.send2(response);
|
||||
} else if (err) {
|
||||
errLogger.error('Slack post failed - err:', err, '\n - response was:', response);
|
||||
instance.error(err, response);
|
||||
}
|
||||
}, null, headers);
|
||||
}
|
||||
});
|
||||
instance.on('options', instance.reconfigure);
|
||||
instance.reconfigure();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue