diff --git a/app.js b/app.js index c2a6f75..72fc432 100644 --- a/app.js +++ b/app.js @@ -1,34 +1,16 @@ -const { promisify } = require("util"); -const { App, ExpressReceiver } = require("@slack/bolt"); -const daraz = require("./"); +const { App: BoltApp } = require("@slack/bolt"); -class Receiver extends ExpressReceiver { +class App extends BoltApp { /** - * @param {import("express").Request} req - * @param {import("express").Response} res + * @param {import("@slack/bolt").AppOptions} opt */ - requestHandler(req, res) { - // NOTE: See also https://api.slack.com/events-api#errors - res.header("x-slack-no-retry", "1"); - if (req.headers["x-slack-retry-reason"] === "http_timeout") - return promisify(res.end)(); - - return super.requestHandler(req, res); + constructor(opt) { + super(opt); + const scripts = require("glob").sync("./scripts/*.js").map(require); + scripts.forEach((script) => { + if (Array.isArray(script)) return this.message(...script); + if (script instanceof Function) return script(this); + }); } } - -const receiver = new Receiver({ - signingSecret: process.env.SLACK_SIGNING_SECRET, -}); - -const app = daraz( - new App({ - token: process.env.SLACK_BOT_TOKEN, - receiver, - }) -); - -(async () => { - await app.start(process.env.PORT || 8080); - console.log("Daraz-san ⚡ running"); -})(); +module.exports = App; diff --git a/index.js b/index.js deleted file mode 100644 index 5efe3e9..0000000 --- a/index.js +++ /dev/null @@ -1,9 +0,0 @@ -const scripts = require("glob").sync("./scripts/*.js").map(require); - -module.exports = (app) => { - scripts.forEach((script) => { - if (Array.isArray(script)) return app.message(...script); - if (script instanceof Function) return script(app); - }); - return app; -}; diff --git a/main.js b/main.js new file mode 100644 index 0000000..6b52068 --- /dev/null +++ b/main.js @@ -0,0 +1,18 @@ +const App = require("./app"); +const Receiver = require("./receiver"); + +async function main() { + const receiver = new Receiver({ + signingSecret: process.env.SLACK_SIGNING_SECRET, + }); + + const app = new App({ + token: process.env.SLACK_BOT_TOKEN, + receiver, + }); + + await app.start(process.env.PORT || 8080); + console.log("Daraz-san ⚡ running"); +} + +if (require.main === module) main(); diff --git a/package.json b/package.json index df2376e..bf1359d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "daraz-san", "version": "1.0.0", "description": "Daraz-san ⚡", - "main": "index.js", "author": "Kohei Watanabe ", "contributors": [ "SAKAGUCHI Takashi ", @@ -12,7 +11,7 @@ "license": "MIT", "private": true, "scripts": { - "start": "node app.js" + "start": "node main.js" }, "dependencies": { "@slack/bolt": "2.x", diff --git a/receiver.js b/receiver.js new file mode 100644 index 0000000..66eb0d3 --- /dev/null +++ b/receiver.js @@ -0,0 +1,18 @@ +const { promisify } = require("util"); +const { ExpressReceiver } = require("@slack/bolt"); + +class Receiver extends ExpressReceiver { + /** + * @param {import("express").Request} req + * @param {import("express").Response} res + */ + requestHandler(req, res) { + // NOTE: See also https://api.slack.com/events-api#errors + res.header("x-slack-no-retry", "1"); + if (req.headers["x-slack-retry-reason"] === "http_timeout") + return promisify(res.end)(); + + return super.requestHandler(req, res); + } +} +module.exports = Receiver;