UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

216 lines 9.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowBuilderApi = void 0; const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const constants_1 = require("./constants"); const hubtype_fields_1 = require("./content-fields/hubtype-fields"); const types_1 = require("./types"); class FlowBuilderApi { constructor() { } static create(options) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const newApi = new FlowBuilderApi(); newApi.url = options.url; newApi.request = options.request; // TODO: Refactor later to combine logic from `FlowBuilderApi.create`, `resolveFlowUrl` and `getAccessToken` to be in one place if (process.env.NODE_ENV === types_1.ProcessEnvNodeEnvs.DEVELOPMENT) { yield newApi.updateSessionWithUserInfo(options.accessToken); } const updatedRequest = newApi.request; newApi.flowUrl = options.flowUrl.replace('{bot_id}', updatedRequest.session.bot.id); newApi.flow = (_a = options.flow) !== null && _a !== void 0 ? _a : (yield newApi.getFlow(options.accessToken)); return newApi; }); } getFlow(token) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { data } = yield axios_1.default.get(this.flowUrl, { headers: { Authorization: `Bearer ${token}` }, }); return data; }); } updateSessionWithUserInfo(token) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const url = `${this.url}/v1/flow_builder/user_info/`; const response = yield axios_1.default.get(url, { headers: { Authorization: `Bearer ${token}` }, }); this.request.session.organization_id = response.data.organization_id; this.request.session.bot.id = response.data.bot_id; }); } getNodeByFlowId(id) { const subFlow = this.flow.flows.find(subFlow => subFlow.id === id); if (!subFlow) throw Error(`SubFlow with id: '${id}' not found`); return this.getNodeById(subFlow.start_node_id); } getNodeById(id) { const node = this.flow.nodes.find(node => node.id === id); if (!node) console.error(`Node with id: '${id}' not found`); if ((node === null || node === void 0 ? void 0 : node.type) === hubtype_fields_1.HtNodeWithoutContentType.GO_TO_FLOW) { return this.getNodeByFlowId(node.content.flow_id); } return node; } getRatingNodeByButtonId(id) { const ratingNodes = this.flow.nodes.filter(node => node.type === hubtype_fields_1.HtNodeWithContentType.RATING); const ratingNode = ratingNodes.find(node => node.content.buttons.some(button => button.id === id)); if (!ratingNode) { throw Error(`Rating node with button id: '${id}' not found`); } return ratingNode; } getRatingButtonById(ratingNode, id) { const ratingButton = ratingNode.content.buttons.find(button => button.id === id); if (!ratingButton) { throw Error(`Rating button with id: '${id}' not found`); } return ratingButton; } getNodeByContentID(contentID) { const content = this.flow.nodes.find(node => 'code' in node ? node.code === contentID : false); if (!content) throw Error(`Node with contentID: '${contentID}' not found`); return content; } getStartNode() { const startNodeId = this.flow.start_node_id; if (!startNodeId) throw new Error('Start node id must be defined'); return this.getNodeById(startNodeId); } getFallbackNode(alternate) { const fallbackNode = this.flow.nodes.find(node => node.type === hubtype_fields_1.HtNodeWithContentType.FALLBACK); if (!fallbackNode) { throw new Error('Fallback node must be defined'); } const fallbackFirstMessage = fallbackNode.content.first_message; if (!fallbackFirstMessage) { throw new Error('Fallback 1st message must be defined'); } const fallbackSecondMessage = fallbackNode.content.second_message; if (!fallbackSecondMessage) { return this.getNodeById(fallbackFirstMessage.id); } return alternate ? this.getNodeById(fallbackFirstMessage.id) : this.getNodeById(fallbackSecondMessage.id); } getKnowledgeBaseConfig() { const fallbackNode = this.flow.nodes.find(node => node.type === hubtype_fields_1.HtNodeWithContentType.FALLBACK); return fallbackNode ? { followup: fallbackNode.content.knowledge_base_followup, isActive: fallbackNode.content.is_knowledge_base_active || false, } : undefined; } getSmartIntentNodes() { return this.flow.nodes.filter(node => node.type === hubtype_fields_1.HtNodeWithContentType.SMART_INTENT); } getKeywordNodes() { return this.flow.nodes.filter(node => node.type === hubtype_fields_1.HtNodeWithContentType.KEYWORD); } getPayload(target) { if (!target) { return undefined; } return target.id; } isBotAction(id) { if (!this.isUUID(id)) { return false; } const node = this.getNodeById(id); return (node === null || node === void 0 ? void 0 : node.type) === hubtype_fields_1.HtNodeWithContentType.BOT_ACTION; } isUUID(str) { return constants_1.UUID_REGEXP.test(str); } createPayloadWithParams(botActionNode) { var _a; const payloadId = botActionNode.content.payload_id; const payloadNode = this.getNodeById(payloadId); const customParams = JSON.parse(botActionNode.content.payload_params || '{}'); const followUpContentID = this.getFollowUpContentID((_a = botActionNode.follow_up) === null || _a === void 0 ? void 0 : _a.id); const payloadJson = JSON.stringify(Object.assign(Object.assign({}, customParams), { followUpContentID })); return `${payloadNode.content.payload}${constants_1.SEPARATOR}${payloadJson}`; } getFollowUpContentID(id) { const followUpNode = id ? this.getNodeById(id) : undefined; if ((followUpNode === null || followUpNode === void 0 ? void 0 : followUpNode.type) === hubtype_fields_1.HtNodeWithoutContentType.GO_TO_FLOW) { return this.getNodeById(followUpNode === null || followUpNode === void 0 ? void 0 : followUpNode.content.flow_id) .code; } else { return followUpNode === null || followUpNode === void 0 ? void 0 : followUpNode.code; } } getFlowName(flowId) { const flow = this.flow.flows.find(flow => flow.id === flowId); return flow ? flow.name : ''; } getStartNodeKnowledgeBaseFlow() { const knowledgeBaseFlow = this.flow.flows.find(flow => flow.name === constants_1.KNOWLEDGE_BASE_FLOW_NAME); if (!knowledgeBaseFlow) { return undefined; } return this.getNodeById(knowledgeBaseFlow.start_node_id); } getStartNodeAiAgentFlow() { const aiAgentFlow = this.flow.flows.find(flow => flow.name === constants_1.AI_AGENTS_FLOW_NAME); if (!aiAgentFlow) { return undefined; } return this.getNodeById(aiAgentFlow.start_node_id); } isKnowledgeBaseEnabled() { return this.flow.is_knowledge_base_active || false; } isAiAgentEnabled() { return this.flow.is_ai_agent_active || false; } getResolvedLocale() { const systemLocale = this.request.getSystemLocale(); const locale = this.resolveAsLocale(systemLocale); if (locale) { return locale; } const language = this.resolveAsLanguage(systemLocale); if (language) { this.request.setSystemLocale(language); return language; } const defaultLocale = this.resolveAsDefaultLocale(); this.request.setSystemLocale(defaultLocale); return defaultLocale; } resolveAsLocale(locale) { if (this.flow.locales.find(flowLocale => flowLocale === locale)) { return locale; } return undefined; } resolveAsLanguage(locale) { const language = locale === null || locale === void 0 ? void 0 : locale.split('-')[0]; if (language && this.flow.locales.find(flowLocale => flowLocale === language)) { console.log(`locale: ${locale} has been resolved as ${language}`); return language; } return undefined; } resolveAsDefaultLocale() { console.log(`Resolve locale with default locale: ${this.flow.default_locale_code}`); return this.flow.default_locale_code || 'en'; } } exports.FlowBuilderApi = FlowBuilderApi; //# sourceMappingURL=api.js.map