Skip to content

Commit

Permalink
chore: added comments and fixed various code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
NichArchA82 committed Dec 12, 2024
1 parent faf0999 commit d3847d3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
15 changes: 15 additions & 0 deletions bot/src/commands/button.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
/*
This is a sample command that creates a button in the slack message.
The button click event is handled by the button method.
*/

import logger from "command-handler/src/util/logger.js";

//initialize logger
const log = logger();

export default {
description: 'creates a button',

//button click event handler This is called when the button is clicked
button: ({ handler, body, response }) => {
log.info("Clicked the button", body.user)
response({
text: `<@${body.user.id}> clicked the Button`
})
},

//run method is called when the command is executed
//To execute this command type !button in the slack channel where the bot is installed
//The paramaters are descructured from the object passed to the run method
//response is used to send the response back to the slack channel
//event is the event object that contains the event details from slack.
run: ({ response, event }) => {

response({
//blocks is used to create a button in the slack message
blocks: [
{
"type": "section",
Expand All @@ -32,6 +46,7 @@ export default {
}
}
],
//text is fallback text that is displayed when the message is not supported
text: `Hey there <@${event.user}>!`
})
}
Expand Down
9 changes: 8 additions & 1 deletion bot/src/commands/hello.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
This is a simple command that replies with "Hey there <@user>!" after 5 seconds.
*/

//delay function to simulate a delay
const delay = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
Expand All @@ -6,10 +11,12 @@ export default {
description: 'Replies with Hello',

run: async ({ response, event }) => {
//call the delay function to simulate a delay of 5 seconds.
await delay(5000);

//send the response back to the slack channel After the delay
response({
text: `Hey there <@${event.user}>!`
})
});
}
}
4 changes: 4 additions & 0 deletions bot/src/commands/ping.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
This is a simple command that replies with pong
*/

export default {
description: 'Replies with pong',

Expand Down
7 changes: 6 additions & 1 deletion bot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ const customLogger = {
};

//receiver to integrate express with bolt
//The receiver is what allows Bolt to communicate with Slack's servers.
//The receiver listens for incoming HTTP requests from Slack, and then forwards them to the Bolt app.
const receiver = new ExpressReceiver({
signingSecret: process.env.SIGNING_SECRET,
});

// Initialize Bolt app with custom logger and receiver
const app = new App({
token: process.env.BOT_TOKEN,
receiver,
Expand All @@ -30,11 +33,13 @@ const app = new App({
logger: customLogger,
});

//function that starts the server and initializes the command handler
(async () => {
server(receiver);
new CH({
//passing the bolt app to the command handler
app,
featuresDir: path.join(process.cwd(), 'src', 'features'),
//directory where the commands are located
commandsDir: path.join(process.cwd(), 'src', 'commands'),
});
})();
14 changes: 12 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
/*
This is the entry point for the Bolt app. It defines the express server that listens for incoming requests from Slack.
*/

import express from 'express';
import logger from '../command-handler/src/util/logger.js';

// This function creates an express server that listens for incoming requests from Slack.
export default (receiver) => {
const app = express();
const log = logger();
// The port on which the express server will listen for incoming requests.
// The default port is 5000, but it can be overridden by setting the SERVER_PORT environment variable.
const port = process.env.SERVER_PORT || 5000;

// Attach the receiver's request listener to the express server.
app.use('/', receiver.router);

// Define a simple health check route that returns a 200 status code.
app.get('/', (req, res) => {
res.sendStatus(200);
})
});

// Start the express server.
app.listen(port, () => {
log.info(`The Bolt express server is listening on ${port}`)
})
});
}

0 comments on commit d3847d3

Please sign in to comment.