botserver/deploy/core.gbapp/services/GBConversationalService.ts

128 lines
5.8 KiB
TypeScript
Raw Normal View History

2018-04-21 02:59:30 -03:00
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
| Licensed under the AGPL-3.0. |
| |
| According to our dual licensing model, this program can be used either |
| under the terms of the GNU Affero General Public License, version 3, |
| or under a proprietary license. |
| |
| The texts of the GNU Affero General Public License with an additional |
| permission and of our proprietary license can be found at and |
| in the LICENSE file you have received along with this program. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU Affero General Public License for more details. |
| |
| "General Bots" is a registered trademark of Pragmatismo.io. |
| The licensing of the program under the AGPLv3 does not imply a |
| trademark license. Therefore any rights, title and interest in |
| our trademarks remain entirely with us. |
| |
\*****************************************************************************/
"use strict";
const UrlJoin = require("url-join");
const gBuilder = require("botbuilder");
const logger = require("../../../src/logger");
import { GBConfigService } from "./GBConfigService";
import { GBCoreService } from "./GBCoreService";
import { Session, Message, LuisRecognizer } from "botbuilder";
import { GBService, GBServiceCallback, IGBConversationalService } from "botlib";
2018-04-21 02:59:30 -03:00
import { GBError } from "botlib";
import { GBERROR_TYPE } from "botlib";
import { GBMinInstance } from "botlib";
export class GBConversationalService implements IGBConversationalService {
2018-04-21 02:59:30 -03:00
coreService: GBCoreService;
2018-04-21 02:59:30 -03:00
constructor(coreService: GBCoreService) {
this.coreService = coreService;
}
2018-04-21 02:59:30 -03:00
sendEvent(session: Session, name: string, value: any) {
var msg = new gBuilder.Message();
msg.data.type = "event";
msg.data.name = name;
msg.data.value = value;
session.send(msg);
}
2018-04-21 02:59:30 -03:00
runNLP(
session: Session,
min: GBMinInstance,
text: string,
cb: GBServiceCallback<any>
) {
LuisRecognizer.recognize(
text,
min.instance.nlpServerUrl,
(err, intents, entities) => {
if (err) {
cb(null, new GBError(err, GBERROR_TYPE.nlpGeneralError));
return;
}
2018-04-21 02:59:30 -03:00
if (intents && intents.length > 0) {
var intent = intents[0].intent;
var entity =
entities && entities.length > 0
? entities[0].entity.toUpperCase()
: null;
logger.trace(
"luis: intent: [" + intent + "] entity: [" + entity + "]"
);
2018-04-21 02:59:30 -03:00
// PACKAGE: Send to packages.
2018-04-21 02:59:30 -03:00
if (intent === "Student.CheckAttendance") {
session.replaceDialog("/belagua-check-attendance", { entities: entities });
}
else if (intent === 'User.Authenticate') {
session.replaceDialog("/belagua-user-login", { entities: entities });
}
else if (intent === "PerguntarSobreTermo") {
session.send(
"Vou mostrar um menu para ajudar você a formular sua pergunta..."
);
session.replaceDialog("/menu");
} else if (intent === "ShowSubjectMenu") {
session.replaceDialog("/menu");
} else {
try {
session.replaceDialog("/" + intent);
} catch (error) {
logger.trace("error: intent: [" + intent + "] error: [" + error + "]");
session.sendTyping();
session.send("Desculpe-me, não encontrei nada a respeito...");
session.replaceDialog("/ask", {isReturning: true});
}
}
cb({ intent, entities }, null);
} else {
session.sendTyping();
session.send("Lamento, não achei nada a respeito...");
session.replaceDialog("/ask", {isReturning: true});
cb(null, null);
}
}
);
}
2018-04-21 02:59:30 -03:00
}