From c8ef9bd74db954b1744a069a9113e37abb9aa014 Mon Sep 17 00:00:00 2001 From: NichArchA82 <64152648+NichArchA82@users.noreply.github.com> Date: Wed, 18 Sep 2024 05:00:11 +0000 Subject: [PATCH] breaking: switching to bolt express server instead of socketmode --- Dockerfile | 5 +- README.md | 6 +- bot/package-lock.json | 100 ++++++++++-------- bot/src/commands/button.js | 6 +- bot/src/commands/hello.js | 4 +- bot/src/index.js | 16 +-- .../src/cmd-handler/run-command.js | 4 +- command-handler/src/commands/vm.js | 6 +- command-handler/src/events/legacy-command.js | 9 +- server/server.js | 22 ++-- 10 files changed, 99 insertions(+), 79 deletions(-) diff --git a/Dockerfile b/Dockerfile index 95bbae1..892c453 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 0271d96..9721316 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ If you would like to change the source code, or compile the slack Bot yourself: }, "settings": { "event_subscriptions": { + "request_url": "", "bot_events": [ "message.channels", "message.groups", @@ -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": "" }, "org_deploy_enabled": false, - "socket_mode_enabled": true, + "socket_mode_enabled": false, "token_rotation_enabled": false } } diff --git a/bot/package-lock.json b/bot/package-lock.json index a71c201..0a40403 100644 --- a/bot/package-lock.json +++ b/bot/package-lock.json @@ -434,9 +434,9 @@ } }, "node_modules/body-parser": { - "version": "1.20.2", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", - "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", + "version": "1.20.3", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.3.tgz", + "integrity": "sha512-7rAxByjUMqQ3/bHJy7D6OGXvx/MMc4IqBn/X0fcM1QUcAItpZrBEYhWGem+tzXH90c+G01ypMcYJBO9Y30203g==", "license": "MIT", "dependencies": { "bytes": "3.1.2", @@ -447,7 +447,7 @@ "http-errors": "2.0.0", "iconv-lite": "0.4.24", "on-finished": "2.4.1", - "qs": "6.11.0", + "qs": "6.13.0", "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" @@ -689,9 +689,9 @@ "license": "MIT" }, "node_modules/encodeurl": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", - "integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", "license": "MIT", "engines": { "node": ">= 0.8" @@ -869,37 +869,37 @@ "license": "MIT" }, "node_modules/express": { - "version": "4.19.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.19.2.tgz", - "integrity": "sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==", + "version": "4.21.0", + "resolved": "https://registry.npmjs.org/express/-/express-4.21.0.tgz", + "integrity": "sha512-VqcNGcj/Id5ZT1LZ/cfihi3ttTn+NJmkli2eZADigjq29qTlWi/hAQ43t/VLPq8+UX06FCEx3ByOYet6ZFblng==", "license": "MIT", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.2", + "body-parser": "1.20.3", "content-disposition": "0.5.4", "content-type": "~1.0.4", "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", - "encodeurl": "~1.0.2", + "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "etag": "~1.8.1", - "finalhandler": "1.2.0", + "finalhandler": "1.3.1", "fresh": "0.5.2", "http-errors": "2.0.0", - "merge-descriptors": "1.0.1", + "merge-descriptors": "1.0.3", "methods": "~1.1.2", "on-finished": "2.4.1", "parseurl": "~1.3.3", - "path-to-regexp": "0.1.7", + "path-to-regexp": "0.1.10", "proxy-addr": "~2.0.7", - "qs": "6.11.0", + "qs": "6.13.0", "range-parser": "~1.2.1", "safe-buffer": "5.2.1", - "send": "0.18.0", - "serve-static": "1.15.0", + "send": "0.19.0", + "serve-static": "1.16.2", "setprototypeof": "1.2.0", "statuses": "2.0.1", "type-is": "~1.6.18", @@ -911,19 +911,19 @@ } }, "node_modules/express/node_modules/path-to-regexp": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", - "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==", + "version": "0.1.10", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.10.tgz", + "integrity": "sha512-7lf7qcQidTku0Gu3YDPc8DJ1q7OOucfa/BSsIwjuh56VU7katFvuM8hULfkwB3Fns/rsVF7PwPKVw1sl5KQS9w==", "license": "MIT" }, "node_modules/finalhandler": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", - "integrity": "sha512-5uXcUVftlQMFnWC9qu/svkWv3GTd2PfUhK/3PLkYNAe7FbqJMt3515HaxE6eRL74GdsriiwujiawdaB1BpEISg==", + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.1.tgz", + "integrity": "sha512-6BN9trH7bp3qvnrRyzsBz+g3lZxTNZTbVO2EV1CS0WIcDbawYVdYvGflME/9QP0h0pYlCDBCTjYa9nZzMDpyxQ==", "license": "MIT", "dependencies": { "debug": "2.6.9", - "encodeurl": "~1.0.2", + "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "on-finished": "2.4.1", "parseurl": "~1.3.3", @@ -1615,10 +1615,13 @@ } }, "node_modules/merge-descriptors": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", - "integrity": "sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==", - "license": "MIT" + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.3.tgz", + "integrity": "sha512-gaNvAS7TZ897/rVaZ0nMtAyxNyi/pdbjbAwUpFQpN70GqnVfOiXpeUUMKRBmzXaSQ8DdTX4/0ms62r2K+hE6mQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } }, "node_modules/methods": { "version": "1.1.2", @@ -1794,9 +1797,9 @@ } }, "node_modules/path-to-regexp": { - "version": "6.2.2", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.2.tgz", - "integrity": "sha512-GQX3SSMokngb36+whdpRXE+3f9V8UzyAorlYvOGx87ufGHehNTn5lCxrKtLyZ4Yl/wEKnNnr98ZzOwwDZV5ogw==", + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.3.0.tgz", + "integrity": "sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ==", "license": "MIT" }, "node_modules/please-upgrade-node": { @@ -1857,12 +1860,12 @@ "license": "MIT" }, "node_modules/qs": { - "version": "6.11.0", - "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", - "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "version": "6.13.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.13.0.tgz", + "integrity": "sha512-+38qI9SOr8tfZ4QmJNplMUxqjbe7LKvvZgWdExBOmd+egZTtjLB67Gu0HRX3u/XOq7UU2Nx6nsjvS16Z9uwfpg==", "license": "BSD-3-Clause", "dependencies": { - "side-channel": "^1.0.4" + "side-channel": "^1.0.6" }, "engines": { "node": ">=0.6" @@ -2002,9 +2005,9 @@ "license": "MIT" }, "node_modules/send": { - "version": "0.18.0", - "resolved": "https://registry.npmjs.org/send/-/send-0.18.0.tgz", - "integrity": "sha512-qqWzuOjSFOuqPjFe4NOsMLafToQQwBSOEpS+FwEt3A2V3vKubTquT3vmLTQpFgMXp8AlFWFuP1qKaJZOtPpVXg==", + "version": "0.19.0", + "resolved": "https://registry.npmjs.org/send/-/send-0.19.0.tgz", + "integrity": "sha512-dW41u5VfLXu8SJh5bwRmyYUbAoSB3c9uQh6L8h/KtsFREPWpbX1lrljJo186Jc4nmci/sGUZ9a0a0J2zgfq2hw==", "license": "MIT", "dependencies": { "debug": "2.6.9", @@ -2025,6 +2028,15 @@ "node": ">= 0.8.0" } }, + "node_modules/send/node_modules/encodeurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", + "integrity": "sha512-TPJXq8JqFaVYm2CWmPvnP2Iyo4ZSM7/QKcSmuMLDObfpH5fi7RUGmd/rTDf+rut/saiDiQEeVTNgAmJEdAOx0w==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/send/node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -2032,15 +2044,15 @@ "license": "MIT" }, "node_modules/serve-static": { - "version": "1.15.0", - "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.15.0.tgz", - "integrity": "sha512-XGuRDNjXUijsUL0vl6nSD7cwURuzEgglbOaFuZM9g3kwDXOWVTck0jLzjPzGD+TazWbboZYu52/9/XPdUgne9g==", + "version": "1.16.2", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.16.2.tgz", + "integrity": "sha512-VqpjJZKadQB/PEbEwvFdO43Ax5dFBZ2UECszz8bQ7pi7wt//PWe1P6MN7eCnjsatYtBT6EuiClbjSWP2WrIoTw==", "license": "MIT", "dependencies": { - "encodeurl": "~1.0.2", + "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "parseurl": "~1.3.3", - "send": "0.18.0" + "send": "0.19.0" }, "engines": { "node": ">= 0.8.0" diff --git a/bot/src/commands/button.js b/bot/src/commands/button.js index 7202025..fa711d7 100644 --- a/bot/src/commands/button.js +++ b/bot/src/commands/button.js @@ -12,7 +12,7 @@ export default { }) }, - run: ({ response, message }) => { + run: ({ response, event }) => { response({ blocks: [ @@ -20,7 +20,7 @@ export default { "type": "section", "text": { "type": "mrkdwn", - "text": `Hey there <@${message.user}>!` + "text": `Hey there <@${event.user}>!` }, "accessory": { "type": "button", @@ -32,7 +32,7 @@ export default { } } ], - text: `Hey there <@${message.user}>!` + text: `Hey there <@${event.user}>!` }) } } \ No newline at end of file diff --git a/bot/src/commands/hello.js b/bot/src/commands/hello.js index be7afaa..ea9b3da 100644 --- a/bot/src/commands/hello.js +++ b/bot/src/commands/hello.js @@ -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}>!` }) } } \ No newline at end of file diff --git a/bot/src/index.js b/bot/src/index.js index ed8f606..a84d2c4 100644 --- a/bot/src/index.js +++ b/bot/src/index.js @@ -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(); @@ -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'), diff --git a/command-handler/src/cmd-handler/run-command.js b/command-handler/src/cmd-handler/run-command.js index 7526b45..8c9a46e 100644 --- a/command-handler/src/cmd-handler/run-command.js +++ b/command-handler/src/cmd-handler/run-command.js @@ -2,7 +2,7 @@ export default async ({ commandName, handler, app, - message, + event, args, say }) => { @@ -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 }); }; \ No newline at end of file diff --git a/command-handler/src/commands/vm.js b/command-handler/src/commands/vm.js index 1975bb8..4fe7075 100644 --- a/command-handler/src/commands/vm.js +++ b/command-handler/src/commands/vm.js @@ -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", diff --git a/command-handler/src/events/legacy-command.js b/command-handler/src/events/legacy-command.js index 3a98013..0755067 100644 --- a/command-handler/src/events/legacy-command.js +++ b/command-handler/src/events/legacy-command.js @@ -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); @@ -15,9 +16,9 @@ export default function command(app, handler) { commandName, handler, app, - message, + event, say, args }) - }) + }); } \ No newline at end of file diff --git a/server/server.js b/server/server.js index f2bc653..48e75b7 100644 --- a/server/server.js +++ b/server/server.js @@ -1,14 +1,18 @@ import express from 'express'; import logger from '../command-handler/src/util/logger.js'; -const app = express(); -const log = logger(); -const port = process.env.SERVER_PORT || 5000; +export default (receiver) => { + const app = express(); + const log = logger(); + const port = process.env.SERVER_PORT || 5000; -app.get('/', (req, res) => { - res.sendStatus(200); -}) + app.use('/', receiver.router); -app.listen(port, () => { - log.info(`The server is listening on port ${port}`) -}) \ No newline at end of file + app.get('/', (req, res) => { + res.sendStatus(200); + }) + + app.listen(port, () => { + log.info(`The Bolt express server is listening on ${port}`) + }) +} \ No newline at end of file