UNPKG

@botonic/plugin-flow-builder

Version:

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

133 lines 5.11 kB
import { __awaiter } from "tslib"; import { BotServerMessageFactory } from '@botonic/core'; import { SOURCE_INFO_SEPARATOR } from '../constants'; import { ContentFieldsBase } from './content-fields-base'; import { FlowWebview } from './flow-webview'; import { HtNodeWithContentType, } from './hubtype-fields'; export class FlowButton extends ContentFieldsBase { constructor() { super(...arguments); this.text = ''; } static fromHubtypeCMS(cmsButton, locale, cmsApi) { const urlId = this.getUrlId(cmsButton, locale); const newButton = new FlowButton(cmsButton.id); newButton.text = this.getTextByLocale(locale, cmsButton.text); if (cmsButton.target) { const webview = this.getTargetWebview(cmsApi, cmsButton.target.id); if (webview) { newButton.flowWebview = webview; const params = this.getWebviewParams(webview, cmsApi); newButton.webview = { name: webview.webviewComponentName }; newButton.params = params; } else { newButton.payload = cmsApi.getPayload(cmsButton.target); } } if (cmsButton.url && urlId) { const urlNode = cmsApi.getNodeById(urlId); newButton.url = urlNode.content.url; } return newButton; } static getWebviewParams(webview, cmsApi) { const params = { webview_id: webview.webviewTargetId, }; const exitSuccessContentID = this.getExitSuccessContentID(webview, cmsApi); if (exitSuccessContentID) { params.exitSuccessContentID = exitSuccessContentID; } return params; } static getExitSuccessContentID(webview, cmsApi) { var _a, _b; const webviewSuccessExit = (_a = webview.exits) === null || _a === void 0 ? void 0 : _a.find(exit => exit.name === 'Success'); const exitSuccessId = (_b = webviewSuccessExit === null || webviewSuccessExit === void 0 ? void 0 : webviewSuccessExit.target) === null || _b === void 0 ? void 0 : _b.id; if (!exitSuccessId) { return undefined; } const exitNode = cmsApi.getNodeById(exitSuccessId); return exitNode.code; } static fromAIAgent(button) { const newButton = new FlowButton(button.id); newButton.text = button.text; if (button.url) { newButton.url = button.url; newButton.target = button.target; } else { newButton.payload = button.payload; } return newButton; } static fromRating(button) { var _a; const newButton = new FlowButton(button.id); newButton.text = button.text; newButton.payload = button.payload; newButton.target = (_a = button.target) === null || _a === void 0 ? void 0 : _a.id; newButton.ratingValue = button.value; return newButton; } static getUrlId(cmsButton, locale) { var _a; return (_a = cmsButton.url.find(url => url.locale === locale)) === null || _a === void 0 ? void 0 : _a.id; } static getTargetWebview(cmsApi, targetId) { const targetNode = cmsApi.getNodeById(targetId); if (targetNode.type !== HtNodeWithContentType.WEBVIEW) { return undefined; } return FlowWebview.fromHubtypeCMS(targetNode); } trackFlow(botonicContext) { return __awaiter(this, void 0, void 0, function* () { if (this.flowWebview) { yield this.flowWebview.trackFlow(botonicContext); } }); } processContent() { return __awaiter(this, void 0, void 0, function* () { return; }); } addButtonToBotonic({ buttonIndex, buttonStyle: _buttonStyle, botonicContext, }) { // TODO: Support quick replies // if (buttonStyle === HtButtonStyle.QUICK_REPLY) { // return MessageFactory.createReplyContent( // this.text, // this.getButtonPayload(buttonIndex) as string // ) // } if (this.webview) { if (!botonicContext) { throw new Error('FlowButton.addButtonToBotonic: botonicContext is required for webview buttons'); } return BotServerMessageFactory.createWebviewButton({ title: this.text, webview: this.webview.name, params: this.params, }, botonicContext); } if (this.url) { return BotServerMessageFactory.createUrlButton({ title: this.text, url: this.url, }); } return BotServerMessageFactory.createPostbackButton({ title: this.text, payload: this.getButtonPayload(buttonIndex) || '', }); } getButtonPayload(buttonIndex) { return this.payload ? `${this.payload}${SOURCE_INFO_SEPARATOR}${buttonIndex}` : undefined; } } //# sourceMappingURL=flow-button.js.map