From 0a1b5911e0cd7c07c1eb55d99260b51dbb1f84d7 Mon Sep 17 00:00:00 2001 From: Massimo Candela Date: Tue, 15 Dec 2020 03:49:08 +0100 Subject: [PATCH] reportTelegram use default user group if user group is not specified (#432) --- src/reports/reportHTTP.js | 48 +++++++++++++++++++---------------- src/reports/reportTelegram.js | 37 +++++++++++++++------------ 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/reports/reportHTTP.js b/src/reports/reportHTTP.js index 9cd442f..5189be5 100644 --- a/src/reports/reportHTTP.js +++ b/src/reports/reportHTTP.js @@ -60,26 +60,33 @@ export default class ReportHTTP extends Report { } } - _sendHTTPMessage = (url, channel, content) => { - const context = this.getContext(channel, content); + getTemplate = (group, channel, content) => { + return this.params.templates[channel] || this.params.templates["default"]; + }; - if (this.params.showPaths > 0 && context.pathNumber > 0) { - context.summary = `${context.summary}. Top ${context.pathNumber} most used AS paths: ${context.paths}.`; - } - const blob = this.parseTemplate(this.params.templates[channel] || this.params.templates["default"], context); + _sendHTTPMessage = (group, channel, content) => { + const url = this.params.hooks[group] || this.params.hooks["default"]; + if (url) { + const context = this.getContext(channel, content); - this.axios({ - url: url, - method: "POST", - headers: this.headers, - data: (this.params.isTemplateJSON) ? JSON.parse(blob) : blob - }) - .catch((error) => { - this.logger.log({ - level: 'error', - message: error + if (this.params.showPaths > 0 && context.pathNumber > 0) { + context.summary = `${context.summary}. Top ${context.pathNumber} most used AS paths: ${context.paths}.`; + } + const blob = this.parseTemplate(this.getTemplate(group, channel, content), context); + + this.axios({ + url: url, + method: "POST", + headers: this.headers, + data: (this.params.isTemplateJSON) ? JSON.parse(blob) : blob + }) + .catch((error) => { + this.logger.log({ + level: 'error', + message: error + }); }); - }); + } }; report = (channel, content) => { @@ -89,11 +96,8 @@ export default class ReportHTTP extends Report { groups = (groups.length) ? [...new Set(groups)] : Object.keys(this.params.hooks); // If there is no specific group defined, send to all of them for (let group of groups) { - if (this.params.hooks[group]) { - this._sendHTTPMessage(this.params.hooks[group], channel, content); - } + this._sendHTTPMessage(group, channel, content); } } - - } + }; } \ No newline at end of file diff --git a/src/reports/reportTelegram.js b/src/reports/reportTelegram.js index 84d54ef..0388645 100644 --- a/src/reports/reportTelegram.js +++ b/src/reports/reportTelegram.js @@ -35,23 +35,12 @@ import ReportHTTP from "./reportHTTP"; export default class reportTelegram extends ReportHTTP { constructor(channels, params, env) { - const templates = {}; const hooks = {}; - const getTemplateItem = (chatId) => { - return JSON.stringify({ - "chat_id": chatId, - "text": "${summary}", - "parse_mode": 'HTML', - "disable_web_page_preview": true - }); - }; - - for (let channel in params.chatIds) { - templates[channel] = getTemplateItem(params.chatIds[channel]); - hooks[channel] = params.botUrl; + for (let userGroup in params.chatIds) { + hooks[userGroup] = params.botUrl; } - + hooks["default"] = params.botUrl; const telegramParams = { headers: {}, @@ -59,10 +48,11 @@ export default class reportTelegram extends ReportHTTP { showPaths: params.showPaths, hooks: hooks, name: "reportTelegram", - templates + templates: {} }; super(channels, telegramParams, env); + this.chatIds = params.chatIds; if (!params.botUrl) { this.logger.log({ @@ -70,6 +60,21 @@ export default class reportTelegram extends ReportHTTP { message: `${this.name} reporting is not enabled: no botUrl provided` }); this.enabled = false; + } else if (!params.chatIds["default"]) { + this.logger.log({ + level: 'error', + message: `${this.name} reporting is not enabled: no default chat id provided` + }); + this.enabled = false; } - } + }; + + getTemplate = (group, channel, content) => { + return JSON.stringify({ + "chat_id": this.chatIds[group] || this.chatIds["default"], + "text": "${summary}", + "parse_mode": 'HTML', + "disable_web_page_preview": true + }); + }; } \ No newline at end of file