botserver/deploy/kb.gbapp/dialogs/AskDialog.ts

228 lines
7.9 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, |
2018-09-11 19:40:53 -03:00
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
2018-04-21 02:59:30 -03:00
| 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. |
| |
\*****************************************************************************/
2018-09-14 12:56:54 -03:00
"use strict";
2018-04-21 02:59:30 -03:00
2018-09-14 12:56:54 -03:00
import { IGBDialog } from "botlib";
import { AzureText } from "pragmatismo-io-framework";
import { GBMinInstance } from "botlib";
import { KBService } from "./../services/KBService";
import { BotAdapter } from "botbuilder";
import { Messages } from "../strings";
import { LuisRecognizer } from "botbuilder-ai";
import { GuaribasQuestion } from "../models";
2018-04-21 02:59:30 -03:00
2018-09-14 12:56:54 -03:00
const logger = require("../../../src/logger");
2018-04-21 02:59:30 -03:00
export class AskDialog extends IGBDialog {
2018-09-09 14:39:37 -03:00
/**
* Setup dialogs flows and define services call.
2018-09-14 12:56:54 -03:00
*
2018-09-09 14:39:37 -03:00
* @param bot The bot adapter.
* @param min The minimal bot instance data.
*/
static setup(bot: BotAdapter, min: GBMinInstance) {
2018-09-14 12:56:54 -03:00
const service = new KBService(min.core.sequelize);
2018-04-21 02:59:30 -03:00
const model = new LuisRecognizer({
appId: min.instance.nlpAppId,
subscriptionKey: min.instance.nlpSubscriptionKey,
serviceEndpoint: min.instance.nlpServerUrl
2018-09-14 12:56:54 -03:00
});
min.dialogs.add("/answerEvent", [
async (dc, args) => {
if (args && args.questionId) {
let question = await service.getQuestionById(min.instance.instanceId, args.questionId);
let answer = await service.getAnswerById(min.instance.instanceId, question.answerId)
// Sends the answer to all outputs, including projector.
await service.sendAnswer(
min.conversationalService,
dc,
answer
);
await dc.replace("/ask", { isReturning: true });
}
}])
min.dialogs.add("/answer", [
async (dc, args) => {
// Initialize values.
2018-09-14 12:56:54 -03:00
const user = min.userState.get(dc.context);
let text = args.query;
2018-09-10 16:24:32 -03:00
if (!text) {
2018-09-14 12:56:54 -03:00
throw new Error(`/answer being called with no args.query text.`);
2018-09-10 16:24:32 -03:00
}
let locale = dc.context.activity.locale;
2018-09-10 16:24:32 -03:00
// Stops any content on projector.
2018-09-14 12:56:54 -03:00
await min.conversationalService.sendEvent(dc, "stop", null);
2018-04-21 02:59:30 -03:00
// Handle extra text from FAQ.
if (args && args.query) {
2018-09-14 12:56:54 -03:00
text = args.query;
2018-04-21 02:59:30 -03:00
} else if (args && args.fromFaq) {
2018-09-14 12:56:54 -03:00
await dc.context.sendActivity(Messages[locale].going_answer);
2018-04-21 02:59:30 -03:00
}
// Spells check the input text before sending Search or NLP.
2018-09-10 16:24:32 -03:00
if (min.instance.spellcheckerKey) {
let data = await AzureText.getSpelledText(
min.instance.spellcheckerKey,
2018-09-14 12:56:54 -03:00
text
);
2018-09-10 16:24:32 -03:00
if (data != text) {
2018-09-14 12:56:54 -03:00
logger.info(`Spelling corrected: ${data}`);
text = data;
2018-09-10 16:24:32 -03:00
}
}
// Searches KB for the first time.
2018-09-14 12:56:54 -03:00
user.lastQuestion = text;
2018-08-28 19:16:29 -03:00
let resultsA = await service.ask(
min.instance,
text,
min.instance.searchScore,
2018-09-14 12:56:54 -03:00
user.subjects
);
2018-08-28 19:16:29 -03:00
// If there is some result, answer immediately.
2018-08-28 19:16:29 -03:00
if (resultsA && resultsA.answer) {
// Saves some context info.
2018-09-14 12:56:54 -03:00
user.isAsking = false;
user.lastQuestionId = resultsA.questionId;
2018-08-28 19:16:29 -03:00
// Sends the answer to all outputs, including projector.
2018-09-14 12:56:54 -03:00
await service.sendAnswer(
min.conversationalService,
dc,
resultsA.answer
);
2018-08-28 19:16:29 -03:00
// Goes to ask loop, again.
2018-09-14 12:56:54 -03:00
await dc.replace("/ask", { isReturning: true });
2018-08-28 19:16:29 -03:00
} else {
// Second time running Search, now with no filter.
2018-09-14 12:56:54 -03:00
let resultsB = await service.ask(
min.instance,
text,
min.instance.searchScore,
null
);
2018-08-28 19:16:29 -03:00
// If there is some result, answer immediately.
2018-08-28 19:16:29 -03:00
if (resultsB && resultsB.answer) {
// Saves some context info.
2018-09-14 12:56:54 -03:00
const user = min.userState.get(dc.context);
user.isAsking = false;
user.lastQuestionId = resultsB.questionId;
2018-09-10 16:24:32 -03:00
// Informs user that a broader search will be used.
2018-08-28 19:16:29 -03:00
if (user.subjects.length > 0) {
2018-09-14 12:56:54 -03:00
let subjectText = `${KBService.getSubjectItemsSeparatedBySpaces(
user.subjects
)}`;
await dc.context.sendActivity(Messages[locale].wider_answer);
}
2018-08-28 19:16:29 -03:00
// Sends the answer to all outputs, including projector.
2018-09-14 12:56:54 -03:00
await service.sendAnswer(
min.conversationalService,
dc,
resultsB.answer
);
await dc.replace("/ask", { isReturning: true });
2018-08-28 19:16:29 -03:00
} else {
if (!(await min.conversationalService.routeNLP(dc, min, text))) {
2018-09-14 12:56:54 -03:00
await dc.context.sendActivity(Messages[locale].did_not_find);
await dc.replace("/ask", { isReturning: true });
2018-09-10 16:24:32 -03:00
}
}
2018-08-28 19:16:29 -03:00
}
2018-04-21 02:59:30 -03:00
}
2018-09-14 12:56:54 -03:00
]);
2018-04-21 02:59:30 -03:00
min.dialogs.add("/ask", [
async (dc, args) => {
2018-09-14 12:56:54 -03:00
const locale = dc.context.activity.locale;
const user = min.userState.get(dc.context);
user.isAsking = true;
if (!user.subjects) {
2018-09-14 12:56:54 -03:00
user.subjects = [];
}
2018-09-14 12:56:54 -03:00
let text = [];
// Three forms of asking.
2018-09-16 18:17:28 -03:00
if (args && args.firstTime) {
text = Messages[locale].ask_first_time;
} else if (args && args.isReturning) {
2018-09-14 12:56:54 -03:00
text = Messages[locale].anything_else;
} else if (user.subjects.length > 0) {
text = Messages[locale].which_question;
} else {
throw new Error("Invalid use of /ask");
}
2018-09-14 12:56:54 -03:00
if (text.length > 0) {
await dc.prompt("textPrompt", text);
2018-04-21 02:59:30 -03:00
}
},
async (dc, value) => {
2018-09-14 12:56:54 -03:00
await dc.endAll();
await dc.begin("/answer", { query: value });
}
2018-09-14 12:56:54 -03:00
]);
2018-04-21 02:59:30 -03:00
}
}