UNPKG

umbot

Version:

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

135 lines (134 loc) 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Marusia = void 0; const TemplateTypeModel_1 = require("./TemplateTypeModel"); const mmApp_1 = require("../mmApp"); const Text_1 = require("../utils/standard/Text"); class Marusia extends TemplateTypeModel_1.TemplateTypeModel { VERSION = '1.0'; MAX_TIME_REQUEST = 2.8; _session = null; _stateName = null; async _getResponse() { const response = { text: Text_1.Text.resize(this.controller.text, 1024), tts: Text_1.Text.resize(this.controller.tts, 1024), end_session: this.controller.isEnd, }; if (this.controller.isScreen) { if (this.controller.card.images.length) { response.card = (await this.controller.card.getCards()); if (!response.card) { response.card = undefined; } } response.buttons = this.controller.buttons.getButtons(); } return response; } _getSession() { return { session_id: this._session.session_id, message_id: this._session.message_id, user_id: this._session.user_id, }; } _setState(state) { if (typeof state.user !== 'undefined') { this.controller.state = state.user; this._stateName = 'user_state_update'; } else if (typeof state.session !== 'undefined') { this.controller.state = state.session; this._stateName = 'session_state'; } } _initUserCommand(request) { if (request.type === 'SimpleUtterance') { this.controller.userCommand = request.command.trim(); this.controller.originalUserCommand = request.original_utterance.trim(); } else { if (typeof request.payload === 'string') { this.controller.userCommand = request.payload; this.controller.originalUserCommand = request.payload; } else { this.controller.userCommand = request.command?.trim(); this.controller.originalUserCommand = request.original_utterance?.trim(); } this.controller.payload = request.payload; } if (!this.controller.userCommand) { this.controller.userCommand = this.controller.originalUserCommand; } } async init(query, controller) { if (query) { let content; if (typeof query === 'string') { content = JSON.parse(query); } else { content = { ...query }; } if (typeof content.session === 'undefined' && typeof content.request === 'undefined') { if (typeof content.account_linking_complete_event !== 'undefined') { this.controller.userEvents = { auth: { status: true, }, }; return true; } this.error = 'Marusia.init(): Не корректные данные!'; return false; } if (!this.controller) { this.controller = controller; } this.controller.requestObject = content; this._initUserCommand(content.request); if (typeof content.state !== 'undefined') { this._setState(content.state); } this._session = content.session; this.controller.userId = this._session.user_id; mmApp_1.mmApp.params.user_id = this.controller.userId; this.controller.nlu.setNlu(content.request.nlu || null); this.controller.userMeta = content.meta || []; this.controller.messageId = this._session.message_id; mmApp_1.mmApp.params.app_id = this._session.skill_id; this.controller.isScreen = typeof this.controller.userMeta.interfaces.screen !== 'undefined'; return true; } else { this.error = 'Marusia:init(): Отправлен пустой запрос!'; } return false; } async getContext() { const result = { version: this.VERSION, }; await this._initTTS(); result.response = await this._getResponse(); result.session = this._getSession(); if (this.isUsedLocalStorage && this.controller.userData && this._stateName) { result[this._stateName] = this.controller.userData; } const timeEnd = this.getProcessingTime(); if (timeEnd >= this.MAX_TIME_REQUEST) { this.error = `Marusia:getContext(): Превышено ограничение на отправку ответа. Время ответа составило: ${timeEnd} сек.`; } return result; } async getLocalStorage() { return Promise.resolve(this.controller.state); } isLocalStorage() { return this.controller.state !== null; } } exports.Marusia = Marusia;