2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2020-07-01 15:00:40 -03:00
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
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
|
|
|
|
2020-05-27 23:01:44 -03:00
|
|
|
import { GBServer } from '../../../src/app';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { BotAdapter } from 'botbuilder';
|
|
|
|
import { WaterfallDialog } from 'botbuilder-dialogs';
|
2020-07-26 16:46:37 -03:00
|
|
|
import { GBLog, GBMinInstance, IGBDialog, IGBPackage } from 'botlib';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { Messages } from '../strings';
|
|
|
|
import { KBService } from './../services/KBService';
|
2020-01-10 10:04:26 -03:00
|
|
|
import { GuaribasAnswer } from '../models';
|
|
|
|
import { GBMinService } from '../../../packages/core.gbapp/services/GBMinService';
|
2020-07-26 16:46:37 -03:00
|
|
|
import { SecService } from '../../security.gbapp/services/SecService';
|
|
|
|
import { CollectionUtil, AzureText } from 'pragmatismo-io-framework';
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
/**
|
|
|
|
* Dialog arguments.
|
|
|
|
*/
|
|
|
|
export class AskDialogArgs {
|
|
|
|
public questionId: number;
|
|
|
|
public fromFaq: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the ask loop on knowledge base data or delegate to other services.
|
|
|
|
*/
|
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-11-12 12:20:44 -02:00
|
|
|
public static setup(bot: BotAdapter, min: GBMinInstance) {
|
2018-09-14 12:56:54 -03:00
|
|
|
const service = new KBService(min.core.sequelize);
|
2019-03-09 16:59:31 -03:00
|
|
|
min.dialogs.add(new WaterfallDialog('/answerEvent', AskDialog.getAnswerEventDialog(service, min)));
|
|
|
|
min.dialogs.add(new WaterfallDialog('/answer', AskDialog.getAnswerDialog(min, service)));
|
|
|
|
min.dialogs.add(new WaterfallDialog('/ask', AskDialog.getAskDialog(min)));
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
private static getAskDialog(min: GBMinInstance) {
|
|
|
|
return [
|
|
|
|
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;
|
|
|
|
// 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) {
|
2020-05-17 21:30:21 +00:00
|
|
|
return await min.conversationalService.prompt(min, step, text);
|
2019-03-09 16:59:31 -03:00
|
|
|
}
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
return await step.next();
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
if (step.result) {
|
2020-06-04 20:14:02 -03:00
|
|
|
const translatorEnabled = () => {
|
|
|
|
if (min.instance.params) {
|
|
|
|
const params = JSON.parse(min.instance.params);
|
2020-08-22 18:41:54 -03:00
|
|
|
return params ? params['Enable Worldwide Translator'] === 'TRUE' : false;
|
2020-06-04 20:14:02 -03:00
|
|
|
}
|
|
|
|
return false;
|
2020-08-22 18:41:54 -03:00
|
|
|
}; // TODO: Encapsulate.
|
2020-05-17 19:05:18 +00:00
|
|
|
|
|
|
|
let query = step.result;
|
2020-05-17 21:30:21 +00:00
|
|
|
|
2020-05-18 01:03:42 +00:00
|
|
|
let locale = 'pt';
|
2020-05-30 19:30:11 -03:00
|
|
|
const minBoot = GBServer.globals.minBoot as any;
|
2020-08-22 18:41:54 -03:00
|
|
|
if (process.env.TRANSLATOR_DISABLED !== 'true' && translatorEnabled()) {
|
|
|
|
locale = await AzureText.getLocale(
|
|
|
|
minBoot.instance.textAnalyticsKey ? minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey,
|
|
|
|
minBoot.instance.textAnalyticsEndpoint
|
|
|
|
? minBoot.instance.textAnalyticsEndpoint
|
|
|
|
: minBoot.instance.textAnalyticsEndpoint,
|
|
|
|
query
|
|
|
|
);
|
2020-05-30 19:30:11 -03:00
|
|
|
}
|
2020-05-27 23:01:44 -03:00
|
|
|
|
2020-05-17 21:30:21 +00:00
|
|
|
let sec = new SecService();
|
|
|
|
const member = step.context.activity.from;
|
|
|
|
|
2020-08-22 18:41:54 -03:00
|
|
|
const user = await sec.ensureUser(min.instance.instanceId, member.id, member.name, '', 'web', member.name);
|
2020-05-17 21:30:21 +00:00
|
|
|
user.locale = locale;
|
|
|
|
await user.save();
|
2020-07-26 16:46:37 -03:00
|
|
|
const notTranslatedQuery = query;
|
2020-08-22 18:41:54 -03:00
|
|
|
query = await min.conversationalService.translate(
|
|
|
|
min,
|
2020-05-30 19:30:11 -03:00
|
|
|
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
|
|
|
|
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
|
2020-05-17 21:30:21 +00:00
|
|
|
query,
|
2020-08-22 18:41:54 -03:00
|
|
|
'pt'
|
|
|
|
);
|
|
|
|
GBLog.info(`Translated text: ${query}.`);
|
2020-07-26 16:46:37 -03:00
|
|
|
|
|
|
|
let handled = false;
|
|
|
|
await CollectionUtil.asyncForEach(min.appPackages, async (e: IGBPackage) => {
|
2020-08-22 18:41:54 -03:00
|
|
|
if (
|
|
|
|
await e.onExchangeData(min, 'handleAnswer', {
|
|
|
|
query: query,
|
|
|
|
step: step,
|
|
|
|
notTranslatedQuery: notTranslatedQuery,
|
|
|
|
message: query,
|
|
|
|
user: user ? user['dataValues'] : null
|
|
|
|
})
|
|
|
|
) {
|
2020-07-26 16:46:37 -03:00
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!handled) {
|
|
|
|
return await step.beginDialog('/answer', {
|
|
|
|
query: query
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return await step.next();
|
|
|
|
}
|
2019-03-09 16:59:31 -03:00
|
|
|
} else {
|
2018-11-04 09:19:03 -02:00
|
|
|
return await step.next();
|
2018-09-10 16:24:32 -03:00
|
|
|
}
|
2019-03-09 16:59:31 -03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
private static getAnswerDialog(min: GBMinInstance, service: KBService) {
|
|
|
|
return [
|
|
|
|
async step => {
|
|
|
|
const user = await min.userProfile.get(step.context, {});
|
|
|
|
let text = step.options.query;
|
2020-05-17 21:30:21 +00:00
|
|
|
|
|
|
|
let sec = new SecService();
|
|
|
|
const member = step.context.activity.from;
|
2020-08-22 18:41:54 -03:00
|
|
|
const userDb = await sec.ensureUser(min.instance.instanceId, member.id, member.name, '', 'web', member.name);
|
2020-05-30 19:30:11 -03:00
|
|
|
const minBoot = GBServer.globals.minBoot as any;
|
2020-08-22 18:41:54 -03:00
|
|
|
text = await min.conversationalService.translate(
|
|
|
|
min,
|
2020-05-30 19:30:11 -03:00
|
|
|
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
|
|
|
|
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
|
2020-05-17 21:30:21 +00:00
|
|
|
text,
|
|
|
|
userDb.locale ? userDb.locale : 'pt'
|
2020-05-27 23:01:44 -03:00
|
|
|
);
|
2020-05-17 21:30:21 +00:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
if (!text) {
|
|
|
|
throw new Error(`/answer being called with no args query text.`);
|
|
|
|
}
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
// Stops any content on projector.
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendEvent(min, step, 'stop', undefined);
|
2019-03-09 16:59:31 -03:00
|
|
|
// Handle extra text from FAQ.
|
|
|
|
if (step.options && step.options.query) {
|
|
|
|
text = step.options.query;
|
|
|
|
} else if (step.options && step.options.fromFaq) {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].going_answer);
|
2019-03-09 16:59:31 -03:00
|
|
|
}
|
|
|
|
// Spells check the input text before sending Search or NLP.
|
2020-06-03 16:43:35 -03:00
|
|
|
const key = min.instance.spellcheckerKey ? minBoot.instance.spellcheckerKey : min.instance.spellcheckerKey;
|
2020-06-03 22:15:24 -03:00
|
|
|
if (key) {
|
2019-03-09 16:59:31 -03:00
|
|
|
const data = await AzureText.getSpelledText(min.instance.spellcheckerKey, text);
|
|
|
|
if (data !== text) {
|
|
|
|
GBLog.info(`Spelling corrected: ${data}`);
|
|
|
|
text = data;
|
2018-11-04 09:19:03 -02:00
|
|
|
}
|
2019-03-09 16:59:31 -03:00
|
|
|
}
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2020-06-03 22:15:24 -03:00
|
|
|
const searchScore = min.instance.searchScore ? min.instance.searchScore : minBoot.instance.searchScore;
|
2019-03-09 16:59:31 -03:00
|
|
|
// Searches KB for the first time.
|
|
|
|
user.lastQuestion = text;
|
|
|
|
await min.userProfile.set(step.context, user);
|
2020-06-03 16:43:35 -03:00
|
|
|
const resultsA = await service.ask(min.instance, text, searchScore, user.subjects);
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
// If there is some result, answer immediately.
|
|
|
|
if (resultsA !== undefined && resultsA.answer !== undefined) {
|
|
|
|
// Saves some context info.
|
|
|
|
user.isAsking = false;
|
|
|
|
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
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
// Sends the answer to all outputs, including projector.
|
2019-08-24 12:22:52 -03:00
|
|
|
|
2020-01-10 10:04:26 -03:00
|
|
|
return await AskDialog.handleAnswer(service, min, step, resultsA.answer);
|
2019-03-09 16:59:31 -03:00
|
|
|
} else {
|
|
|
|
// Second time running Search, now with no filter.
|
2020-06-03 16:43:35 -03:00
|
|
|
const resultsB = await service.ask(min.instance, text, searchScore, undefined);
|
2018-08-28 19:16:29 -03:00
|
|
|
// If there is some result, answer immediately.
|
2018-08-28 17:50:19 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
if (resultsB !== undefined && resultsB.answer !== undefined) {
|
2018-08-28 19:16:29 -03:00
|
|
|
// Saves some context info.
|
2019-03-09 16:59:31 -03:00
|
|
|
const user2 = await min.userProfile.get(step.context, {});
|
|
|
|
user2.isAsking = false;
|
|
|
|
user2.lastQuestionId = resultsB.questionId;
|
|
|
|
await min.userProfile.set(step.context, user2);
|
2020-07-26 16:46:37 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
// Informs user that a broader search will be used.
|
|
|
|
if (user2.subjects.length > 0) {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].wider_answer);
|
2019-03-09 16:59:31 -03:00
|
|
|
}
|
2018-11-04 09:19:03 -02:00
|
|
|
|
2020-01-10 10:04:26 -03:00
|
|
|
if (resultsB.answer)
|
|
|
|
// Sends the answer to all outputs, including projector.
|
|
|
|
|
|
|
|
return await AskDialog.handleAnswer(service, min, step, resultsA.answer);
|
2018-08-28 19:16:29 -03:00
|
|
|
} else {
|
2019-03-09 16:59:31 -03:00
|
|
|
if (!(await min.conversationalService.routeNLP(step, min, text))) {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].did_not_find);
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
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
|
|
|
}
|
2019-03-09 16:59:31 -03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2020-01-10 10:04:26 -03:00
|
|
|
private static async handleAnswer(service: KBService, min: GBMinInstance, step: any, answer: GuaribasAnswer) {
|
2020-05-16 16:40:44 -03:00
|
|
|
if (answer.content.endsWith('.docx')) {
|
2020-06-03 21:31:00 -03:00
|
|
|
const mainName = answer.content.replace(/\s|\-/gi, '').split('.')[0];
|
2020-06-05 14:40:21 -03:00
|
|
|
return await GBMinService.callVM(mainName, min, step);
|
2020-01-10 10:04:26 -03:00
|
|
|
} else {
|
|
|
|
await service.sendAnswer(min, AskDialog.getChannel(step), step, answer);
|
2020-06-05 14:40:21 -03:00
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
2020-01-10 10:04:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:22:52 -03:00
|
|
|
private static getChannel(step): string {
|
2019-08-24 18:46:04 -03:00
|
|
|
return !isNaN(step.context.activity.from.id) ? 'whatsapp' : step.context.activity.channelId;
|
2019-08-24 12:22:52 -03:00
|
|
|
}
|
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
private static getAnswerEventDialog(service: KBService, min: GBMinInstance) {
|
|
|
|
return [
|
|
|
|
async step => {
|
|
|
|
const data = step.options as AskDialogArgs;
|
|
|
|
if (data !== undefined && data.questionId !== undefined) {
|
|
|
|
const question = await service.getQuestionById(min.instance.instanceId, data.questionId);
|
|
|
|
const answer = await service.getAnswerById(min.instance.instanceId, question.answerId);
|
|
|
|
// Sends the answer to all outputs, including projector.
|
2019-08-24 18:46:04 -03:00
|
|
|
await service.sendAnswer(min, AskDialog.getChannel(step), step, answer);
|
2019-03-09 16:59:31 -03:00
|
|
|
await step.replaceDialog('/ask', { isReturning: true });
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2019-02-28 15:15:51 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
return await step.next();
|
|
|
|
}
|
|
|
|
];
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2019-05-25 15:22:51 -03:00
|
|
|
}
|