botserver/packages/kb.gbapp/dialogs/MenuDialog.ts

169 lines
6.5 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. |
| |
\*****************************************************************************/
"use strict";
const UrlJoin = require("url-join");
import { BotAdapter, CardFactory, MessageFactory } from "botbuilder";
import { IGBDialog } from "botlib";
import { GBMinInstance } from "botlib";
import { GuaribasSubject } from "../models";
import { KBService } from "../services/KBService";
import { Messages } from "../strings";
import { AzureText } from "pragmatismo-io-framework";
import { WaterfallDialog } from "botbuilder-dialogs";
2018-04-21 02:59:30 -03:00
export class MenuDialog 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) {
var service = new KBService(min.core.sequelize);
2018-04-21 02:59:30 -03:00
min.dialogs.add(new WaterfallDialog("/menu", [
async step => {
const locale = step.context.activity.locale;
var rootSubjectId = null;
2018-04-21 02:59:30 -03:00
2018-11-01 21:39:37 -03:00
if (step.options && step.options["data"]) {
var subject = step.result.data;
// If there is a shortcut specified as subject destination, go there.
if (subject.to) {
let dialog = subject.to.split(":")[1];
await step.replaceDialog("/" + dialog);
await step.endDialog();
return;
2018-04-21 02:59:30 -03:00
}
// Adds to bot a perception of a new subject.
const user = await min.userProfile.get(context, {});
user.subjects.push(subject);
rootSubjectId = subject.subjectId;
// Whenever a subject is selected, shows a faq about it.
2018-09-14 12:56:54 -03:00
if (user.subjects.length > 0) {
2018-09-14 12:56:54 -03:00
let data = await service.getFaqBySubjectArray(
"menu",
user.subjects
);
await min.conversationalService.sendEvent(step, "play", {
2018-09-09 14:39:37 -03:00
playerType: "bullet",
data: data.slice(0, 10)
});
}
} else {
const user = await min.userProfile.get(context, {});
user.subjects = [];
2018-09-14 12:56:54 -03:00
await step.context.sendActivity(Messages[locale].here_is_subjects); // TODO: Handle rnd.
user.isAsking = false;
}
2018-04-21 02:59:30 -03:00
const msg = MessageFactory.text("");
var attachments = [];
2018-09-09 14:39:37 -03:00
let data = await service.getSubjectItems(
min.instance.instanceId,
2018-09-14 12:56:54 -03:00
rootSubjectId
);
2018-09-09 14:39:37 -03:00
msg.attachmentLayout = "carousel";
2018-09-09 14:39:37 -03:00
2018-09-14 12:56:54 -03:00
data.forEach(function(item: GuaribasSubject) {
var subject = item;
2018-09-09 14:39:37 -03:00
var card = CardFactory.heroCard(
subject.title,
subject.description,
2018-09-14 12:56:54 -03:00
CardFactory.images([
UrlJoin("/kb", min.instance.kb, "subjects", "subject.png")
2018-09-14 12:56:54 -03:00
]),
2018-09-09 14:39:37 -03:00
CardFactory.actions([
{
2018-09-14 12:56:54 -03:00
type: "postBack",
title: Messages[locale].menu_select,
2018-09-09 14:39:37 -03:00
value: JSON.stringify({
title: subject.title,
description: subject.description,
2018-09-09 14:39:37 -03:00
subjectId: subject.subjectId,
internalId: subject.internalId,
2018-09-09 14:39:37 -03:00
to: subject.to
})
2018-09-14 12:56:54 -03:00
}
])
);
2018-09-09 14:39:37 -03:00
attachments.push(card);
});
2018-09-09 14:39:37 -03:00
if (attachments.length == 0) {
const user = await min.userProfile.get(context, {});
2018-09-09 14:39:37 -03:00
if (user.subjects && user.subjects.length > 0) {
await step.context.sendActivity(
2018-09-14 12:56:54 -03:00
Messages[locale].lets_search(
KBService.getFormattedSubjectItems(user.subjects)
)
);
2018-04-21 02:59:30 -03:00
}
2018-09-09 14:39:37 -03:00
await step.replaceDialog("/ask", {});
2018-09-09 14:39:37 -03:00
} else {
msg.attachments = attachments;
await step.context.sendActivity(msg);
2018-09-09 14:39:37 -03:00
}
const user = await min.userProfile.get(context, {});
user.isAsking = true;
return await step.next();
},
async step => {
var text = step.result;
const locale = step.context.activity.locale;
2018-09-14 12:56:54 -03:00
if (AzureText.isIntentNo(locale, text)) {
await step.replaceDialog("/feedback");
} else {
await step.replaceDialog("/ask");
2018-04-21 02:59:30 -03:00
}
return await step.next();
}
]));
2018-04-21 02:59:30 -03:00
}
}