From 4a8625b1fc25a9cedff334f400040fab3de46a3a Mon Sep 17 00:00:00 2001 From: Connor Patterson Date: Wed, 6 Dec 2023 17:21:38 -0500 Subject: [PATCH 1/2] Add support for using events api instead of rtm --- core/router.js | 7 +++++ core/server.js | 20 +++++++++++++ index.js | 77 ++++++++++++++++++++++++++++++-------------------- 3 files changed, 73 insertions(+), 31 deletions(-) diff --git a/core/router.js b/core/router.js index 5b51185..a6d30ab 100644 --- a/core/router.js +++ b/core/router.js @@ -22,6 +22,7 @@ module.exports = class Router { if (this.server) { this.setupCallback(); + this.setupEventWebhook(); } } @@ -160,6 +161,12 @@ module.exports = class Router { }); } + setupEventWebhook() { + this.server.initEventsEndpoint((body) => { + this.handle(body.event); + }) + } + addExtras(data) { const matches = data.text.match(cmdPattern); if (!matches) { diff --git a/core/server.js b/core/server.js index f9621b6..ff62727 100644 --- a/core/server.js +++ b/core/server.js @@ -49,6 +49,26 @@ module.exports = class Server { }) } + initEventsEndpoint(callback) { + this.app.post('/events', (req, res) => { + const body = JSON.parse(req.body) + // check that the verification token matches expected value + if (body.token === config.getKey('slack_verification_token')) { + // immediately respond with a empty 200 response to let + // Slack know the command was received + res.send('') + + callback(body) + } else { + console.error( + 'Sent 500', + 'Check that the verification token matches expected value' + ) + res.sendStatus(500) + } + }) + } + start(callback = () => { }) { const port = config.getKey('port') || 3000 this.app.listen(port, () => { diff --git a/index.js b/index.js index b42b13a..8da7d6c 100644 --- a/index.js +++ b/index.js @@ -77,40 +77,55 @@ class SlackCat { return; } - const rtm = new RTMClient(config.getKey("slack_access_token")); - let router; - - rtm.start(); - - rtm.on("authenticated", data => { - console.log('Incoming RTM authenticated event'); - if (!router) { - const bot = new SlackCatBot(data); - const server = new Server(); - const moduleLoader = new MoudleLoader(bot, server, this.pathToModules); - const modules = moduleLoader.getModules(); - - // Fix me :((((((((((( - bot.setModules(modules); - router = new Router(bot, modules, server); - - server.start(() => { - SlackCatEvents.publish(SlackCatEvents.EventTypes.SetupComplete, []); - }); - } - }); + if (config.getKey("use_events_api") === 'true') { + const bot = new SlackCatBot(data); + const server = new Server(); + const moduleLoader = new MoudleLoader(bot, server, this.pathToModules); + const modules = moduleLoader.getModules(); + + // Fix me :((((((((((( + bot.setModules(modules); + const router = new Router(bot, modules, server); - rtm.on("message", data => { - router.handle(data); - }); + server.start(() => { + SlackCatEvents.publish(SlackCatEvents.EventTypes.SetupComplete, []); + }); + } else { + const rtm = new RTMClient(config.getKey("slack_access_token")); + let router; + + rtm.start(); + + rtm.on("authenticated", data => { + console.log('Incoming RTM authenticated event'); + if (!router) { + const bot = new SlackCatBot(data); + const server = new Server(); + const moduleLoader = new MoudleLoader(bot, server, this.pathToModules); + const modules = moduleLoader.getModules(); + + // Fix me :((((((((((( + bot.setModules(modules); + router = new Router(bot, modules, server); + + server.start(() => { + SlackCatEvents.publish(SlackCatEvents.EventTypes.SetupComplete, []); + }); + } + }); - rtm.on("reaction_added", data => { - router.handle(data); - }); + rtm.on("message", data => { + router.handle(data); + }); - rtm.on("member_joined_channel", data => { - router.handle(data); - }); + rtm.on("reaction_added", data => { + router.handle(data); + }); + + rtm.on("member_joined_channel", data => { + router.handle(data); + }); + } } async runDebugCommand() { From c48a467dfbd8565ba6bec7a0b111b5bf133bb138 Mon Sep 17 00:00:00 2001 From: Connor Patterson Date: Wed, 6 Dec 2023 17:23:18 -0500 Subject: [PATCH 2/2] Empty object for bot data --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8da7d6c..f6ad880 100644 --- a/index.js +++ b/index.js @@ -78,7 +78,8 @@ class SlackCat { } if (config.getKey("use_events_api") === 'true') { - const bot = new SlackCatBot(data); + // TODO: how to get bot data now + const bot = new SlackCatBot({}); const server = new Server(); const moduleLoader = new MoudleLoader(bot, server, this.pathToModules); const modules = moduleLoader.getModules();