2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
2018-09-10 16:24:32 -03:00
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2018-04-21 02:59:30 -03:00
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
2019-03-09 16:59:31 -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. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-09-10 16:24:32 -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-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-11-11 19:09:18 -02:00
|
|
|
/**
|
2018-11-27 22:56:11 -02:00
|
|
|
* @fileoverview Conversation handling and external service calls.
|
2018-11-11 19:09:18 -02:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
import { MessageFactory, RecognizerResult } from 'botbuilder';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { LuisRecognizer } from 'botbuilder-ai';
|
2019-03-08 17:05:58 -03:00
|
|
|
import { GBDialogStep, GBLog, GBMinInstance, IGBConversationalService, IGBCoreService } from 'botlib';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { AzureText } from 'pragmatismo-io-framework';
|
|
|
|
import { Messages } from '../strings';
|
|
|
|
const Nexmo = require('nexmo');
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2018-09-11 19:33:58 -03:00
|
|
|
export interface LanguagePickerSettings {
|
2018-09-14 12:56:54 -03:00
|
|
|
defaultLocale?: string;
|
|
|
|
supportedLocales?: string[];
|
2018-09-11 19:33:58 -03:00
|
|
|
}
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
/**
|
|
|
|
* Provides basic services for handling messages and dispatching to back-end
|
|
|
|
* services like NLP or Search.
|
|
|
|
*/
|
2018-09-11 19:33:58 -03:00
|
|
|
export class GBConversationalService implements IGBConversationalService {
|
2019-03-08 06:37:13 -03:00
|
|
|
public coreService: IGBCoreService;
|
|
|
|
|
|
|
|
constructor(coreService: IGBCoreService) {
|
2018-09-14 12:56:54 -03:00
|
|
|
this.coreService = coreService;
|
2018-09-11 19:33:58 -03:00
|
|
|
}
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
public getCurrentLanguage(step: GBDialogStep) {
|
2018-11-01 21:06:11 -03:00
|
|
|
return step.context.activity.locale;
|
2018-09-11 19:33:58 -03:00
|
|
|
}
|
|
|
|
|
2019-03-08 19:13:00 -03:00
|
|
|
public async sendEvent(step: GBDialogStep, name: string, value: Object): Promise<any> {
|
2018-11-12 12:20:44 -02:00
|
|
|
if (step.context.activity.channelId === 'webchat') {
|
|
|
|
const msg = MessageFactory.text('');
|
2018-09-24 11:04:36 -03:00
|
|
|
msg.value = value;
|
2018-11-12 12:20:44 -02:00
|
|
|
msg.type = 'event';
|
2018-09-24 11:04:36 -03:00
|
|
|
msg.name = name;
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
return step.context.sendActivity(msg);
|
2018-09-24 11:04:36 -03:00
|
|
|
}
|
2018-09-11 19:33:58 -03:00
|
|
|
}
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
// tslint:disable:no-unsafe-any due to Nexmo.
|
2018-11-27 22:56:11 -02:00
|
|
|
public async sendSms(min: GBMinInstance, mobile: string, text: string): Promise<any> {
|
|
|
|
return new Promise(
|
|
|
|
(resolve: any, reject: any): any => {
|
|
|
|
const nexmo = new Nexmo({
|
|
|
|
apiKey: min.instance.smsKey,
|
|
|
|
apiSecret: min.instance.smsSecret
|
|
|
|
});
|
2019-03-08 17:05:58 -03:00
|
|
|
// tslint:disable-next-line:no-unsafe-any
|
2018-11-27 22:56:11 -02:00
|
|
|
nexmo.message.sendSms(min.instance.smsServiceNumber, mobile, text, (err, data) => {
|
2018-09-24 11:04:36 -03:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(data);
|
|
|
|
}
|
2018-11-27 22:56:11 -02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
2019-03-08 17:05:58 -03:00
|
|
|
// tslint:enable:no-unsafe-any
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
public async routeNLP(step: GBDialogStep, min: GBMinInstance, text: string): Promise<boolean> {
|
2019-02-28 15:15:51 -03:00
|
|
|
|
2018-09-11 19:33:58 -03:00
|
|
|
const model = new LuisRecognizer({
|
2018-10-14 11:38:40 -03:00
|
|
|
applicationId: min.instance.nlpAppId,
|
2018-10-14 19:58:54 -03:00
|
|
|
endpointKey: min.instance.nlpKey,
|
2019-06-21 08:59:42 -03:00
|
|
|
endpoint: min.instance.nlpEndpoint
|
2018-09-14 12:56:54 -03:00
|
|
|
});
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
let nlp: RecognizerResult;
|
2018-09-24 15:27:26 -03:00
|
|
|
try {
|
2018-11-01 21:06:11 -03:00
|
|
|
nlp = await model.recognize(step.context);
|
2018-09-24 15:27:26 -03:00
|
|
|
} catch (error) {
|
2019-03-08 17:05:58 -03:00
|
|
|
// tslint:disable:no-unsafe-any
|
2018-11-27 22:56:11 -02:00
|
|
|
if (error.statusCode === 404) {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.warn('NLP application still not publish and there are no other options for answering.');
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
return Promise.resolve(false);
|
2018-11-12 12:20:44 -02:00
|
|
|
} else {
|
2018-11-27 22:56:11 -02:00
|
|
|
const msg = `Error calling NLP, check if you have a published model and assigned keys. Error: ${
|
|
|
|
error.statusCode ? error.statusCode : ''
|
2019-06-21 08:59:42 -03:00
|
|
|
} {error.message; }`;
|
2018-11-04 09:19:03 -02:00
|
|
|
|
2018-11-27 22:56:11 -02:00
|
|
|
return Promise.reject(new Error(msg));
|
|
|
|
}
|
2019-03-08 17:05:58 -03:00
|
|
|
// tslint:enable:no-unsafe-any
|
2018-09-24 15:27:26 -03:00
|
|
|
}
|
2018-09-11 19:33:58 -03:00
|
|
|
|
2019-07-19 13:35:11 -03:00
|
|
|
let nlpActive = false;
|
|
|
|
|
|
|
|
Object.keys(nlp.intents).forEach((name) => {
|
|
|
|
const score = nlp.intents[name].score;
|
|
|
|
if (score > min.instance.nlpScore){
|
|
|
|
nlpActive = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-11 19:33:58 -03:00
|
|
|
// Resolves intents returned from LUIS.
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const topIntent = LuisRecognizer.topIntent(nlp);
|
2019-07-19 13:35:11 -03:00
|
|
|
if (topIntent !== undefined && nlpActive) {
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const intent = topIntent;
|
2019-03-08 17:05:58 -03:00
|
|
|
// tslint:disable:no-unsafe-any
|
|
|
|
const firstEntity = nlp.entities && nlp.entities.length > 0 ? nlp.entities[0].entity.toUpperCase() : undefined;
|
|
|
|
// tslint:ensable:no-unsafe-any
|
2018-09-14 12:56:54 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
if (intent === 'None') {
|
2018-09-24 15:27:26 -03:00
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`NLP called: ${intent} ${firstEntity}`);
|
2018-09-11 19:33:58 -03:00
|
|
|
|
|
|
|
try {
|
2019-05-01 13:14:36 +00:00
|
|
|
await step.replaceDialog(`/${intent}`, nlp.entities);
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2018-09-24 15:27:26 -03:00
|
|
|
return Promise.resolve(true);
|
2018-09-11 19:33:58 -03:00
|
|
|
} catch (error) {
|
2018-11-27 22:56:11 -02:00
|
|
|
const msg = `Error finding dialog associated to NLP event: ${intent}: ${error.message}`;
|
|
|
|
|
2018-09-24 15:27:26 -03:00
|
|
|
return Promise.reject(new Error(msg));
|
2018-09-11 19:33:58 -03:00
|
|
|
}
|
2018-05-14 01:48:39 -03:00
|
|
|
}
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2018-09-24 15:27:26 -03:00
|
|
|
return Promise.resolve(false);
|
2018-09-11 19:33:58 -03:00
|
|
|
}
|
2018-09-14 14:29:44 -03:00
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
public async checkLanguage(step: GBDialogStep, min, text) {
|
2018-11-27 22:56:11 -02:00
|
|
|
const locale = await AzureText.getLocale(min.instance.textAnalyticsKey, min.instance.textAnalyticsEndpoint, text);
|
|
|
|
if (locale !== step.context.activity.locale.split('-')[0]) {
|
2018-09-14 14:29:44 -03:00
|
|
|
switch (locale) {
|
2018-11-12 12:20:44 -02:00
|
|
|
case 'pt':
|
|
|
|
step.context.activity.locale = 'pt-BR';
|
2018-11-01 21:06:11 -03:00
|
|
|
await step.context.sendActivity(Messages[locale].changing_language);
|
2018-09-14 14:29:44 -03:00
|
|
|
break;
|
2018-11-12 12:20:44 -02:00
|
|
|
case 'en':
|
|
|
|
step.context.activity.locale = 'en-US';
|
2018-11-01 21:06:11 -03:00
|
|
|
await step.context.sendActivity(Messages[locale].changing_language);
|
2018-09-14 14:29:44 -03:00
|
|
|
break;
|
|
|
|
default:
|
2019-03-08 17:05:58 -03:00
|
|
|
await step.context.sendActivity(`; Unknown; language: $;{locale;}`);
|
2018-09-14 14:29:44 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|