UNPKG

@lskjs/bots-plugin-notify

Version:

LSK.js plugin for @lskjs/bots module for telegram, slack, discord, whatsapp, twitter, instagram and vk bots creation

316 lines (252 loc) 11.5 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.NotifyPlugin = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _botsPlugin = _interopRequireDefault(require("@lskjs/bots-plugin")); var _err = _interopRequireDefault(require("@lskjs/err")); var _axios = _interopRequireDefault(require("axios")); var _bluebird = _interopRequireDefault(require("bluebird")); var _forEach = _interopRequireDefault(require("lodash/forEach")); var _get = _interopRequireDefault(require("lodash/get")); var _isEmpty = _interopRequireDefault(require("lodash/isEmpty")); var _nodeCron = _interopRequireDefault(require("node-cron")); var _createPost = require("./createPost"); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } var NotifyPlugin = /*#__PURE__*/function (_BaseBotPlugin) { (0, _inherits2["default"])(NotifyPlugin, _BaseBotPlugin); var _super = _createSuper(NotifyPlugin); function NotifyPlugin() { var _this; (0, _classCallCheck2["default"])(this, NotifyPlugin); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _this.providers = ['telegram', 'slack']; _this.crons = []; _this.alertmanager = _createPost.alertmanager; _this.github = _createPost.github; _this.gitlab = _createPost.gitlab; _this.graylog = _createPost.graylog; _this.getEconnabortedErrorMessage = _createPost.getEconnabortedErrorMessage; _this.getOtherErrorMessage = _createPost.getOtherErrorMessage; _this.getRedirectErrorMessage = _createPost.getRedirectErrorMessage; _this.getWarningMessage = _createPost.getWarningMessage; return _this; } (0, _createClass2["default"])(NotifyPlugin, [{ key: "sendNotification", value: function sendNotification(_ref) { var bot = _ref.bot, message = _ref.message; if (!message) throw new _err["default"]('!message'); var provider = bot.provider; var projectName = message.projectName; var project = this.config.projects[projectName]; var isDefault = false; if (!project) { isDefault = true; project = this.config.projects._default; } var msg = (0, _get["default"])(message, 'text'); if (this.debug) this.log.trace('NotifyPlugin.sendNotification.message', message); if (message.type === 'gitlab') { msg = this.gitlab(message, project, bot); } if (message.type === 'github') { msg = this.github(message, project, bot); } if (message.type === 'alertmanager') { msg = this.alertmanager(message, bot); } if (message.type === 'graylog') { msg = this.graylog(message); } if (isDefault && msg) { msg = "/notify/".concat(projectName, "\n\n").concat(msg); } var options = {}; if (message.md || message.isMd) { options.parse_mode = 'MarkdownV2'; } var chats = project[provider] || []; if (!msg) throw new _err["default"]('!NotifyPlugin.sendNotification.msg'); return _bluebird["default"].map(chats.filter(Boolean), function (chat) { return bot.sendMessage(chat, msg, options); }); } }, { key: "checkResourses", value: function checkResourses(bot) { var _this2 = this; var projects = this.config.projects; var timeout = 30 * 1000; var timeoutWarn = 7 * 1000; (0, _forEach["default"])(projects, function (project, projectName) { // eslint-disable-next-line no-shadow var monitoring = project.monitoring; if (!monitoring) return; (0, _forEach["default"])(monitoring, /*#__PURE__*/function () { var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(resourse) { var url, time, _yield$axios, status, message, _message, _err$response, _err$response2, _message2; return _regenerator["default"].wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: url = resourse.url; if (url) { _context.next = 3; break; } return _context.abrupt("return", {}); case 3: _context.prev = 3; time = new Date(); _context.next = 7; return (0, _axios["default"])({ url: url, timeout: timeout }); case 7: _yield$axios = _context.sent; status = _yield$axios.status; if (!(status >= 300)) { _context.next = 14; break; } message = _this2.getRedirectErrorMessage({ projectName: projectName, url: url }, bot); _context.next = 13; return _this2.sendNotification({ bot: bot, message: message }); case 13: return _context.abrupt("return", {}); case 14: if (!(Date.now() - time >= timeoutWarn)) { _context.next = 19; break; } _message = _this2.getWarningMessage({ projectName: projectName, url: url, timeoutWarn: timeoutWarn }, bot); _context.next = 18; return _this2.sendNotification({ bot: bot, message: _message }); case 18: return _context.abrupt("return", { status: 200 }); case 19: _this2.log.debug('Resource monitoring - OK', url); return _context.abrupt("return", {}); case 23: _context.prev = 23; _context.t0 = _context["catch"](3); _message2 = _context.t0 && _context.t0.code === 'ECONNABORTED' ? _this2.getEconnabortedErrorMessage({ projectName: projectName, url: url, timeout: timeout }, bot) : _this2.getOtherErrorMessage({ projectName: projectName, url: url, err: _context.t0 }, bot); _this2.log.error("Status: ".concat((_context.t0 === null || _context.t0 === void 0 ? void 0 : (_err$response = _context.t0.response) === null || _err$response === void 0 ? void 0 : _err$response.status) || '', " ").concat((_context.t0 === null || _context.t0 === void 0 ? void 0 : (_err$response2 = _context.t0.response) === null || _err$response2 === void 0 ? void 0 : _err$response2.statusText) || '')); _context.next = 29; return _this2.sendNotification({ bot: bot, message: _message2 }); case 29: return _context.abrupt("return", {}); case 30: case "end": return _context.stop(); } } }, _callee, null, [[3, 23]]); })); return function (_x) { return _ref2.apply(this, arguments); }; }()); }); } }, { key: "runMonitoring", value: function runMonitoring(bot) { var _this3 = this; var cronConfig = (0, _get["default"])(this.config, 'cron', '* * * * *'); if (!Array.isArray(cronConfig)) cronConfig = [cronConfig]; this.crons = cronConfig.map(function (config) { var _config$time = config.time, time = _config$time === void 0 ? '* * * * *' : _config$time, _config$timeZone = config.timeZone, timeZone = _config$timeZone === void 0 ? 'Europe/Moscow' : _config$timeZone; return { time: time, timeZone: timeZone, cron: _nodeCron["default"].schedule(time, function () { _this3.checkResourses(bot); }, { timeZone: timeZone }) }; }); this.log.info("Cron: [".concat(this.crons.map(function (_ref3) { var time = _ref3.time; return time; }).join(','), "]. Provider: ").concat(bot.provider)); } }, { key: "runBot", value: function () { var _runBot = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(bot) { return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: if (!(!this.config || (0, _isEmpty["default"])(this.config))) { _context2.next = 2; break; } return _context2.abrupt("return"); case 2: this.runMonitoring(bot); case 3: case "end": return _context2.stop(); } } }, _callee2, this); })); function runBot(_x2) { return _runBot.apply(this, arguments); } return runBot; }() }]); return NotifyPlugin; }(_botsPlugin["default"]); exports.NotifyPlugin = NotifyPlugin; var _default = NotifyPlugin; exports["default"] = _default; //# sourceMappingURL=NotifyPlugin.js.map