umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
134 lines (133 loc) • 4.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BotController = void 0;
const button_1 = require("../components/button");
const card_1 = require("../components/card");
const sound_1 = require("../components/sound");
const nlu_1 = require("../components/nlu");
const mmApp_1 = require("../mmApp");
const Text_1 = require("../utils/standard/Text");
class BotController {
buttons;
card;
text = '';
tts = null;
nlu;
sound;
userId = null;
userToken = null;
userMeta = null;
messageId = null;
userCommand = null;
originalUserCommand = null;
payload = null;
userData = {};
isAuth = false;
userEvents = null;
state = null;
isScreen = false;
isEnd = false;
isSend = false;
requestObject = null;
thisIntentName = null;
emotion = null;
appeal = null;
isSendRating = false;
oldIntentName = null;
constructor() {
this.buttons = new button_1.Buttons();
this.card = new card_1.Card();
this.sound = new sound_1.Sound();
this.nlu = new nlu_1.Nlu();
}
clearStoreData() {
this.buttons.clear();
this.card.clear();
this.nlu.setNlu({});
this.text = '';
this.tts = null;
this.userId = null;
this.userToken = null;
this.userMeta = null;
this.messageId = null;
this.userCommand = null;
this.originalUserCommand = null;
this.payload = null;
this.userData = {};
this.isAuth = false;
this.userEvents = null;
this.state = null;
this.isScreen = false;
this.isEnd = false;
this.isSend = false;
this.requestObject = null;
this.oldIntentName = null;
this.thisIntentName = null;
this.emotion = null;
this.appeal = null;
this.isSendRating = false;
}
static _intents() {
return mmApp_1.mmApp.params.intents || [];
}
static _getIntent(text) {
if (!text) {
return null;
}
const intents = BotController._intents();
for (const intent of intents) {
if (Text_1.Text.isSayText(intent.slots || [], text, intent.is_pattern || false)) {
return intent.name;
}
}
return null;
}
_getCommand() {
if (!this.userCommand) {
return null;
}
const commandKeys = Object.keys(mmApp_1.mmApp.commands);
if (commandKeys.length) {
for (let i = 0; i < commandKeys.length; i++) {
if (Text_1.Text.isSayText(mmApp_1.mmApp.commands[commandKeys[i]].slots || [], this.userCommand, mmApp_1.mmApp.commands[commandKeys[i]].isPattern || false)) {
const res = mmApp_1.mmApp.commands[commandKeys[i]].cb?.(this.userCommand, this);
if (res) {
this.text = res;
}
return commandKeys[i];
}
}
}
return null;
}
run() {
const commandResult = this._getCommand();
if (commandResult) {
this.action(commandResult, true);
}
else {
let intent = BotController._getIntent(this.userCommand);
if (intent === null &&
this.originalUserCommand &&
this.userCommand !== this.originalUserCommand) {
intent = BotController._getIntent(this.originalUserCommand.toLowerCase());
}
if (intent === null && this.messageId === 0) {
intent = mmApp_1.WELCOME_INTENT_NAME;
}
switch (intent) {
case mmApp_1.WELCOME_INTENT_NAME:
this.text = Text_1.Text.getText(mmApp_1.mmApp.params.welcome_text || '');
break;
case mmApp_1.HELP_INTENT_NAME:
this.text = Text_1.Text.getText(mmApp_1.mmApp.params.help_text || '');
break;
}
this.action(intent);
}
if (this.tts === null && (mmApp_1.mmApp.appType === mmApp_1.T_ALISA || mmApp_1.mmApp.appType === mmApp_1.T_MARUSIA)) {
this.tts = this.text;
}
}
}
exports.BotController = BotController;