@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
181 lines • 5.97 kB
JavaScript
import * as cms from '../cms';
import { ButtonStyle, CmsException } from '../cms';
export class RenderOptions {
constructor() {
this.followUpDelaySeconds = 4;
this.maxButtons = 3;
this.maxQuickReplies = 5;
this.defaultButtonsStyle = ButtonStyle.BUTTON;
}
}
export class BotonicMsgConverter {
constructor(options = {}) {
this.options = Object.assign(Object.assign({}, new RenderOptions()), options);
}
convert(content, delayS = 0) {
if (content instanceof cms.Carousel) {
return this.carousel(content, delayS);
}
if (content instanceof cms.Text) {
return this.text(content, delayS);
}
if (content instanceof cms.StartUp) {
return this.startUp(content, delayS);
}
if (content instanceof cms.Image) {
return this.image(content, delayS);
}
if (content instanceof cms.Video) {
return this.video(content, delayS);
}
if (content instanceof cms.Document) {
return this.document(content, delayS);
}
throw new CmsException(`Unsupported content type ${content.contentType}`);
}
carousel(carousel, delayS = 0) {
const msg = {
type: 'carousel',
delay: delayS,
data: {
elements: carousel.elements.map(e => this.element(e)),
},
};
return this.appendFollowUp(msg, carousel);
}
element(cmsElement) {
return {
img: cmsElement.imgUrl,
title: this.str(cmsElement.title),
subtitle: this.str(cmsElement.subtitle),
buttons: this.convertButtons(cmsElement.buttons, ButtonStyle.BUTTON),
};
}
convertButtons(cmsButtons, style) {
const maxButtons = style == ButtonStyle.BUTTON
? this.options.maxButtons
: this.options.maxQuickReplies;
if (cmsButtons.length > maxButtons) {
console.error('Content has more buttons than maximum. Trimming');
cmsButtons = cmsButtons.slice(0, maxButtons);
}
return cmsButtons.map(cmsButton => {
const msgButton = {
payload: cmsButton.callback.payload,
url: cmsButton.callback.url,
};
if (style == ButtonStyle.BUTTON) {
msgButton['title'] = this.str(cmsButton.text);
}
else {
msgButton['text'] = this.str(cmsButton.text);
}
return msgButton;
});
}
text(text, delayS = 0) {
const msg = {
type: 'text',
delay: delayS,
data: { text: this.str(text.text) },
};
const buttonsStyle = text.buttonsStyle || this.options.defaultButtonsStyle;
const buttons = this.convertButtons(text.buttons, buttonsStyle);
if (buttonsStyle == ButtonStyle.QUICK_REPLY) {
msg['replies'] = buttons;
}
else {
msg['buttons'] = buttons;
}
return this.appendFollowUp(msg, text);
}
startUp(startUp, delayS = 0) {
const img = {
type: 'image',
delay: delayS,
data: { image: startUp.imgUrl },
};
const text = {
type: 'text',
data: { text: this.str(startUp.text) },
buttons: this.convertButtons(startUp.buttons, ButtonStyle.BUTTON),
};
return this.appendFollowUp([img, text], startUp);
}
image(img, delayS = 0) {
const msg = {
type: 'image',
delay: delayS,
data: {
image: img.imgUrl,
},
};
return this.appendFollowUp(msg, img);
}
video(video, delayS = 0) {
const msg = {
type: 'video',
delay: delayS,
data: {
video: video.videoUrl,
},
};
return this.appendFollowUp(msg, video);
}
document(doc, delayS = 0) {
const msg = {
type: 'document',
delay: delayS,
data: {
document: doc.docUrl,
},
};
return this.appendFollowUp(msg, doc);
}
appendFollowUp(contentMsgs, content) {
if (content.common.followUp) {
const followUp = this.followUp(content.common.followUp);
const followUps = Array.isArray(followUp) ? followUp : [followUp];
if (Array.isArray(contentMsgs)) {
contentMsgs.push(...followUps);
}
else {
contentMsgs = [contentMsgs, ...followUps];
}
return contentMsgs;
}
return contentMsgs;
}
followUp(followUp) {
if (followUp instanceof cms.Text) {
// give user time to read the initial text
return this.text(followUp, this.options.followUpDelaySeconds);
}
else if (followUp instanceof cms.Carousel) {
// for carousels, the previous text usually introduces the carousel. So, we set a smaller delay
return this.carousel(followUp, 2);
}
else if (followUp instanceof cms.Image) {
return this.image(followUp);
}
else if (followUp instanceof cms.Video) {
return this.video(followUp);
}
else if (followUp instanceof cms.StartUp) {
return this.startUp(followUp);
}
else if (followUp instanceof cms.Document) {
return this.document(followUp);
}
else {
throw new Error('Unexpected followUp type ' + typeof followUp);
}
}
str(str) {
if (this.options.replaceEmptyStringsWith == undefined) {
return str;
}
return str || this.options.replaceEmptyStringsWith;
}
}
//# sourceMappingURL=botonic-converter.js.map