forked from LittleNa1000/process-announcer-line-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
31 lines (27 loc) · 942 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import * as line from "@line/bot-sdk";
import * as express from "express";
import * as dotenv from "dotenv";
import { handleEvent } from "./src/handleEvent";
import { initSystem } from "./src/init";
import { logger } from "./src/logger";
const env = dotenv.config().parsed;
const app = express();
const lineConfig = {
channelAccessToken: env!.NODE_ENV === "development" ? env!.ACCESS_TOKEN_DEMO : env!.ACCESS_TOKEN,
channelSecret: env!.NODE_ENV === "development" ? env!.SECRET_TOKEN_DEMO : env!.SECRET_TOKEN,
};
app.post("/webhook", line.middleware(lineConfig), async (req, res) => {
try {
const events = req.body.events;
return events.length > 0
? await events.map((item) => handleEvent(item))
: res.status(200).send("OK");
} catch (err) {
logger.error(err.stack);
res.status(500).end();
}
});
app.listen(env!.PORT, async () => {
logger.info(`On port ${env!.PORT}`);
initSystem(lineConfig);
});