Skip to content

Commit

Permalink
reportTelegram use default user group if user group is not specified (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
massimocandela committed Dec 15, 2020
1 parent 5b5d62c commit 0a1b591
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
48 changes: 26 additions & 22 deletions src/reports/reportHTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
}
}

}
};
}
37 changes: 21 additions & 16 deletions src/reports/reportTelegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,46 @@ 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: {},
isTemplateJSON: true,
showPaths: params.showPaths,
hooks: hooks,
name: "reportTelegram",
templates
templates: {}
};

super(channels, telegramParams, env);
this.chatIds = params.chatIds;

if (!params.botUrl) {
this.logger.log({
level: 'error',
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
});
};
}

0 comments on commit 0a1b591

Please sign in to comment.