Skip to content

Instantly share code, notes, and snippets.

@loloDawit
Created November 19, 2021 02:25
Show Gist options
  • Save loloDawit/e3321c356284446b9c10880fb77790a8 to your computer and use it in GitHub Desktop.
Save loloDawit/e3321c356284446b9c10880fb77790a8 to your computer and use it in GitHub Desktop.
Handler
/* eslint-disable no-undef */
"use strict";
const {unDeleteDashboard} = require("./newrelic/dashboard");
const {response} = require("./slack/format");
const {recoverModal} = require("./slack/payloads");
/**
* Bolt App Initialization
*/
const {App, ExpressReceiver} = require("@slack/bolt");
const awsServerlessExpress = require("aws-serverless-express");
const expressReceiver = new ExpressReceiver({
signingSecret:
process.env.NODE_ENV === "dev" ? process.env.SLACK_SIGNING_SECRET : process.env.SLACK_SIGNING_SECRET_PROD,
processBeforeResponse: true
});
const app = new App({
token: process.env.NODE_ENV === "dev" ? process.env.SLACK_BOT_TOKEN : process.env.SLACK_BOT_TOKEN_PROD,
receiver: expressReceiver,
processBeforeResponse: true
});
/**
* Application Logic
*/
// Listen for a slash command invocation
app.command("/recoverdashboard", async ({ack, body, client}) => {
// Acknowledge the command request
await ack();
try {
let view = recoverModal().view;
// Call views.open with the built-in client
await client.views.open({
// Pass a valid trigger_id within 3 seconds of receiving it
trigger_id: body.trigger_id,
// View payload
view
});
} catch (error) {
logger.error(error);
await ack(`:x: Failed to post a message (error: ${error})`);
}
});
// Handle a view_submission request
app.view("view_recover", async ({ack, body, view, client, logger}) => {
// Acknowledge the view_submission request
await ack();
const guid = view.state.values.guid_input.title.value;
const userId = body.user.id;
// const channelId = "C02DB6FRTDK"; can use this to post to a specific channel
// Message to send user
let msg = "";
let sideBar = "#db963c";
let extra = "";
const results = await unDeleteDashboard(guid);
if (!results.success) {
if (results.message === "DASHBOARD_NOT_FOUND") {
msg = `The dashboard is probably not deleted. API response: ${results.message}`;
extra = "*DASHBOARD_NOT_FOUND* _The dashboard is not found in a recently deleted queue._";
} else {
msg = `API response: ${results.message}`;
extra = `Status: ${results.success}`;
}
} else {
// success - dashboard has been recovered.
msg = ":tada: The dashboard is successfully restored :tada:";
sideBar = "#00b37d";
extra = `Status: ${results.success}`;
}
// Message the user
try {
await client.chat.postEphemeral({
user: userId,
channel: userId,
text: `*Hi, <@${userId}> :wave:*`,
attachments: response(sideBar, msg, extra).attachments
});
} catch (error) {
logger.error(error);
await ack(`:x: Failed to post a message (error: ${error})`);
}
});
app.action("btn_moreInfo", async ({ack, body}) => {
await ack();
var selectedUser = body.user.id;
// temp for now so it does not throw 502
console.log(`You selected <@${selectedUser}>`);
});
// ------------------------
// AWS Lambda handler
// ------------------------
const server = awsServerlessExpress.createServer(expressReceiver.app);
module.exports.app = (event, context) => {
awsServerlessExpress.proxy(server, event, context);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment