@botonic/react
Version:
Build Chatbots using React
132 lines • 5.71 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
import React from 'react';
import { Audio } from './components/audio';
import { Button } from './components/button/index';
import { ButtonsDisabler } from './components/buttons-disabler';
import { Carousel } from './components/carousel';
import { Document } from './components/document';
import { Element } from './components/element';
import { Image } from './components/image';
import { Location } from './components/location';
import { Pic } from './components/pic';
import { Reply } from './components/reply';
import { Subtitle } from './components/subtitle';
import { Text } from './components/text';
import { Title } from './components/title';
import { Video } from './components/video';
import { isAudio, isButtonMessage, isCarousel, isCustom, isDocument, isImage, isLocation, isText, isVideo, } from './message-utils';
/**
*
* @param msg {object}
* @param customMessageTypes {{customTypeName}[]?}
* @return {React.ReactNode}
*/
export function msgToBotonic(msg, customMessageTypes = []) {
delete msg.display;
if (isCustom(msg)) {
try {
return customMessageTypes
.find(mt => mt.customTypeName === msg.data.customTypeName)
.deserialize(msg);
}
catch (e) {
console.log(e);
}
}
else if (isText(msg)) {
return textToBotonic(msg);
}
else if (isCarousel(msg)) {
const elements = msg.elements || msg.data.elements;
return (_createElement(Carousel, Object.assign({}, msg, { key: msg.key }), elementsParse(elements)));
}
else if (isImage(msg)) {
return (_jsx(Image, Object.assign({}, msg, { src: msg.data.image !== undefined ? msg.data.image : msg.data }), msg.key));
}
else if (isVideo(msg)) {
return (_jsx(Video, Object.assign({}, msg, { src: msg.data.video !== undefined ? msg.data.video : msg.data })));
}
else if (isAudio(msg)) {
return (_jsx(Audio, Object.assign({}, msg, { src: msg.data.audio !== undefined ? msg.data.audio : msg.data })));
}
else if (isDocument(msg)) {
return (_jsx(Document, Object.assign({}, msg, { src: msg.data.document !== undefined ? msg.data.document : msg.data })));
}
else if (isLocation(msg)) {
const lat = msg.data ? msg.data.location.lat : msg.latitude;
const long = msg.data ? msg.data.location.long : msg.longitude;
return _jsx(Location, Object.assign({}, msg, { lat: lat, long: long }));
}
else if (isButtonMessage(msg)) {
const buttons = buttonsParse(msg.buttons);
return (_jsx(_Fragment, { children: _createElement(Text, Object.assign({}, msg, { key: msg.key }),
msg.text,
buttons) }));
}
console.warn(`Not converting message of type ${msg.type}`);
return null;
}
/**
* @param msgs {object|object[]}
* @param customMessageTypes {{customTypeName}[]?}
* @return {React.ReactNode}
*/
export function msgsToBotonic(msgs, customMessageTypes) {
if (Array.isArray(msgs)) {
return (_jsx(_Fragment, { children: msgs.map((msg, i) => {
if (msg.key == null) {
msg.key = `msg${i}`;
}
return msgToBotonic(msg, customMessageTypes);
}) }));
}
return msgToBotonic(msgs, customMessageTypes);
}
function textToBotonic(msg) {
var _a, _b;
const txt = (_b = (_a = msg.data) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : String(msg.data);
if ((msg.replies && msg.replies.length) ||
(msg.keyboard && msg.keyboard.length))
return (_createElement(Text, Object.assign({}, msg, { key: msg.key }),
txt,
parseQuickReplies(msg)));
if (msg.buttons && msg.buttons.length)
return (_createElement(Text, Object.assign({}, msg, { key: msg.key }),
txt,
buttonsParse(msg.buttons)));
return (_createElement(Text, Object.assign({}, msg, { key: msg.key }), txt));
}
function elementsParse(elements) {
return elements.map((e, i) => (_jsxs(Element, { children: [_jsx(Pic, { src: e.img || e.pic || e.image_url }), _jsx(Title, { children: e.title }), _jsx(Subtitle, { children: e.subtitle }), buttonsParse(e.button || e.buttons)] }, i)));
}
function buttonsParse(buttons) {
return buttons.map((b, i) => {
const props = b.props || b;
let payload = props.payload;
if (props.path)
payload = `__PATH_PAYLOAD__${props.path}`;
const url = props.messenger_extensions ? null : props.url;
const target = props.messenger_extensions ? null : props.target;
const title = props.title;
const webview = props.messenger_extensions ? props.url : props.webview;
const disabledProps = ButtonsDisabler.constructBrowserProps(props);
return (_jsx(Button, Object.assign({ payload: payload, url: url, target: target, webview: webview }, disabledProps, { children: title }), i));
});
}
function parseQuickReplies(msg) {
let replies = null;
if (msg.replies) {
replies = msg.replies.map((el, i) => {
let payload = el.payload;
if (el.path)
payload = `__PATH_PAYLOAD__${el.path}`;
return (_jsx(Reply, Object.assign({ payload: payload }, { children: el.text }), i));
});
}
if (msg.keyboard) {
replies = msg.keyboard.map((el, i) => (_jsx(Reply, Object.assign({ payload: el.data }, { children: el.label }), i)));
}
return replies;
}
//# sourceMappingURL=msg-to-botonic.js.map