mirror of
https://github.com/kou029w/daraz-san.git
synced 2025-01-18 08:05:04 +00:00
refactor
This commit is contained in:
parent
4e9ff3cb8f
commit
cc8db81930
5 changed files with 48 additions and 40 deletions
40
app.js
40
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;
|
||||
|
|
9
index.js
9
index.js
|
@ -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;
|
||||
};
|
18
main.js
Normal file
18
main.js
Normal file
|
@ -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();
|
|
@ -2,7 +2,6 @@
|
|||
"name": "daraz-san",
|
||||
"version": "1.0.0",
|
||||
"description": "Daraz-san ⚡",
|
||||
"main": "index.js",
|
||||
"author": "Kohei Watanabe <kou029w@gmail.com>",
|
||||
"contributors": [
|
||||
"SAKAGUCHI Takashi <takashi.sakaguchi@ummm.info>",
|
||||
|
@ -12,7 +11,7 @@
|
|||
"license": "MIT",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node app.js"
|
||||
"start": "node main.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@slack/bolt": "2.x",
|
||||
|
|
18
receiver.js
Normal file
18
receiver.js
Normal file
|
@ -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;
|
Loading…
Add table
Reference in a new issue