umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
204 lines (203 loc) • 7.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartApp = void 0;
const TemplateTypeModel_1 = require("./TemplateTypeModel");
const mmApp_1 = require("../mmApp");
const Text_1 = require("../utils/standard/Text");
const button_1 = require("../components/button");
const api_1 = require("../api");
class SmartApp extends TemplateTypeModel_1.TemplateTypeModel {
MAX_TIME_REQUEST = 2800;
_session = null;
async _getPayload() {
const payload = {
pronounceText: this.controller.text,
pronounceTextType: 'application/text',
device: this._session.device,
intent: this.controller.thisIntentName,
projectName: this._session.projectName,
auto_listening: !this.controller.isEnd,
finished: this.controller.isEnd,
};
if (this.controller.emotion) {
payload.emotion = {
emotionId: this.controller.emotion,
};
}
if (this.controller.text) {
payload.items = [
{
bubble: {
text: Text_1.Text.resize(this.controller.text, 250),
markdown: true,
expand_policy: 'auto_expand',
},
},
];
}
if (this.controller.tts) {
payload.pronounceText = this.controller.tts;
payload.pronounceTextType = 'application/ssml';
}
if (this.controller.isScreen) {
if (this.controller.card.images.length) {
if (typeof payload.items === 'undefined') {
payload.items = [];
}
const cards = await this.controller.card.getCards();
payload.items.push(cards);
}
payload.suggestions = {
buttons: this.controller.buttons.getButtons(button_1.Buttons.T_SMARTAPP_BUTTONS),
};
}
if (this.controller.isEnd) {
if (typeof payload.items === 'undefined') {
payload.items = [];
}
payload.items.push({
command: {
type: 'close_app',
},
});
}
return payload;
}
_initUserCommand(content) {
this.controller.requestObject = content;
this.controller.messageId = content.messageId;
switch (content.messageName) {
case 'MESSAGE_TO_SKILL':
case 'CLOSE_APP':
this.controller.userCommand = content.payload.message.normalized_text;
this.controller.originalUserCommand = content.payload.message.original_text;
break;
case 'SERVER_ACTION':
case 'RUN_APP':
this.controller.payload = content.payload?.server_action?.parameters;
if (typeof this.controller.payload === 'string') {
this.controller.userCommand = this.controller.originalUserCommand =
this.controller.payload;
}
if (content.messageName === 'RUN_APP') {
this.controller.messageId = 0;
this.controller.originalUserCommand = this.controller.userCommand;
this.controller.userCommand = '';
}
break;
case 'RATING_RESULT':
this.controller.payload = content.payload;
this.controller.messageId = 0;
this.controller.userEvents = {
rating: {
status: content.payload.status_code?.code === 1,
value: content.payload.rating?.estimation,
},
};
break;
}
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 (!this.controller) {
this.controller = controller;
}
this._initUserCommand(content);
this._session = {
device: content.payload.device,
meta: content.payload.meta,
sessionId: content.sessionId,
messageId: content.messageId,
uuid: content.uuid,
projectName: content.payload.projectName,
};
this.controller.oldIntentName = content.payload.intent;
this.controller.appeal = content.payload.character.appeal;
this.controller.userId = content.uuid.userId;
mmApp_1.mmApp.params.user_id = this.controller.userId;
const nlu = {
entities: content.payload.message.entities,
tokens: content.payload.message.tokenized_elements_list,
};
this.controller.nlu.setNlu(nlu);
this.controller.userMeta = content.payload.meta || {};
mmApp_1.mmApp.params.app_id = content.payload.app_info.applicationId;
if (content.payload.device.capabilities && content.payload.device.capabilities.screen) {
this.controller.isScreen = content.payload.device.capabilities.screen.available;
}
else {
this.controller.isScreen = true;
}
return true;
}
else {
this.error = 'SmartApp:init(): Отправлен пустой запрос!';
}
return false;
}
async getRatingContext() {
return {
messageName: 'CALL_RATING',
sessionId: this._session.sessionId,
messageId: this._session.messageId,
uuid: this._session.uuid,
payload: {},
};
}
async getContext() {
const result = {
messageName: 'ANSWER_TO_USER',
sessionId: this._session.sessionId,
messageId: this._session.messageId,
uuid: this._session.uuid,
};
if (this.controller.sound.sounds.length) {
if (this.controller.tts === null) {
this.controller.tts = this.controller.text;
}
this.controller.tts = await this.controller.sound.getSounds(this.controller.tts);
}
result.payload = await this._getPayload();
const timeEnd = this.getProcessingTime();
if (timeEnd >= this.MAX_TIME_REQUEST) {
this.error = `SmartApp:getContext(): Превышено ограничение на отправку ответа. Время ответа составило: ${timeEnd} сек.`;
}
return result;
}
async _getUserData() {
const request = new api_1.Request();
request.url = `https://smartapp-code.sberdevices.ru/tools/api/data/${this.controller.userId}`;
const result = await request.send();
if (result.status && result.data) {
return result.data;
}
return {};
}
async _setUserData(data) {
const request = new api_1.Request();
request.header = api_1.Request.HEADER_AP_JSON;
request.url = `https://smartapp-code.sberdevices.ru/tools/api/data/${this.controller.userId}`;
request.post = data;
return await request.send();
}
async setLocalStorage(data) {
await this._setUserData(data);
}
async getLocalStorage() {
return this._getUserData();
}
isLocalStorage() {
return true;
}
}
exports.SmartApp = SmartApp;