Skip to content

Commit

Permalink
send alerts to Slack (close #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Sep 29, 2023
1 parent 3891068 commit cbb56d6
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 3 deletions.
23 changes: 20 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "dotenv/config";
import { MongoClient } from "mongodb";
import { WebClient } from "@slack/web-api";
import Fastify from "fastify";

/**
Expand Down Expand Up @@ -31,6 +32,10 @@ const verboseMode = definedBolean(process.env.VERBOSE_MODE, false);
const showErrors = definedBolean(process.env.SHOW_ERRORS, true);
const pingIntervalConfig = process.env.PING_INTERVAL || "1000";
const pingInterval = parseInt(pingIntervalConfig, 10);
const slackToken = process.env.SLACK_TOKEN || "secret-token";
const slackChannel = process.env.SLACK_CHANNEL || "mongo-alerts";

const slackClient = new WebClient(slackToken);

// Create a new MongoClient
const client = new MongoClient(uri, {
Expand Down Expand Up @@ -96,10 +101,22 @@ const verboseLog = (msg) => {
/**
* Send the alert to Slack.
*/
const sendToSlack = () => {
const sendToSlack = async () => {
log(`ALERT - sent alert on Slack: ${lastState}`);

// TODO: send the alert to Slack
await slackClient.chat.postMessage({
blocks: [
{
text: {
text: lastState,
type: "mrkdwn"
},
type: "section"
}
],
text: lastState,
channel: slackChannel,
});
};

/**
Expand Down Expand Up @@ -133,7 +150,7 @@ const pingServer = async () => {
if (lastState !== message) {
lastState = message;
lastChange = new Date().toISOString();
sendToSlack();
await sendToSlack();
}
};

Expand Down
231 changes: 231 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@slack/web-api": "^6.9.0",
"dotenv": "^16.3.1",
"fastify": "^4.23.2",
"mongodb": "^6.1.0"
Expand Down

0 comments on commit cbb56d6

Please sign in to comment.