Skip to content

Instantly share code, notes, and snippets.

@allanchua101
Last active August 22, 2020 13:58
Show Gist options
  • Save allanchua101/2055fbebb46d14f811f5db2e23d7b51d to your computer and use it in GitHub Desktop.
Save allanchua101/2055fbebb46d14f811f5db2e23d7b51d to your computer and use it in GitHub Desktop.
Sample module used for raising Slack Alarms
const axios = require("axios");
const moment = require("moment");
const slackConfig = require("./slack.config.json");
let buildMarkdown = (err) => {
let output = "";
output += `Something went wrong on our application! :boom:\n`;
output += `*Lambda Function Name:* ${process.env.AWS_LAMBDA_FUNCTION_NAME}\n`;
output += `*Error Message:* [${err.message}]\n`;
output += `*Stack Trace:*\n`;
output += "```";
output += `${err.stack}`;
output += "```";
output += `\n`;
return output;
};
/**
* @module slackNotifier
* @description Module used for sending messages to Slack Channels.
* @author Allan A. Chua
* @version 1.0
* @since August 22, 2020
*/
module.exports = {
/**
* @function sendAlert
* @description Method used for sending ChatOps alerts to slack channel.
* @param {Error} appError Application Error instance.
* @returns {Promise<object>} Promise indicating completion of request.
*/
sendAlert(appError) {
let payload = {
channel: slackConfig.slackChannelName,
attachments: [
{
author_name: "Alarm Bot",
text: buildMarkdown(appError),
mrkdwn: true,
color: "#FF0000",
ts: moment().unix(),
},
],
};
return new Promise((resolve, reject) => {
axios
.post(slackConfig.slackHookUrl, payload)
.then(() => {
resolve({ completed: true });
})
.catch((error) => {
reject({ completed: false, error });
});
});
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment