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-06-04 05:33:37 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
"use strict"
|
2018-06-04 05:33:37 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
const UrlJoin = require("url-join")
|
2018-09-11 12:04:50 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
import { BotAdapter, CardFactory, MessageFactory } from "botbuilder"
|
|
|
|
import { IGBDialog } from "botlib"
|
|
|
|
import { GBMinInstance } from "botlib"
|
|
|
|
import { GuaribasSubject } from '../models'
|
|
|
|
import { KBService } from "../services/KBService"
|
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.
|
|
|
|
*
|
|
|
|
* @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-04-21 02:59:30 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
var service = new KBService(min.core.sequelize)
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
bot
|
2018-06-04 05:33:37 -03:00
|
|
|
min.dialogs.add("/menu", [
|
|
|
|
async (dc, args) => {
|
2018-09-11 19:40:53 -03:00
|
|
|
var rootSubjectId = null
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
// var msg = dc.message TODO: message from Where in V4?
|
2018-06-04 05:33:37 -03:00
|
|
|
// if (msg.attachments && msg.attachments.length > 0) {
|
2018-09-11 19:40:53 -03:00
|
|
|
// var attachment = msg.attachments[0]
|
2018-06-04 05:33:37 -03:00
|
|
|
// }
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
if (args && args.data) {
|
2018-09-11 19:40:53 -03:00
|
|
|
var subject = JSON.parse(args.data) // ?
|
2018-06-04 05:33:37 -03:00
|
|
|
|
2018-09-11 12:04:50 -03:00
|
|
|
// If there is a shortcut specified as subject destination, go there.
|
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
if (subject.to) {
|
2018-09-11 19:40:53 -03:00
|
|
|
let dialog = subject.to.split(":")[1]
|
|
|
|
await dc.replace("/" + dialog)
|
|
|
|
await dc.end()
|
|
|
|
return
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2018-09-11 19:40:53 -03:00
|
|
|
const user = min.userState.get(dc.context)
|
|
|
|
user.subjects.push(subject)
|
|
|
|
rootSubjectId = subject.subjectId
|
2018-06-04 05:33:37 -03:00
|
|
|
|
|
|
|
if (user.subjects.length > 0) {
|
2018-09-11 19:40:53 -03:00
|
|
|
let data = await service.getFaqBySubjectArray("menu", user.subjects)
|
2018-09-09 14:39:37 -03:00
|
|
|
await min.conversationalService.sendEvent(dc, "play", {
|
|
|
|
playerType: "bullet",
|
|
|
|
data: data.slice(0, 6)
|
2018-09-11 19:40:53 -03:00
|
|
|
})
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-11 19:40:53 -03:00
|
|
|
const user = min.userState.get(dc.context)
|
|
|
|
user.subjects = []
|
2018-06-04 05:33:37 -03:00
|
|
|
|
2018-09-11 12:04:50 -03:00
|
|
|
let messages = [
|
|
|
|
"Aqui estão algumas categorias de assuntos...",
|
|
|
|
"Selecionando o assunto você pode me ajudar a encontrar a resposta certa...",
|
|
|
|
"Você pode selecionar algum dos assuntos abaixo e perguntar algo..."
|
2018-09-11 19:40:53 -03:00
|
|
|
]
|
|
|
|
await dc.context.sendActivity(messages[0]) // TODO: Handle rnd.
|
|
|
|
user.isAsking = false
|
2018-06-04 05:33:37 -03:00
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
const msg = MessageFactory.text('')
|
|
|
|
var attachments = []
|
2018-06-04 05:33:37 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
let data = await service.getSubjectItems(
|
2018-06-04 05:33:37 -03:00
|
|
|
min.instance.instanceId,
|
2018-09-11 19:40:53 -03:00
|
|
|
rootSubjectId)
|
2018-06-04 05:33:37 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
msg.attachmentLayout = 'carousel'
|
2018-09-09 14:39:37 -03:00
|
|
|
|
|
|
|
data.forEach(function (item: GuaribasSubject) {
|
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
var subject = item
|
2018-09-09 14:39:37 -03:00
|
|
|
var card = CardFactory.heroCard(
|
|
|
|
subject.title,
|
|
|
|
CardFactory.images([UrlJoin(
|
|
|
|
"/kb",
|
|
|
|
min.instance.kb,
|
|
|
|
"subjects",
|
|
|
|
subject.internalId + ".png" // TODO: or fallback to subject.png
|
|
|
|
)]),
|
|
|
|
CardFactory.actions([
|
|
|
|
{
|
|
|
|
type: 'postBack',
|
|
|
|
title: 'Selecionar',
|
|
|
|
value: JSON.stringify({
|
|
|
|
title: subject.title,
|
|
|
|
subjectId: subject.subjectId,
|
|
|
|
to: subject.to
|
|
|
|
})
|
2018-09-11 19:40:53 -03:00
|
|
|
}]))
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
attachments.push(card)
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
})
|
2018-09-09 14:39:37 -03:00
|
|
|
|
|
|
|
if (attachments.length == 0) {
|
2018-09-11 19:40:53 -03:00
|
|
|
const user = min.userState.get(dc.context)
|
2018-09-09 14:39:37 -03:00
|
|
|
if (user.subjects && user.subjects.length > 0) {
|
|
|
|
await dc.context.sendActivity(
|
|
|
|
`Vamos pesquisar sobre ${KBService.getFormattedSubjectItems(
|
|
|
|
user.subjects
|
|
|
|
)}?`
|
2018-09-11 19:40:53 -03:00
|
|
|
)
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
await dc.replace("/ask", {})
|
2018-09-09 14:39:37 -03:00
|
|
|
} else {
|
2018-09-11 19:40:53 -03:00
|
|
|
msg.attachments = attachments
|
|
|
|
await dc.context.sendActivity(msg)
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
const user = min.userState.get(dc.context)
|
|
|
|
user.isAsking = true
|
2018-06-04 05:33:37 -03:00
|
|
|
},
|
|
|
|
async (dc, value) => {
|
2018-09-11 19:40:53 -03:00
|
|
|
var text = value
|
2018-09-11 12:04:50 -03:00
|
|
|
if (text === "no" || text === "n") { // TODO: Migrate to a common.
|
2018-09-11 19:40:53 -03:00
|
|
|
await dc.replace("/feedback")
|
2018-06-04 05:33:37 -03:00
|
|
|
} else {
|
2018-09-11 19:40:53 -03:00
|
|
|
await dc.replace("/ask")
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2018-06-04 05:33:37 -03:00
|
|
|
}
|
2018-09-11 19:40:53 -03:00
|
|
|
])
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|