UNPKG

umbot

Version:

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

169 lines (168 loc) 6.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Alisa = void 0; const TemplateTypeModel_1 = require("./TemplateTypeModel"); const mmApp_1 = require("../mmApp"); const Text_1 = require("../utils/standard/Text"); class Alisa extends TemplateTypeModel_1.TemplateTypeModel { VERSION = '1.0'; MAX_TIME_REQUEST = 2800; _session; _isState = false; _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; } _setState(state) { if (typeof state.user !== 'undefined') { this.controller.state = state.user; this._stateName = 'user_state_update'; } else if (typeof state.application !== 'undefined') { this.controller.state = state.application; this._stateName = 'application_state'; } 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; } } _setUserId() { if (this._session) { let userId = null; this._isState = false; if (mmApp_1.mmApp.params.y_isAuthUser) { if (typeof this._session.user !== 'undefined' && typeof this._session.user.user_id !== 'undefined') { userId = this._session.user.user_id; this._isState = true; this.controller.userToken = this._session.user.access_token || null; } } if (userId === null) { if (typeof this._session.application !== 'undefined' && this._session.application.application_id !== 'undefined') { userId = this._session.application.application_id; } else { userId = this._session.user_id; } } mmApp_1.mmApp.params.user_id = this.controller.userId = userId; } } 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 (content.account_linking_complete_event) { this.controller.userEvents = { auth: { status: true, }, }; return true; } this.error = 'Alisa.init(): Не корректные данные!'; return false; } if (!this.controller) { this.controller = controller; } this.controller.requestObject = content; this._initUserCommand(content.request); this._session = content.session; this._setUserId(); this.controller.nlu.setNlu(content.request.nlu || {}); this.controller.userMeta = content.meta || {}; this.controller.messageId = this._session.message_id; if (typeof content.state !== 'undefined') { this._setState(content.state); } mmApp_1.mmApp.params.app_id = this._session.skill_id; this.controller.isScreen = typeof this.controller.userMeta.interfaces.screen !== 'undefined'; if (this.controller.originalUserCommand === 'ping') { this.controller.text = 'pong'; this.sendInInit = this.getContext(); } return true; } else { this.error = 'Alisa:init(): Отправлен пустой запрос!'; } return false; } async getContext() { const result = { version: this.VERSION, }; if (this.controller.isAuth && this.controller.userToken === null) { result.start_account_linking = function () { }; } else { await this._initTTS(); result.response = await this._getResponse(); } if ((this._isState || this.isUsedLocalStorage) && this._stateName) { if (this.isUsedLocalStorage && this.controller.userData) { result[this._stateName] = this.controller.userData; } else if (this.controller.state) { result[this._stateName] = this.controller.state; } } const timeEnd = this.getProcessingTime(); if (timeEnd >= this.MAX_TIME_REQUEST) { this.error = `Alisa:getContext(): Превышено ограничение на отправку ответа. Время ответа составило: ${timeEnd} сек.`; } return result; } async getLocalStorage() { return Promise.resolve(this.controller.state); } isLocalStorage() { return this.controller.state !== null; } } exports.Alisa = Alisa;