Refactor out to a template for easier message maintenance.
This commit is contained in:
1 parent
e44cbf376f
commit
aa977ce34e
1 file changed
+24
-16
@@ -18,25 +18,27 @@ class NotificationService {
|
|||||||
this.networkService = networkService;
|
this.networkService = networkService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
formatNotificationMessage(monitor, status, platform, chatId) {
|
||||||
|
const messageText = `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`;
|
||||||
|
|
||||||
|
if (platform === 'telegram') {
|
||||||
|
return { chat_id: chatId, text: messageText };
|
||||||
|
}
|
||||||
|
if (platform === 'slack') {
|
||||||
|
return { text: messageText };
|
||||||
|
}
|
||||||
|
if (platform === 'discord') {
|
||||||
|
return { content: messageText };
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) {
|
async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) {
|
||||||
const { monitor, status } = networkResponse;
|
const { monitor, status } = networkResponse;
|
||||||
let message;
|
|
||||||
let url = address;
|
let url = address;
|
||||||
|
|
||||||
if (platform === 'slack') {
|
const message = this.formatNotificationMessage(monitor, status, platform, chatId);
|
||||||
message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
if (!message) {
|
||||||
} else if (platform === 'discord') {
|
|
||||||
message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
|
||||||
} else if (platform === 'telegram') {
|
|
||||||
if (!botToken || !chatId) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
message = {
|
|
||||||
chat_id: chatId,
|
|
||||||
text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`
|
|
||||||
};
|
|
||||||
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
|
|
||||||
} else {
|
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Unsupported platform: ${platform}`,
|
message: `Unsupported platform: ${platform}`,
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
@@ -46,6 +48,13 @@ class NotificationService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (platform === 'telegram') {
|
||||||
|
if (!botToken || !chatId) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await this.networkService.requestWebhook(platform, url, message);
|
const response = await this.networkService.requestWebhook(platform, url, message);
|
||||||
return response.status;
|
return response.status;
|
||||||
@@ -64,7 +73,6 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends an email notification for hardware infrastructure alerts
|
* Sends an email notification for hardware infrastructure alerts
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in new issue
Block a user