@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
106 lines • 4.54 kB
JavaScript
import { __awaiter } from "tslib";
import { ACCESS_TOKEN_VARIABLE_KEY, VARIABLE_PATTERN_GLOBAL, } from '../constants';
import { ContentFilterExecutor } from '../filters';
import { getFlowBuilderPlugin } from '../utils/get-flow-builder-plugin';
export class ContentFieldsBase {
constructor(id) {
this.id = id;
this.code = '';
}
filterContent(botonicContext, content) {
return __awaiter(this, void 0, void 0, function* () {
const flowBuilderPlugin = getFlowBuilderPlugin(botonicContext.plugins);
const contentFilters = flowBuilderPlugin.contentFilters;
const contentFilterExecutor = new ContentFilterExecutor({
filters: contentFilters,
});
const filteredContent = yield contentFilterExecutor.filter(botonicContext, content);
return filteredContent;
});
}
static getTextByLocale(locale, text) {
var _a;
const result = text.find(t => t.locale === locale);
return (_a = result === null || result === void 0 ? void 0 : result.message) !== null && _a !== void 0 ? _a : '';
}
static getAssetByLocale(locale, asset) {
var _a;
const result = asset.find(i => i.locale === locale);
return (_a = result === null || result === void 0 ? void 0 : result.file) !== null && _a !== void 0 ? _a : '';
}
static getImageByLocale(locale, image) {
var _a;
const result = image.find(i => i.locale === locale);
return (_a = result === null || result === void 0 ? void 0 : result.file) !== null && _a !== void 0 ? _a : '';
}
static getVideoByLocale(locale, video) {
var _a;
const result = video.find(v => v.locale === locale);
return (_a = result === null || result === void 0 ? void 0 : result.url) !== null && _a !== void 0 ? _a : '';
}
static getQueueByLocale(locale, queues) {
return queues.find(queue => queue.locale === locale);
}
replaceVariables(text, botonicContext) {
if (!botonicContext) {
return text;
}
return text.replace(VARIABLE_PATTERN_GLOBAL, match => {
// remove \\ ( escape for _ ) added by text node with markdown
const keyPath = match.slice(1, -1).replaceAll('\\', '');
const botVariable = keyPath.endsWith(ACCESS_TOKEN_VARIABLE_KEY)
? match
: this.getValueFromKeyPath(botonicContext, keyPath);
return this.isValidType(botVariable) ? String(botVariable) : match;
});
}
getValueFromKeyPath(botonicContext, keyPath) {
if (keyPath.startsWith('input.')) {
const parts = keyPath.split('.');
const input = botonicContext.input;
const firstKey = parts[1];
const firstVal = ContentFieldsBase.INPUT_ACCESSOR_KEYS[firstKey]
? ContentFieldsBase.INPUT_ACCESSOR_KEYS[firstKey](input)
: this.resolveObjectKey(input, firstKey);
if (parts.length === 2)
return firstVal;
return parts
.slice(2)
.reduce((object, key) => this.resolveObjectKey(object, key), firstVal);
}
if (keyPath.startsWith('session.')) {
return keyPath
.split('.')
.slice(1)
.reduce((object, key) => this.resolveObjectKey(object, key), botonicContext.session);
}
return keyPath
.split('.')
.reduce((object, key) => this.resolveObjectKey(object, key), botonicContext.session.user.extraData);
}
resolveObjectKey(object, key) {
if (!object)
return undefined;
if (object[key] !== undefined)
return object[key];
const altKey = key.includes('_') ? this.camelCase(key) : this.snakeCase(key);
return object[altKey];
}
camelCase(s) {
return s.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
}
snakeCase(s) {
return s.replace(/[A-Z]/g, c => `_${c.toLowerCase()}`);
}
isValidType(botVariable) {
const validTypes = ['boolean', 'string', 'number'];
return validTypes.includes(typeof botVariable);
}
}
// v2 input: input.data is BotonicData; map to input.text for template {{input.data}}
ContentFieldsBase.INPUT_ACCESSOR_KEYS = {
data: input => input.text,
payload: input => input.payload,
referral: input => input.referral,
};
//# sourceMappingURL=content-fields-base.js.map