2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| 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-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'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";
|
2018-11-01 21:06:11 -03:00
|
|
|
import { WaterfallDialog } from "botbuilder-dialogs";
|
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.
|
|
|
|
*/
|
2018-06-04 05:33:37 -03:00
|
|
|
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
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog("/answerEvent", [
|
|
|
|
async step => {
|
|
|
|
if (step.options && step.options["questionId"]) {
|
|
|
|
let question = await service.getQuestionById(
|
|
|
|
min.instance.instanceId,
|
|
|
|
step.options["questionId"]
|
|
|
|
);
|
|
|
|
let answer = await service.getAnswerById(
|
|
|
|
min.instance.instanceId,
|
|
|
|
question.answerId
|
|
|
|
);
|
2018-09-20 12:35:47 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
// Sends the answer to all outputs, including projector.
|
2018-09-20 12:35:47 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
await service.sendAnswer(min.conversationalService, step, answer);
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
await step.replaceDialog("/ask", { isReturning: true });
|
|
|
|
}
|
|
|
|
return await step.next();
|
2018-09-10 16:24:32 -03:00
|
|
|
}
|
2018-11-04 09:19:03 -02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog("/answer", [
|
|
|
|
async step => {
|
|
|
|
const user = await min.userProfile.get(step.context, {});
|
|
|
|
let text = step.options["query"];
|
|
|
|
if (!text) {
|
|
|
|
throw new Error(`/answer being called with no args.query text.`);
|
|
|
|
}
|
2018-09-10 16:24:32 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
let locale = step.context.activity.locale;
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
// Stops any content on projector.
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
await min.conversationalService.sendEvent(step, "stop", null);
|
2018-06-04 20:27:21 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
// Handle extra text from FAQ.
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
if (step.options && step.options["query"]) {
|
|
|
|
text = step.options["query"];
|
|
|
|
} else if (step.options && step.options["fromFaq"]) {
|
|
|
|
await step.context.sendActivity(Messages[locale].going_answer);
|
2018-09-10 16:24:32 -03:00
|
|
|
}
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
// Spells check the input text before sending Search or NLP.
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
if (min.instance.spellcheckerKey) {
|
|
|
|
let data = await AzureText.getSpelledText(
|
|
|
|
min.instance.spellcheckerKey,
|
|
|
|
text
|
|
|
|
);
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
if (data != text) {
|
|
|
|
logger.info(`Spelling corrected: ${data}`);
|
|
|
|
text = data;
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
// Searches KB for the first time.
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
user.lastQuestion = text;
|
2018-11-01 21:39:37 -03:00
|
|
|
await min.userProfile.set(step.context, user);
|
2018-11-04 09:19:03 -02:00
|
|
|
let resultsA = await service.ask(
|
2018-09-14 12:56:54 -03:00
|
|
|
min.instance,
|
|
|
|
text,
|
|
|
|
min.instance.searchScore,
|
2018-11-04 09:19:03 -02:00
|
|
|
user.subjects
|
2018-09-14 12:56:54 -03:00
|
|
|
);
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-08-28 19:16:29 -03:00
|
|
|
// If there is some result, answer immediately.
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
if (resultsA && resultsA.answer) {
|
2018-08-28 19:16:29 -03:00
|
|
|
// Saves some context info.
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2018-09-14 12:56:54 -03:00
|
|
|
user.isAsking = false;
|
2018-11-04 09:19:03 -02:00
|
|
|
user.lastQuestionId = resultsA.questionId;
|
2018-11-01 21:39:37 -03:00
|
|
|
await min.userProfile.set(step.context, user);
|
2018-08-28 17:50:19 -03:00
|
|
|
|
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,
|
2018-11-01 21:06:11 -03:00
|
|
|
step,
|
2018-11-04 09:19:03 -02:00
|
|
|
resultsA.answer
|
2018-09-14 12:56:54 -03:00
|
|
|
);
|
2018-11-04 09:19:03 -02:00
|
|
|
|
|
|
|
// Goes to ask loop, again.
|
|
|
|
|
|
|
|
return await step.replaceDialog("/ask", { isReturning: true });
|
2018-08-28 19:16:29 -03:00
|
|
|
} else {
|
2018-11-04 09:19:03 -02:00
|
|
|
// Second time running Search, now with no filter.
|
|
|
|
|
|
|
|
let resultsB = await service.ask(
|
|
|
|
min.instance,
|
|
|
|
text,
|
|
|
|
min.instance.searchScore,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
// If there is some result, answer immediately.
|
|
|
|
|
|
|
|
if (resultsB && resultsB.answer) {
|
|
|
|
// Saves some context info.
|
|
|
|
|
|
|
|
const user = await min.userProfile.get(step.context, {});
|
|
|
|
|
|
|
|
user.isAsking = false;
|
|
|
|
user.lastQuestionId = resultsB.questionId;
|
|
|
|
await min.userProfile.set(step.context, user);
|
|
|
|
|
|
|
|
// Informs user that a broader search will be used.
|
|
|
|
|
|
|
|
if (user.subjects.length > 0) {
|
|
|
|
let subjectText = `${KBService.getSubjectItemsSeparatedBySpaces(
|
|
|
|
user.subjects
|
|
|
|
)}`;
|
|
|
|
await step.context.sendActivity(Messages[locale].wider_answer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends the answer to all outputs, including projector.
|
|
|
|
|
|
|
|
await service.sendAnswer(
|
|
|
|
min.conversationalService,
|
|
|
|
step,
|
|
|
|
resultsB.answer
|
|
|
|
);
|
|
|
|
return await step.replaceDialog("/ask", { isReturning: true });
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
!(await min.conversationalService.routeNLP(step, min, text))
|
|
|
|
) {
|
|
|
|
await step.context.sendActivity(Messages[locale].did_not_find);
|
|
|
|
return await step.replaceDialog("/ask", { isReturning: true });
|
|
|
|
}
|
2018-09-10 16:24:32 -03:00
|
|
|
}
|
2018-08-28 17:50:19 -03:00
|
|
|
}
|
2018-08-28 19:16:29 -03:00
|
|
|
}
|
2018-11-04 09:19:03 -02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog("/ask", [
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const user = await min.userProfile.get(step.context, {});
|
|
|
|
user.isAsking = true;
|
|
|
|
if (!user.subjects) {
|
|
|
|
user.subjects = [];
|
|
|
|
}
|
|
|
|
let text;
|
2018-09-14 12:56:54 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
// Three forms of asking.
|
|
|
|
|
|
|
|
if (step.options && step.options["firstTime"]) {
|
|
|
|
text = Messages[locale].ask_first_time;
|
|
|
|
} else if (step.options && step.options["isReturning"]) {
|
|
|
|
text = Messages[locale].anything_else;
|
|
|
|
} else if (user.subjects.length > 0) {
|
|
|
|
text = Messages[locale].which_question;
|
|
|
|
} else {
|
|
|
|
throw new Error("Invalid use of /ask");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text.length > 0) {
|
|
|
|
return await step.prompt("textPrompt", text);
|
|
|
|
}
|
|
|
|
return await step.next();
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
return await step.replaceDialog("/answer", { query: step.result });
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2018-11-04 09:19:03 -02:00
|
|
|
])
|
|
|
|
);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|