UNPKG

3.76 kBJavaScriptView Raw
1"use strict";
2var __rest = (this && this.__rest) || function (s, e) {
3 var t = {};
4 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5 t[p] = s[p];
6 if (s != null && typeof Object.getOwnPropertySymbols === "function")
7 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9 t[p[i]] = s[p[i]];
10 }
11 return t;
12};
13var __importDefault = (this && this.__importDefault) || function (mod) {
14 return (mod && mod.__esModule) ? mod : { "default": mod };
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17const path_1 = __importDefault(require("path"));
18const merge_1 = __importDefault(require("lodash/merge"));
19const express_1 = require("@bottender/express");
20const LineBot_1 = __importDefault(require("./line/LineBot"));
21const MessengerBot_1 = __importDefault(require("./messenger/MessengerBot"));
22const SlackBot_1 = __importDefault(require("./slack/SlackBot"));
23const TelegramBot_1 = __importDefault(require("./telegram/TelegramBot"));
24const ViberBot_1 = __importDefault(require("./viber/ViberBot"));
25const WhatsappBot_1 = __importDefault(require("./whatsapp/WhatsappBot"));
26const getBottenderConfig_1 = __importDefault(require("./shared/getBottenderConfig"));
27const getSessionStore_1 = __importDefault(require("./getSessionStore"));
28const BOT_MAP = {
29 messenger: MessengerBot_1.default,
30 line: LineBot_1.default,
31 slack: SlackBot_1.default,
32 telegram: TelegramBot_1.default,
33 viber: ViberBot_1.default,
34 whatsapp: WhatsappBot_1.default,
35};
36function initializeServer({ isConsole, config, } = {}) {
37 const bottenderConfig = getBottenderConfig_1.default();
38 const { initialState, plugins, channels } = merge_1.default(bottenderConfig, config);
39 const sessionStore = getSessionStore_1.default();
40 const Entry = require(path_1.default.resolve('index.js'));
41 let ErrorEntry;
42 try {
43 ErrorEntry = require(path_1.default.resolve('_error.js'));
44 }
45 catch (err) { }
46 function initializeBot(bot) {
47 if (initialState) {
48 bot.setInitialState(initialState);
49 }
50 if (plugins) {
51 plugins.forEach((plugin) => {
52 bot.use(plugin);
53 });
54 }
55 bot.onEvent(Entry);
56 if (ErrorEntry) {
57 bot.onError(ErrorEntry);
58 }
59 }
60 if (isConsole) {
61 const ConsoleBot = require('./console/ConsoleBot').default;
62 const bot = new ConsoleBot({
63 fallbackMethods: true,
64 sessionStore,
65 });
66 initializeBot(bot);
67 bot.createRuntime();
68 }
69 else {
70 let server;
71 Object.entries(channels || {})
72 .filter(([, { enabled }]) => enabled)
73 .map((_a) => {
74 var [channel, _b] = _a, { path: webhookPath } = _b, channelConfig = __rest(_b, ["path"]);
75 const ChannelBot = BOT_MAP[channel];
76 const channelBot = new ChannelBot(Object.assign(Object.assign({}, channelConfig), { sessionStore }));
77 initializeBot(channelBot);
78 return { channel, webhookPath, bot: channelBot };
79 })
80 .forEach(({ channel, webhookPath, bot }) => {
81 const routePath = webhookPath || `/webhooks/${channel}`;
82 if (server) {
83 express_1.registerRoutes(server, bot, { path: routePath });
84 }
85 else {
86 server = express_1.createServer(bot, {
87 path: routePath,
88 });
89 }
90 });
91 return server;
92 }
93}
94exports.default = initializeServer;
95//# sourceMappingURL=initializeServer.js.map
\No newline at end of file