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:
@@ -60,26 +60,33 @@ export default class ReportHTTP extends Report {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_sendHTTPMessage = (url, channel, content) => {
|
getTemplate = (group, channel, content) => {
|
||||||
const context = this.getContext(channel, content);
|
return this.params.templates[channel] || this.params.templates["default"];
|
||||||
|
};
|
||||||
|
|
||||||
if (this.params.showPaths > 0 && context.pathNumber > 0) {
|
_sendHTTPMessage = (group, channel, content) => {
|
||||||
context.summary = `${context.summary}. Top ${context.pathNumber} most used AS paths: ${context.paths}.`;
|
const url = this.params.hooks[group] || this.params.hooks["default"];
|
||||||
}
|
if (url) {
|
||||||
const blob = this.parseTemplate(this.params.templates[channel] || this.params.templates["default"], context);
|
const context = this.getContext(channel, content);
|
||||||
|
|
||||||
this.axios({
|
if (this.params.showPaths > 0 && context.pathNumber > 0) {
|
||||||
url: url,
|
context.summary = `${context.summary}. Top ${context.pathNumber} most used AS paths: ${context.paths}.`;
|
||||||
method: "POST",
|
}
|
||||||
headers: this.headers,
|
const blob = this.parseTemplate(this.getTemplate(group, channel, content), context);
|
||||||
data: (this.params.isTemplateJSON) ? JSON.parse(blob) : blob
|
|
||||||
})
|
this.axios({
|
||||||
.catch((error) => {
|
url: url,
|
||||||
this.logger.log({
|
method: "POST",
|
||||||
level: 'error',
|
headers: this.headers,
|
||||||
message: error
|
data: (this.params.isTemplateJSON) ? JSON.parse(blob) : blob
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
this.logger.log({
|
||||||
|
level: 'error',
|
||||||
|
message: error
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
report = (channel, content) => {
|
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
|
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) {
|
for (let group of groups) {
|
||||||
if (this.params.hooks[group]) {
|
this._sendHTTPMessage(group, channel, content);
|
||||||
this._sendHTTPMessage(this.params.hooks[group], channel, content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
|
||||||
}
|
}
|
@@ -35,23 +35,12 @@ import ReportHTTP from "./reportHTTP";
|
|||||||
export default class reportTelegram extends ReportHTTP {
|
export default class reportTelegram extends ReportHTTP {
|
||||||
|
|
||||||
constructor(channels, params, env) {
|
constructor(channels, params, env) {
|
||||||
const templates = {};
|
|
||||||
const hooks = {};
|
const hooks = {};
|
||||||
|
|
||||||
const getTemplateItem = (chatId) => {
|
for (let userGroup in params.chatIds) {
|
||||||
return JSON.stringify({
|
hooks[userGroup] = params.botUrl;
|
||||||
"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;
|
|
||||||
}
|
}
|
||||||
|
hooks["default"] = params.botUrl;
|
||||||
|
|
||||||
const telegramParams = {
|
const telegramParams = {
|
||||||
headers: {},
|
headers: {},
|
||||||
@@ -59,10 +48,11 @@ export default class reportTelegram extends ReportHTTP {
|
|||||||
showPaths: params.showPaths,
|
showPaths: params.showPaths,
|
||||||
hooks: hooks,
|
hooks: hooks,
|
||||||
name: "reportTelegram",
|
name: "reportTelegram",
|
||||||
templates
|
templates: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
super(channels, telegramParams, env);
|
super(channels, telegramParams, env);
|
||||||
|
this.chatIds = params.chatIds;
|
||||||
|
|
||||||
if (!params.botUrl) {
|
if (!params.botUrl) {
|
||||||
this.logger.log({
|
this.logger.log({
|
||||||
@@ -70,6 +60,21 @@ export default class reportTelegram extends ReportHTTP {
|
|||||||
message: `${this.name} reporting is not enabled: no botUrl provided`
|
message: `${this.name} reporting is not enabled: no botUrl provided`
|
||||||
});
|
});
|
||||||
this.enabled = false;
|
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
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
Reference in New Issue
Block a user