@botonic/react
Version:
Build Chatbots using React
47 lines • 1.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultichannelFacebook = exports.MAX_CHARACTERS_FACEBOOK = void 0;
exports.MAX_CHARACTERS_FACEBOOK = 640;
class MultichannelFacebook {
constructor() { }
convertText(props, originalText) {
if (originalText.length > exports.MAX_CHARACTERS_FACEBOOK) {
const texts = this.splitText(originalText);
const lastText = texts.pop();
const { propsLastText, propsWithoutChildren } = this.getNewProps(props, lastText);
return { texts, propsLastText, propsWithoutChildren };
}
return { propsLastText: props };
}
splitText(originalText) {
const lines = originalText.split('\n');
const initialText = lines.shift();
const texts = [initialText];
let index = 0;
lines.forEach(currentText => {
if (texts[index].length + currentText.length > exports.MAX_CHARACTERS_FACEBOOK) {
index++;
texts.push(currentText);
}
else {
texts[index] = texts[index].concat('\n', currentText);
}
});
return texts;
}
// modifies the props to keep the children only for the last text message, this message will be the one with buttons and replies
getNewProps(props, lastText) {
const propsLastText = Object.assign({}, props);
propsLastText.children = [lastText];
if (Array.isArray(props.children)) {
props.children
.filter(e => e.type)
.forEach(e => propsLastText.children.push(e));
}
const propsWithoutChildren = Object.assign({}, props);
delete propsWithoutChildren.children;
return { propsLastText, propsWithoutChildren };
}
}
exports.MultichannelFacebook = MultichannelFacebook;
//# sourceMappingURL=facebook.js.map
;