Store bot token and chat id in their own respective fields.
This commit is contained in:
1 parent
d3fb41acfe
commit
8f9406261f
2 files changed
+14
-6
No files matched your search
@@ -10,6 +10,15 @@ const NotificationSchema = mongoose.Schema(
|
||||
type: String,
|
||||
enum: ["email", "sms", "discord", "slack", "telegram"],
|
||||
},
|
||||
webhookUrl: {
|
||||
type: String,
|
||||
},
|
||||
botToken: {
|
||||
type: String,
|
||||
},
|
||||
chatId: {
|
||||
type: String,
|
||||
},
|
||||
address: {
|
||||
type: String,
|
||||
},
|
||||
@@ -76,4 +85,5 @@ NotificationSchema.pre("findOneAndUpdate", function (next) {
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
export default mongoose.model("Notification", NotificationSchema);
|
||||
@@ -18,17 +18,16 @@ class NotificationService {
|
||||
this.networkService = networkService;
|
||||
}
|
||||
|
||||
async sendWebhookNotification(networkResponse, address, platform) {
|
||||
async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) {
|
||||
const { monitor, status } = networkResponse;
|
||||
let message;
|
||||
let url = address;
|
||||
|
||||
|
||||
if (platform === 'slack') {
|
||||
message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
||||
} else if (platform === 'discord') {
|
||||
message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
||||
} else if (platform === 'telegram') {
|
||||
const [botToken, chatId] = address.split('|').map(part => part?.trim());
|
||||
if (!botToken || !chatId) {
|
||||
return false;
|
||||
}
|
||||
@@ -38,7 +37,7 @@ class NotificationService {
|
||||
};
|
||||
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const response = await this.networkService.requestWebhook(platform, url, message);
|
||||
return response.status;
|
||||
@@ -56,9 +55,8 @@ class NotificationService {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Sends an email notification for hardware infrastructure alerts
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user