UNPKG

@botonic/plugin-flow-builder

Version:

Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.

88 lines 3.6 kB
import { __awaiter } from "tslib"; import { BotServerMessageFactory, } from '@botonic/core'; import { EMPTY_PAYLOAD } from '../constants'; import { trackOneContent } from '../tracking'; import { ContentFieldsBase } from './content-fields-base'; import { FlowButton } from './flow-button'; import { HtButtonStyle } from './hubtype-fields/index'; export class FlowText extends ContentFieldsBase { constructor() { super(...arguments); this.text = ''; this.buttons = []; this.buttonStyle = HtButtonStyle.BUTTON; } static fromHubtypeCMS(cmsText, locale, cmsApi) { const newText = new FlowText(cmsText.id); newText.code = cmsText.code; newText.buttonStyle = cmsText.content.buttons_style || HtButtonStyle.BUTTON; newText.text = this.getTextByLocale(locale, cmsText.content.text); newText.buttons = cmsText.content.buttons.map(button => FlowButton.fromHubtypeCMS(button, locale, cmsApi)); newText.followUp = cmsText.follow_up; return newText; } trackFlow(botonicContext) { return __awaiter(this, void 0, void 0, function* () { yield trackOneContent(botonicContext, this); for (const button of this.buttons) { yield button.trackFlow(botonicContext); } }); } processContent(botonicContext) { return __awaiter(this, void 0, void 0, function* () { yield this.trackFlow(botonicContext); return this.filterContent(botonicContext, this); }); } static fromAIAgent(id, textMessage) { if (textMessage.type === 'text') { return BotServerMessageFactory.createText({ text: textMessage.content.text, }); } const buttons = textMessage.content.buttons.map((button, buttonIndex) => { const payload = button.payload || EMPTY_PAYLOAD; const buttonData = 'url' in button ? { id: `${id}-button-${buttonIndex}`, text: button.text, url: button.url, target: button.target, } : { id: `${id}-button-${buttonIndex}`, text: button.text, payload, }; return FlowButton.fromAIAgent(buttonData).addButtonToBotonic({ buttonIndex, buttonStyle: HtButtonStyle.BUTTON, }); }); return BotServerMessageFactory.createButtonMessage({ text: textMessage.content.text, buttons: buttons, }); } toBotonic(botonicContext) { return __awaiter(this, void 0, void 0, function* () { const replacedText = this.replaceVariables(this.text, botonicContext); if (this.buttons.length === 0) { // TODO: Tweak botonic model so it accepts botInteractionId botInteractionId: request.input.botInteractionId, return BotServerMessageFactory.createText({ text: replacedText }); } return BotServerMessageFactory.createButtonMessage({ text: replacedText, buttons: this.buttons.map((button, index) => { return button.addButtonToBotonic({ buttonIndex: index, buttonStyle: HtButtonStyle.BUTTON, botonicContext, }); }), }); }); } } //# sourceMappingURL=flow-text.js.map