@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
187 lines • 7.48 kB
JavaScript
import { __awaiter } from "tslib";
import { AI_AGENTS_FLOW_NAME, KNOWLEDGE_BASE_FLOW_NAME, SEPARATOR, UUID_REGEXP, } from './constants';
import { HtNodeWithContentType, } from './content-fields/hubtype-fields/index';
import FlowClient from './services/flow-client';
import { FlowLocale } from './utils';
export class FlowBuilderApi {
constructor(botonicContext, flow) {
this.botonicContext = botonicContext;
this.flow = flow;
}
static create(botonicContext, localFlow) {
return __awaiter(this, void 0, void 0, function* () {
const client = new FlowClient(botonicContext);
const flow = localFlow ? localFlow : yield client.getFlow();
return new FlowBuilderApi(botonicContext, flow);
});
}
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);
}
getNodeByCampaignId(id) {
const campaign = this.flow.campaigns.find(campaign => campaign.id === id);
if (!campaign)
throw Error(`Campaign with id: '${id}' not found`);
return this.getNodeById(campaign.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`);
return node;
}
getRatingNodeByButtonId(id) {
const ratingNodes = this.flow.nodes.filter(node => node.type === 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);
}
getStartNodeAiAgentFlow() {
const aiAgentFlow = this.flow.flows.find(flow => flow.name === AI_AGENTS_FLOW_NAME);
if (!aiAgentFlow) {
return undefined;
}
return this.getNodeById(aiAgentFlow.start_node_id);
}
getFallbackNode(alternate) {
const fallbackNode = this.flow.nodes.find(node => node.type === 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);
}
getSmartIntentNodes() {
return this.flow.nodes.filter(node => node.type === HtNodeWithContentType.SMART_INTENT);
}
getKeywordNodes() {
return this.flow.nodes.filter(node => node.type === 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) === HtNodeWithContentType.BOT_ACTION;
}
isUUID(str) {
return 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}${SEPARATOR}${payloadJson}`;
}
getFollowUpContentID(id) {
const followUpNode = id
? this.getNodeById(id)
: undefined;
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 : this.getCampaignFlowName(flowId);
}
getCampaignFlowName(campaignId) {
const campaign = this.flow.campaigns.find(campaign => campaign.id === campaignId);
return campaign ? campaign.name : '';
}
getStartNodeKnowledgeBaseFlow() {
const knowledgeBaseFlow = this.flow.flows.find(flow => flow.name === KNOWLEDGE_BASE_FLOW_NAME);
if (!knowledgeBaseFlow) {
return undefined;
}
return this.getNodeById(knowledgeBaseFlow.start_node_id);
}
isKnowledgeBaseEnabled() {
return this.flow.is_knowledge_base_active || false;
}
isAiAgentEnabled() {
return this.flow.is_ai_agent_active || false;
}
shouldCaptureUserInput() {
return !!this.getCaptureUserInputId();
}
getCaptureUserInputId() {
return this.botonicContext.session.captureUserInputNodeId;
}
setCaptureUserInputId(id) {
return __awaiter(this, void 0, void 0, function* () {
yield this.botonicContext.updateBotSession({
captureUserInputNodeId: id,
});
});
}
removeCaptureUserInputId() {
return __awaiter(this, void 0, void 0, function* () {
yield this.botonicContext.updateBotSession({
captureUserInputNodeId: '',
});
});
}
setUserExtraDataVariable(key, value) {
return __awaiter(this, void 0, void 0, function* () {
yield this.botonicContext.updateBotSession({
user: {
extraData: Object.assign(Object.assign({}, this.botonicContext.session.user.extraData), { [key]: value }),
},
});
});
}
getCaptureUserInputNode() {
const captureUserInputId = this.getCaptureUserInputId();
if (!captureUserInputId) {
return undefined;
}
return this.getNodeById(captureUserInputId);
}
getResolvedLocale() {
const flowLocales = this.flow.locales;
const defaultLocaleCode = this.flow.default_locale_code;
return new FlowLocale(this.botonicContext, flowLocales, defaultLocaleCode).resolve();
}
}
//# sourceMappingURL=api.js.map