mirror of
https://github.com/kou029w/daraz-san.git
synced 2025-01-19 00:18:01 +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: BoltApp } = require("@slack/bolt");
|
||||||
const { App, ExpressReceiver } = require("@slack/bolt");
|
|
||||||
const daraz = require("./");
|
|
||||||
|
|
||||||
class Receiver extends ExpressReceiver {
|
class App extends BoltApp {
|
||||||
/**
|
/**
|
||||||
* @param {import("express").Request} req
|
* @param {import("@slack/bolt").AppOptions} opt
|
||||||
* @param {import("express").Response} res
|
|
||||||
*/
|
*/
|
||||||
requestHandler(req, res) {
|
constructor(opt) {
|
||||||
// NOTE: See also https://api.slack.com/events-api#errors
|
super(opt);
|
||||||
res.header("x-slack-no-retry", "1");
|
const scripts = require("glob").sync("./scripts/*.js").map(require);
|
||||||
if (req.headers["x-slack-retry-reason"] === "http_timeout")
|
scripts.forEach((script) => {
|
||||||
return promisify(res.end)();
|
if (Array.isArray(script)) return this.message(...script);
|
||||||
|
if (script instanceof Function) return script(this);
|
||||||
return super.requestHandler(req, res);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
module.exports = App;
|
||||||
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");
|
|
||||||
})();
|
|
||||||
|
|
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",
|
"name": "daraz-san",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Daraz-san ⚡",
|
"description": "Daraz-san ⚡",
|
||||||
"main": "index.js",
|
|
||||||
"author": "Kohei Watanabe <kou029w@gmail.com>",
|
"author": "Kohei Watanabe <kou029w@gmail.com>",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"SAKAGUCHI Takashi <takashi.sakaguchi@ummm.info>",
|
"SAKAGUCHI Takashi <takashi.sakaguchi@ummm.info>",
|
||||||
|
@ -12,7 +11,7 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node app.js"
|
"start": "node main.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@slack/bolt": "2.x",
|
"@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