botserver/deploy/core.gbapp/services/GBConversationalService.ts

107 lines
4.5 KiB
TypeScript
Raw Normal View History

2018-04-21 02:59:30 -03:00
/*****************************************************************************\
2018-09-10 16:24:32 -03:00
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
2018-04-21 02:59:30 -03:00
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
2018-09-10 16:24:32 -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-09-10 16:24:32 -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-04-21 02:59:30 -03:00
\*****************************************************************************/
2018-09-11 19:40:53 -03:00
"use strict"
2018-04-21 02:59:30 -03:00
2018-09-11 19:40:53 -03:00
const logger = require("../../../src/logger")
2018-04-21 02:59:30 -03:00
2018-09-11 19:40:53 -03:00
import { GBCoreService } from "./GBCoreService"
import { IGBConversationalService } from "botlib"
import { GBMinInstance } from "botlib"
import { LuisRecognizer } from "botbuilder-ai"
import { MessageFactory } from "botbuilder"
2018-04-21 02:59:30 -03:00
export interface LanguagePickerSettings {
2018-09-11 19:40:53 -03:00
defaultLocale?: string
supportedLocales?: string[]
}
2018-09-09 14:39:37 -03:00
export class GBConversationalService implements IGBConversationalService {
2018-09-11 19:40:53 -03:00
coreService: GBCoreService
constructor(coreService: GBCoreService) {
2018-09-11 19:40:53 -03:00
this.coreService = coreService
}
getCurrentLanguage(dc: any) {
2018-09-11 19:40:53 -03:00
return dc.context.activity.locale
}
async sendEvent(dc: any, name: string, value: any): Promise<any> {
2018-09-11 19:40:53 -03:00
const msg = MessageFactory.text("")
msg.value = value
msg.type = "event"
msg.name = name
return dc.context.sendActivity(msg)
}
async runNLP(dc: any, min: GBMinInstance, text: string): Promise<any> {
// Invokes LUIS.
const model = new LuisRecognizer({
appId: min.instance.nlpAppId,
subscriptionKey: min.instance.nlpSubscriptionKey,
serviceEndpoint: min.instance.nlpServerUrl
2018-09-11 19:40:53 -03:00
})
let res = await model.recognize(dc.context)
// Resolves intents returned from LUIS.
2018-09-11 19:40:53 -03:00
let topIntent = LuisRecognizer.topIntent(res)
if (topIntent) {
2018-09-11 19:40:53 -03:00
var intent = topIntent
var entity =
res.entities && res.entities.length > 0
? res.entities[0].entity.toUpperCase()
2018-09-11 19:40:53 -03:00
: null
logger.info("luis: intent: [" + intent + "] entity: [" + entity + "]")
try {
2018-09-11 19:40:53 -03:00
await dc.replace("/" + intent)
} catch (error) {
2018-09-11 19:40:53 -03:00
logger.info("error: intent: [" + intent + "] error: [" + error + "]")
await dc.context.sendActivity(
"Desculpe-me, não encontrei nada a respeito..."
2018-09-11 19:40:53 -03:00
)
await dc.replace("/ask", { isReturning: true })
}
2018-09-11 19:40:53 -03:00
return Promise.resolve({ intent, entities: res.entities })
} else {
2018-09-11 19:40:53 -03:00
await dc.context.sendActivity("Lamento, não achei nada a respeito...")
await dc.replace("/ask", { isReturning: true })
2018-09-11 19:40:53 -03:00
return Promise.resolve(null)
}
}
2018-04-21 02:59:30 -03:00
}