1
0
mirror of https://github.com/nttgin/BGPalerter.git synced 2024-05-19 06:50:08 +00:00

reportTelegram use default user group if user group is not specified (#432)

This commit is contained in:
Massimo Candela
2020-12-15 03:49:08 +01:00
parent 5b5d62c0e1
commit 0a1b5911e0
2 changed files with 47 additions and 38 deletions

View File

@@ -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);
}
}
}
};
}

View File

@@ -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
});
};
}