UNPKG

umbot

Version:

Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber

135 lines (134 loc) 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Buttons = void 0; const Button_1 = require("./Button"); const AlisaButton_1 = require("./types/AlisaButton"); const TelegramButton_1 = require("./types/TelegramButton"); const VkButton_1 = require("./types/VkButton"); const ViberButton_1 = require("./types/ViberButton"); const SmartAppButton_1 = require("./types/SmartAppButton"); class Buttons { static T_ALISA_BUTTONS = 'alisa_btn'; static T_ALISA_CARD_BUTTON = 'alisa_card_btn'; static T_VK_BUTTONS = 'vk_btn'; static T_TELEGRAM_BUTTONS = 'telegram_btn'; static T_VIBER_BUTTONS = 'viber_btn'; static T_SMARTAPP_BUTTONS = 'smart-app_btn'; static T_SMARTAPP_BUTTON_CARD = 'smart-app_card_btn'; static T_USER_APP_BUTTONS = 'user_app_btn'; buttons; btns; links; type; constructor() { this.buttons = []; this.btns = []; this.links = []; this.type = Buttons.T_ALISA_BUTTONS; } clear() { this.buttons = []; this.btns = []; this.links = []; } _add(title, url, payload, hide = false, options = {}) { let button = new Button_1.Button(); if (hide === Button_1.Button.B_LINK) { if (!button.initLink(title, url, payload, options)) { button = null; } } else { if (!button.initBtn(title, url, payload, options)) { button = null; } } if (button) { this.buttons.push(button); } return this; } addBtn(title, url = '', payload = '', options = {}) { return this._add(title, url, payload, Button_1.Button.B_BTN, options); } addLink(title, url = '', payload = '', options = {}) { return this._add(title, url, payload, Button_1.Button.B_LINK, options); } _initProcessingBtn(buttons, callback) { if (typeof buttons === 'object') { buttons.forEach((button) => { if (typeof button !== 'string') { callback(button.title || null, button.url || '', button.payload || null, button.options || {}); } else { callback(button); } }); } else { callback(buttons); } } _processing() { if (this.btns.length) { this._initProcessingBtn(this.btns, this.addBtn.bind(this)); } if (this.links.length) { this._initProcessingBtn(this.links, this.addLink.bind(this)); } this.btns = []; this.links = []; } getButtons(type = null, userButton = null) { this._processing(); if (type === null) { type = this.type; } let button = null; switch (type) { case Buttons.T_ALISA_BUTTONS: button = new AlisaButton_1.AlisaButton(); button.isCard = false; break; case Buttons.T_ALISA_CARD_BUTTON: button = new AlisaButton_1.AlisaButton(); button.isCard = true; break; case Buttons.T_VK_BUTTONS: button = new VkButton_1.VkButton(); break; case Buttons.T_TELEGRAM_BUTTONS: button = new TelegramButton_1.TelegramButton(); break; case Buttons.T_VIBER_BUTTONS: button = new ViberButton_1.ViberButton(); break; case Buttons.T_SMARTAPP_BUTTONS: button = new SmartAppButton_1.SmartAppButton(); button.isCard = false; break; case Buttons.T_SMARTAPP_BUTTON_CARD: button = new SmartAppButton_1.SmartAppButton(); button.isCard = true; break; case Buttons.T_USER_APP_BUTTONS: if (userButton) { button = userButton; } break; } if (button) { button.buttons = this.buttons; return button.getButtons(); } return null; } getButtonJson(type = null, userButton = null) { const btn = this.getButtons(type, userButton); if (btn && btn.length) { return JSON.stringify(btn); } return null; } } exports.Buttons = Buttons;