UNPKG

bottender

Version:

A framework for building conversational user interfaces.

91 lines 3.27 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cleanChannelBots = void 0; const path_1 = __importDefault(require("path")); const invariant_1 = __importDefault(require("invariant")); const lodash_1 = require("lodash"); const messaging_api_common_1 = require("messaging-api-common"); const __1 = require(".."); const getBottenderConfig_1 = __importDefault(require("./getBottenderConfig")); let channelBots = []; function cleanChannelBots() { channelBots = []; } exports.cleanChannelBots = cleanChannelBots; function getChannelBots() { if (channelBots.length > 0) { return channelBots; } const bottenderConfig = getBottenderConfig_1.default(); const { initialState, plugins, channels = {}, } = lodash_1.merge(bottenderConfig); const sessionStore = __1.getSessionStore(); const Entry = require(path_1.default.resolve('index.js')); let ErrorEntry; try { ErrorEntry = require(path_1.default.resolve('_error.js')); } catch (err) { } function initializeBot(bot) { if (initialState) { bot.setInitialState(initialState); } if (plugins) { plugins.forEach((plugin) => { bot.use(plugin); }); } bot.onEvent(Entry); if (ErrorEntry) { bot.onError(ErrorEntry); } } channelBots = Object.entries(channels) .filter(([, { enabled }]) => enabled) .map((_a) => { var [channel, _b] = _a, { path: webhookPath, sync, onRequest, connector } = _b, connectorConfig = __rest(_b, ["path", "sync", "onRequest", "connector"]); let channelConnector; if ([ 'messenger', 'line', 'telegram', 'slack', 'viber', 'whatsapp', ].includes(channel)) { const ChannelConnector = require(`../${channel}/${messaging_api_common_1.pascalcase(channel)}Connector`).default; channelConnector = new ChannelConnector(connectorConfig); } else { invariant_1.default(connector, `The connector of ${channel} is missing.`); channelConnector = connector; } const channelBot = new __1.Bot({ sessionStore, sync, onRequest, connector: channelConnector, }); initializeBot(channelBot); return { webhookPath: webhookPath || `/webhooks/${channel}`, bot: channelBot, }; }); return channelBots; } exports.default = getChannelBots; //# sourceMappingURL=getChannelBots.js.map