-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (54 loc) · 1.71 KB
/
index.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { MessengerBot, MongoSessionStore } = require("bottender");
const { createServer } = require("bottender/express");
const chatbaseMiddleware = require("bottender-chatbase/express");
const MongoClient = require("mongodb").MongoClient;
const cors = require("cors");
const expressMongoDb = require("express-mongo-db");
// read configs
const config = require("config");
const PORT = parseInt(process.env.PORT) || 5000;
// import backend api routes
const routes = require("./api");
// import event handler for bot server
const { createEventHandler } = require("./handlers");
// import some constants
const { INIT_STATE } = require("./constants");
async function main() {
// connect to database
const db = await MongoClient.connect(config.MONGODB_URI);
/* Setup bot servr */
const bot = new MessengerBot({
accessToken: config.ACCESS_TOKEN,
appSecret: config.APP_SECRET,
sessionStore: new MongoSessionStore(config.MONGODB_URI),
});
bot.setInitialState(INIT_STATE);
// set event handler
bot.onEvent(createEventHandler(db));
const server = createServer(bot, {
verifyToken: config.VERIFY_TOKEN,
webhookMiddleware: chatbaseMiddleware(bot, {
apiKey: config.CHATBASE_API_KEY,
platform: "Facebook",
}),
});
/* Setup api server */
// set CORS
if (config.CORS_ANY !== "FALSE" && config.CORS_ANY !== false) {
server.use(cors());
} else {
server.use(
cors({
origin: [/\.goodjob\.life$/, /goodjoblife.github.io$/, /localhost/],
})
);
}
// setup api routes
server.use(expressMongoDb(config.MONGODB_URI));
server.use("/api", routes);
// start server
server.listen(PORT, () => {
console.log(`server is running on ${PORT} port...`);
});
}
main();