fbhelpernodejs
Version:
Modulo para axuiliar a construção de bots
43 lines (41 loc) • 2.13 kB
JavaScript
const exception = (level, message, name = 'Syntax Error') => {
throw {
name,
level,
message
};
};
const Carrossel = elements => {
if (Array.isArray(elements)) {
if (elements.length === 0) exception('elements', 'O Array informado esta vazio, favor insira ao menos um elemento!');
let response = [];
elements.forEach((element, index) => {
if (!element.title) exception('element-child', `Elemento [${index}] não possui TITLE. ${JSON.stringify(element)}`);
// if (!element.subtitle) exception('element-child', `Elemento [${index}] não possui SUBTITLE. ${JSON.stringify(element)}`)
let buttons;
if (element.buttons) {
if (element.buttons.length === 0) exception('button-length', `Elemento [${index}] possui o campo buttons, porém seu tamanho é 0, favor remova o campo ou preencha com um botão. ${JSON.stringify(element)}`);
buttons = [];
element.buttons.forEach((button, indexButton) => {
if (!button.title && button.type !== 'element_share') exception('buttons-child', `Elemento [${index}] Botão [${indexButton}] não possui TITLE. ${JSON.stringify(element)}`);
if (!button.payload && button.type !== 'element_share') exception('buttons-child', `Elemento [${index}] Botão [${indexButton}] não possui PAYLOAD. ${JSON.stringify(element)}`);
if (!button.type) exception('buttons-child', `Elemento [${index}] Botão [${indexButton}] não possui TYPE. ${JSON.stringify(element)}`);
let { title, payload, type } = button;
let url, webViewRatio;
if (type === 'web_url') {
url = payload;
payload = undefined;
webViewRatio = 'full';
}
buttons.push({ title, payload, type, url, webview_height_ratio: webViewRatio });
});
}
let { title, subtitle, image, item_url } = element;
response.push({ title, subtitle, image_url: image, buttons, item_url });
});
return response;
} else {
exception('constructor', 'O valor informado para elements não corresponde a um array');
}
};
module.exports = { Carrossel };