@botonic/plugin-flow-builder
Version:
Use Flow Builder to show your contents
98 lines • 4.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContentsByFirstInteraction = void 0;
const tslib_1 = require("tslib");
const constants_1 = require("../constants");
const content_fields_1 = require("../content-fields");
const utils_1 = require("../utils");
const ai_agent_1 = require("./ai-agent");
const knowledge_bases_1 = require("./knowledge-bases");
const payload_1 = require("./payload");
function getContentsByFirstInteraction(context) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { contentID, flowBuilderPlugin, request } = context;
/*
* If the contentID is provided, the firstInteractionContents are obtained even if they are not used
* because when obtain this firstInteractionContents is when the session.flow_thread_id is updated.
* This is needed for example when send a WhatsApp campaign is sent,
* the bot not receives the message because this message is sent directly by the backend
* we expect the bot to respond only with the contents of the contentID and not with the firstInteractionContents.
*/
const firstInteractionContents = yield flowBuilderPlugin.getStartContents();
if (contentID) {
try {
const contentsByContentID = yield flowBuilderPlugin.getContentsByContentID(contentID);
if (contentsByContentID.length > 0) {
return contentsByContentID;
}
}
catch (error) {
console.warn(`The contentID ${contentID} is not found. Returning the firstInteractionContents`);
}
}
/* If the first interaction has a FlowBotAction, it should be the last content
* and avoid to render the match with keywords,intents or knowledge base
*/
if (firstInteractionContents.at(-1) instanceof content_fields_1.FlowBotAction) {
return firstInteractionContents;
}
if (request.input.nluResolution || (0, utils_1.inputHasTextData)(request.input)) {
const contentsByUserInput = yield getContentsByUserInput(context);
return [...firstInteractionContents, ...contentsByUserInput];
}
return firstInteractionContents;
});
}
exports.getContentsByFirstInteraction = getContentsByFirstInteraction;
function getContentsByUserInput(context) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { cmsApi, flowBuilderPlugin, request, resolvedLocale } = context;
const payloadByNlu = (_a = request.input.nluResolution) === null || _a === void 0 ? void 0 : _a.payload;
if (payloadByNlu) {
request.input.payload = payloadByNlu;
const conversationStartId = getConversationStartId(cmsApi);
if (request.input.payload === conversationStartId) {
return [];
}
const contentsByKeywordsOrIntents = yield (0, payload_1.getContentsByPayload)({
cmsApi,
flowBuilderPlugin,
request,
resolvedLocale,
});
const hasRepeatedContent = yield checkRepeatedContents(flowBuilderPlugin, contentsByKeywordsOrIntents);
if (hasRepeatedContent) {
return [];
}
if (contentsByKeywordsOrIntents.length > 0) {
return contentsByKeywordsOrIntents;
}
}
const contentsByKnowledgeBase = yield (0, knowledge_bases_1.getContentsByKnowledgeBase)({
cmsApi,
flowBuilderPlugin,
request,
resolvedLocale,
});
if (contentsByKnowledgeBase.length > 0) {
return contentsByKnowledgeBase;
}
const contentsByAiAgent = yield (0, ai_agent_1.getContentsByAiAgent)(context);
return contentsByAiAgent;
});
}
function getConversationStartId(cmsApi) {
var _a;
const conversationStartId = (_a = cmsApi.flow.flows.find(flow => flow.name === constants_1.MAIN_FLOW_NAME)) === null || _a === void 0 ? void 0 : _a.start_node_id;
return conversationStartId;
}
function checkRepeatedContents(flowBuilderPlugin, contentsByKeywordsOrIntents) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const startContents = yield flowBuilderPlugin.getStartContents();
const contentIds = new Set(contentsByKeywordsOrIntents.map(content => content.id));
const hasRepeatedContent = startContents.some(content => contentIds.has(content.id));
return hasRepeatedContent;
});
}
//# sourceMappingURL=first-interaction.js.map