@ebenos/viber-elements
Version:
Elements Library for the Ebony framework.
102 lines • 3.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Message = void 0;
function isText(options) {
return options.text !== undefined;
}
function isUrl(options) {
return options.media !== undefined;
}
function isRichMedia(options) {
return options.rich_media !== undefined;
}
/** Message Class */
class Message {
/**
* Create a message
* @param {MessageOptions|string} options - The message elements
*/
constructor(options) {
const { sender, tracking_data, attachment, keyboard } = options;
this.sender = sender;
this.tracking_data = tracking_data;
this.attachment = attachment;
this.keyboard = keyboard;
this.type = 'text';
if (isUrl(options)) {
const { media } = options;
this.media = media;
this.type = 'url';
}
else if (isRichMedia(options)) {
const { rich_media } = options;
this.rich_media = rich_media;
this.type = 'rich_media';
}
else if (isText(options)) {
const { text } = options;
this.text = text;
this.type = 'text';
}
else {
const _exhaustiveCheck = options;
console.log(_exhaustiveCheck);
throw new Error('This should never happen');
}
}
determineType() {
if (this.type) {
return this.type;
}
else {
throw new Error('Cannot determine message type!');
}
}
serialize() {
/*
* Common for all Serialized messages
**/
const obj = {
type: this.determineType(),
sender: this.sender,
min_api_version: '7'
};
if (this.tracking_data !== undefined) {
if (typeof this.tracking_data === 'string') {
obj.tracking_data = this.tracking_data;
}
else {
obj.tracking_data = JSON.stringify(this.tracking_data);
}
}
if (this.attachment !== undefined) {
obj.attachment = this.attachment.serialize();
}
if (this.keyboard !== undefined) {
obj.keyboard = this.keyboard.serialize();
}
/*
* Properties that depend on the type of message
**/
switch (this.type) {
case 'text':
if (this.text === undefined) {
throw new Error('This should never happen');
}
return { ...obj, text: this.text };
case 'rich_media':
if (this.rich_media === undefined) {
throw new Error('This should never happen');
}
return { ...obj, rich_media: this.rich_media.serialize() };
case 'url':
if (this.media === undefined) {
throw new Error('This should never happen');
}
return { ...obj, media: this.media };
}
throw new Error('This should never happen');
}
}
exports.Message = Message;
//# sourceMappingURL=message.js.map