Skip to content

Commit

Permalink
breaking: switching to bolt express server instead of socketmode
Browse files Browse the repository at this point in the history
  • Loading branch information
NichArchA82 committed Sep 18, 2024
1 parent 33791d5 commit c8ef9bd
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 79 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ WORKDIR /app/command-handler
RUN npm ci
RUN npm link

# Install dependencies and link server globally
# Install dependencies for express server
WORKDIR /app/server
RUN npm ci
RUN npm link

# Go to bot directory, install dependencies, and link both command-handler and server
WORKDIR /app/bot
RUN npm ci
RUN npm link command-handler server
RUN npm link command-handler

# Expose port 5000
EXPOSE 5000
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ If you would like to change the source code, or compile the slack Bot yourself:
},
"settings": {
"event_subscriptions": {
"request_url": "<your server's url>",
"bot_events": [
"message.channels",
"message.groups",
Expand All @@ -74,10 +75,11 @@ If you would like to change the source code, or compile the slack Bot yourself:
]
},
"interactivity": {
"is_enabled": true
"is_enabled": true,
"request_url": "<your server's url>"
},
"org_deploy_enabled": false,
"socket_mode_enabled": true,
"socket_mode_enabled": false,
"token_rotation_enabled": false
}
}
Expand Down
100 changes: 56 additions & 44 deletions bot/package-lock.json

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

6 changes: 3 additions & 3 deletions bot/src/commands/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export default {
})
},

run: ({ response, message }) => {
run: ({ response, event }) => {

response({
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Hey there <@${message.user}>!`
"text": `Hey there <@${event.user}>!`
},
"accessory": {
"type": "button",
Expand All @@ -32,7 +32,7 @@ export default {
}
}
],
text: `Hey there <@${message.user}>!`
text: `Hey there <@${event.user}>!`
})
}
}
4 changes: 2 additions & 2 deletions bot/src/commands/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const delay = (ms) => {
export default {
description: 'Replies with Hello',

run: async ({ response, message }) => {
run: async ({ response, event }) => {
await delay(5000);

response({
text: `Hey there <@${message.user}>!`
text: `Hey there <@${event.user}>!`
})
}
}
16 changes: 9 additions & 7 deletions bot/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pkg from '@slack/bolt'
const { App, LogLevel } = pkg;
const { App, ExpressReceiver, LogLevel } = pkg;
import CH from 'command-handler';
import path from 'path';
import 'dotenv/config';
import 'server';
import server from '../../server/server.js';
import logger from 'command-handler/src/util/logger.js';

const log = logger();
Expand All @@ -17,19 +17,21 @@ const customLogger = {
error: (message) => log.error(message),
};

//receiver to integrate express with bolt
const receiver = new ExpressReceiver({
signingSecret: process.env.SIGNING_SECRET,
});

const app = new App({
token: process.env.BOT_TOKEN,
signingSecret: process.env.SIGNING_SECRET,
socketMode: true,
receiver,
appToken: process.env.APP_TOKEN,
LogLevel: LogLevel.DEBUG,
logger: customLogger,
});

(async () => {
await app.start(process.env.BOLT_PORT || 3000);
log.info('Bot is ready');

server(receiver);
new CH({
app,
featuresDir: path.join(process.cwd(), 'src', 'features'),
Expand Down
4 changes: 2 additions & 2 deletions command-handler/src/cmd-handler/run-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default async ({
commandName,
handler,
app,
message,
event,
args,
say
}) => {
Expand All @@ -21,5 +21,5 @@ export default async ({
say(obj)
};

command.run({ handler, app, message, response, text, args });
command.run({ handler, app, event, response, text, args });
};
6 changes: 3 additions & 3 deletions command-handler/src/commands/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export default {
}
},

run: async ({ message, app }) => {
run: async ({ event, app }) => {
app.client.chat.postEphemeral({
channel: `${message.channel}`,
user: `${message.user}`,
channel: `${event.channel}`,
user: `${event.user}`,
blocks: [
{
"type": "section",
Expand Down
9 changes: 5 additions & 4 deletions command-handler/src/events/legacy-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import runCommand from '../cmd-handler/run-command.js';

export default function command(app, handler) {
const prefix = '!'
app.message(async ({ message, say }) => {
const content = message.text;
app.event('message', async ({ event, say }) => {

const content = event.text;
if (!content.startsWith(prefix)) return;

const args = content.slice(prefix.length).trim().split(/ +/g);
Expand All @@ -15,9 +16,9 @@ export default function command(app, handler) {
commandName,
handler,
app,
message,
event,
say,
args
})
})
});
}
Loading

0 comments on commit c8ef9bd

Please sign in to comment.