2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
2018-09-10 16:24:32 -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
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
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
|
|
|
|
2020-10-14 13:43:58 -03:00
|
|
|
import { MessageFactory, RecognizerResult, TurnContext } from 'botbuilder';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { LuisRecognizer } from 'botbuilder-ai';
|
2020-04-28 20:54:04 -03:00
|
|
|
import { GBDialogStep, GBLog, GBMinInstance, IGBCoreService } from 'botlib';
|
2020-03-30 14:03:12 -03:00
|
|
|
import { GBServer } from '../../../src/app';
|
2020-08-07 18:36:42 -03:00
|
|
|
import { Readable } from 'stream';
|
2020-04-30 21:14:22 -03:00
|
|
|
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
|
2020-07-26 16:46:37 -03:00
|
|
|
import { SecService } from '../../security.gbapp/services/SecService';
|
2020-06-04 16:18:02 -03:00
|
|
|
import { AnalyticsService } from '../../analytics.gblib/services/AnalyticsService';
|
2020-10-18 13:24:19 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
|
|
|
import { WaterfallStep, WaterfallStepContext } from 'botbuilder-dialogs';
|
|
|
|
import { MicrosoftAppCredentials } from 'botframework-connector';
|
2020-03-30 14:03:12 -03:00
|
|
|
const urlJoin = require('url-join');
|
2020-08-07 18:36:42 -03:00
|
|
|
const PasswordGenerator = require('strict-password-generator').default;
|
2018-11-12 12:20:44 -02:00
|
|
|
const Nexmo = require('nexmo');
|
2020-08-07 18:36:42 -03:00
|
|
|
const { join } = require('path');
|
|
|
|
const shell = require('any-shell-escape');
|
|
|
|
const { exec } = require('child_process');
|
|
|
|
const prism = require('prism-media');
|
2020-05-17 19:05:18 +00:00
|
|
|
const uuidv4 = require('uuid/v4');
|
|
|
|
const request = require('request-promise-native');
|
2020-08-07 18:36:42 -03:00
|
|
|
const fs = require('fs');
|
|
|
|
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
|
|
|
|
const { IamAuthenticator } = require('ibm-watson/auth');
|
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.
|
|
|
|
*/
|
2020-04-28 20:54:04 -03:00
|
|
|
export class GBConversationalService {
|
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
|
|
|
}
|
|
|
|
|
2020-02-25 12:37:10 -03:00
|
|
|
public getNewMobileCode() {
|
2020-02-25 10:13:38 -03:00
|
|
|
const passwordGenerator = new PasswordGenerator();
|
|
|
|
const options = {
|
|
|
|
upperCaseAlpha: false,
|
|
|
|
lowerCaseAlpha: false,
|
|
|
|
number: true,
|
|
|
|
specialCharacter: false,
|
|
|
|
minimumLength: 4,
|
|
|
|
maximumLength: 4
|
|
|
|
};
|
|
|
|
let code = passwordGenerator.generatePassword(options);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-05-30 19:30:11 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
public async sendFile(
|
|
|
|
min: GBMinInstance,
|
|
|
|
step: GBDialogStep,
|
|
|
|
mobile: string,
|
|
|
|
url: string,
|
|
|
|
caption: string
|
|
|
|
): Promise<any> {
|
2020-03-31 09:11:04 -03:00
|
|
|
if (step !== null) {
|
2020-05-25 17:59:02 -03:00
|
|
|
if (!isNaN(step.context.activity.from.id as any)) {
|
2020-06-05 16:09:47 -03:00
|
|
|
mobile = step.context.activity.from.id;
|
2020-08-07 18:36:42 -03:00
|
|
|
GBLog.info(`Sending file ${url} to ${mobile}...`);
|
2020-05-15 14:07:30 -03:00
|
|
|
const filename = url.substring(url.lastIndexOf('/') + 1);
|
|
|
|
await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption);
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
|
|
|
GBLog.info(
|
|
|
|
`Sending ${url} as file attachment not available in this channel ${step.context.activity.from.id}...`
|
|
|
|
);
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, url);
|
2020-05-15 14:07:30 -03:00
|
|
|
}
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
|
|
|
GBLog.info(`Sending file ${url} to ${mobile}...`);
|
2020-05-25 17:59:02 -03:00
|
|
|
const filename = url.substring(url.lastIndexOf('/') + 1);
|
|
|
|
await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename, caption);
|
|
|
|
}
|
2019-08-24 12:22:52 -03:00
|
|
|
}
|
|
|
|
|
2019-08-24 18:46:04 -03:00
|
|
|
public async sendAudio(min: GBMinInstance, step: GBDialogStep, url: string): Promise<any> {
|
|
|
|
const mobile = step.context.activity.from.id;
|
|
|
|
await min.whatsAppDirectLine.sendAudioToDevice(mobile, url);
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:30:21 +00:00
|
|
|
public async sendEvent(min: GBMinInstance, 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
|
|
|
|
2019-11-10 16:20:15 -03:00
|
|
|
return await 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> {
|
2020-08-07 18:36:42 -03:00
|
|
|
return new Promise((resolve: any, reject: any): any => {
|
|
|
|
const nexmo = new Nexmo({
|
|
|
|
apiKey: min.instance.smsKey,
|
|
|
|
apiSecret: min.instance.smsSecret
|
|
|
|
});
|
|
|
|
// tslint:disable-next-line:no-unsafe-any
|
|
|
|
nexmo.message.sendSms(min.instance.smsServiceNumber, mobile, text, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
2020-03-30 14:03:12 -03:00
|
|
|
|
|
|
|
public async sendToMobile(min: GBMinInstance, mobile: string, message: string) {
|
2020-03-31 09:11:04 -03:00
|
|
|
min.whatsAppDirectLine.sendToDevice(mobile, message);
|
2020-03-30 14:03:12 -03:00
|
|
|
}
|
|
|
|
|
2020-04-28 20:54:04 -03:00
|
|
|
public static async getAudioBufferFromText(speechKey, cloudRegion, text, locale): Promise<string> {
|
|
|
|
return new Promise<string>(async (resolve, reject) => {
|
|
|
|
const name = GBAdminService.getRndReadableIdentifier();
|
|
|
|
|
|
|
|
const waveFilename = `work/tmp${name}.pcm`;
|
2020-08-22 18:41:54 -03:00
|
|
|
const sdk = require('microsoft-cognitiveservices-speech-sdk');
|
|
|
|
sdk.Recognizer.enableTelemetry(false);
|
2020-09-19 21:57:00 -03:00
|
|
|
|
2020-04-28 20:54:04 -03:00
|
|
|
var audioConfig = sdk.AudioConfig.fromAudioFileOutput(waveFilename);
|
|
|
|
var speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, cloudRegion);
|
|
|
|
|
|
|
|
var synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
|
|
|
|
|
|
|
|
try {
|
|
|
|
speechConfig.speechSynthesisLanguage = locale;
|
2020-08-07 18:36:42 -03:00
|
|
|
speechConfig.speechSynthesisVoiceName = 'pt-BR-FranciscaNeural';
|
2020-04-28 20:54:04 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
synthesizer.speakTextAsync(text, result => {
|
|
|
|
if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
|
|
|
|
let raw = Buffer.from(result.audioData);
|
|
|
|
fs.writeFileSync(waveFilename, raw);
|
|
|
|
GBLog.info(`Audio data byte size: ${result.audioData.byteLength}.`);
|
|
|
|
const oggFilenameOnly = `tmp${name}.ogg`;
|
|
|
|
const oggFilename = `work/${oggFilenameOnly}`;
|
2020-04-28 20:54:04 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
const output = fs.createWriteStream(oggFilename);
|
|
|
|
const transcoder = new prism.FFmpeg({
|
|
|
|
args: ['-analyzeduration', '0', '-loglevel', '0', '-f', 'opus', '-ar', '16000', '-ac', '1']
|
|
|
|
});
|
2020-04-28 20:54:04 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
fs.createReadStream(waveFilename).pipe(transcoder).pipe(output);
|
|
|
|
|
|
|
|
let url = urlJoin(GBServer.globals.publicAddress, 'audios', oggFilenameOnly);
|
|
|
|
resolve(url);
|
|
|
|
} else {
|
|
|
|
const error = 'Speech synthesis canceled, ' + result.errorDetails;
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
synthesizer.close();
|
|
|
|
synthesizer = undefined;
|
|
|
|
});
|
2020-04-28 20:54:04 -03:00
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-17 10:39:54 -03:00
|
|
|
public static async getTextFromAudioBuffer(speechKey, cloudRegion, buffer, locale): Promise<string> {
|
2020-04-13 19:14:55 -03:00
|
|
|
return new Promise<string>(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
let subscriptionKey = speechKey;
|
|
|
|
let serviceRegion = cloudRegion;
|
|
|
|
|
|
|
|
const oggFile = new Readable();
|
2020-08-07 18:36:42 -03:00
|
|
|
oggFile._read = () => {}; // _read is required but you can noop it
|
2020-04-13 19:14:55 -03:00
|
|
|
oggFile.push(buffer);
|
|
|
|
oggFile.push(null);
|
|
|
|
|
2020-04-15 05:08:50 +00:00
|
|
|
const name = GBAdminService.getRndReadableIdentifier();
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-04-30 21:14:22 -03:00
|
|
|
const dest = `work/tmp${name}.wav`;
|
|
|
|
const src = `work/tmp${name}.ogg`;
|
|
|
|
fs.writeFileSync(src, oggFile.read());
|
|
|
|
|
|
|
|
const makeMp3 = shell([
|
2020-08-07 18:36:42 -03:00
|
|
|
'node_modules/ffmpeg-static/ffmpeg.exe',
|
|
|
|
'-y',
|
|
|
|
'-v',
|
|
|
|
'error',
|
|
|
|
'-i',
|
|
|
|
join(process.cwd(), src),
|
|
|
|
'-ar',
|
|
|
|
'44100',
|
|
|
|
'-ac',
|
|
|
|
'1',
|
|
|
|
'-acodec',
|
|
|
|
'pcm_s16le',
|
2020-04-30 21:14:22 -03:00
|
|
|
join(process.cwd(), dest)
|
2020-08-07 18:36:42 -03:00
|
|
|
]);
|
2020-04-30 21:14:22 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
exec(makeMp3, error => {
|
2020-04-30 21:14:22 -03:00
|
|
|
if (error) {
|
|
|
|
GBLog.error(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
} else {
|
|
|
|
let data = fs.readFileSync(dest);
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
const speechToText = new SpeechToTextV1({
|
|
|
|
authenticator: new IamAuthenticator({ apikey: process.env.WATSON_STT_KEY }),
|
|
|
|
url: process.env.WATSON_STT_URL
|
|
|
|
});
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
audio: data,
|
|
|
|
contentType: 'audio/l16; rate=44100',
|
2020-08-19 13:00:21 -03:00
|
|
|
model: 'pt-BR_BroadbandModel'
|
2020-08-07 18:36:42 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
speechToText
|
|
|
|
.recognize(params)
|
|
|
|
.then(response => {
|
|
|
|
if (response.result.results.length > 0) {
|
|
|
|
resolve(response.result.results[0].alternatives[0].transcript);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
GBLog.error(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
});
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
// let pushStream = sdk.AudioInputStream.createPushStream();
|
|
|
|
// pushStream.write(data);
|
|
|
|
// pushStream.close();
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
// let audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);
|
|
|
|
// let speechConfig = sdk.SpeechConfig.fromSubscription(subscriptionKey, serviceRegion);
|
|
|
|
// speechConfig.speechRecognitionLanguage = locale;
|
|
|
|
// let recognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
// recognizer.recognizeOnceAsync(
|
|
|
|
// (result) => {
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
// resolve(result.text ? result.text : 'Speech to Text failed: Audio not converted');
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
// recognizer.close();
|
|
|
|
// recognizer = undefined;
|
|
|
|
// },
|
|
|
|
// (err) => {
|
|
|
|
// reject(err);
|
2020-04-13 19:14:55 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
// recognizer.close();
|
|
|
|
// recognizer = undefined;
|
|
|
|
// });
|
2020-04-30 21:14:22 -03:00
|
|
|
}
|
2020-08-07 18:36:42 -03:00
|
|
|
});
|
2020-04-13 19:14:55 -03:00
|
|
|
} catch (error) {
|
|
|
|
GBLog.error(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
// tslint:enable:no-unsafe-any
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2020-03-30 14:03:12 -03:00
|
|
|
public async sendMarkdownToMobile(min: GBMinInstance, step: GBDialogStep, mobile: string, text: string) {
|
2020-08-07 18:36:42 -03:00
|
|
|
let sleep = ms => {
|
2020-03-30 14:03:12 -03:00
|
|
|
return new Promise(resolve => {
|
2020-08-07 18:36:42 -03:00
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
};
|
2020-03-30 14:03:12 -03:00
|
|
|
enum State {
|
|
|
|
InText,
|
|
|
|
InImage,
|
|
|
|
InImageBegin,
|
|
|
|
InImageCaption,
|
|
|
|
InImageAddressBegin,
|
2020-04-01 15:42:57 -03:00
|
|
|
InImageAddressBody,
|
|
|
|
InEmbedBegin,
|
|
|
|
InEmbedEnd,
|
|
|
|
InEmbedAddressBegin,
|
2020-04-02 19:03:57 -03:00
|
|
|
InEmbedAddressEnd,
|
|
|
|
InLineBreak,
|
2020-04-03 02:50:33 -03:00
|
|
|
InLineBreak1,
|
2020-08-07 18:36:42 -03:00
|
|
|
InLineBreak2
|
|
|
|
}
|
2020-03-30 14:03:12 -03:00
|
|
|
let state = State.InText;
|
|
|
|
let currentImage = '';
|
|
|
|
let currentText = '';
|
2020-03-31 09:11:04 -03:00
|
|
|
let currentCaption = '';
|
2020-04-01 15:42:57 -03:00
|
|
|
let currentEmbedUrl = '';
|
2020-03-30 14:03:12 -03:00
|
|
|
|
|
|
|
//
|
2020-04-13 19:14:55 -03:00
|
|
|
for (let i = 0; i < text.length; i++) {
|
2020-03-30 14:03:12 -03:00
|
|
|
const c = text.charAt(i);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case State.InText:
|
|
|
|
if (c === '!') {
|
|
|
|
state = State.InImageBegin;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else if (c === '[') {
|
2020-04-01 15:42:57 -03:00
|
|
|
state = State.InEmbedBegin;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else if (c === '\n') {
|
2020-04-02 19:03:57 -03:00
|
|
|
state = State.InLineBreak;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-04-03 02:50:33 -03:00
|
|
|
state = State.InText;
|
2020-03-30 14:03:12 -03:00
|
|
|
currentText = currentText.concat(c);
|
|
|
|
}
|
|
|
|
break;
|
2020-04-02 19:03:57 -03:00
|
|
|
case State.InLineBreak:
|
|
|
|
if (c === '\n') {
|
2020-04-03 02:50:33 -03:00
|
|
|
state = State.InLineBreak1;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else if (c === '!') {
|
2020-04-03 02:50:33 -03:00
|
|
|
state = State.InImageBegin;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else if (c === '[') {
|
2020-04-03 02:50:33 -03:00
|
|
|
state = State.InEmbedBegin;
|
|
|
|
} else {
|
|
|
|
currentText = currentText.concat('\n', c);
|
|
|
|
state = State.InText;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case State.InLineBreak1:
|
|
|
|
if (c === '\n') {
|
|
|
|
if (mobile === null) {
|
|
|
|
await step.context.sendActivity(currentText);
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-06-05 16:09:47 -03:00
|
|
|
await this.sendToMobile(min, mobile, currentText);
|
2020-04-03 02:50:33 -03:00
|
|
|
}
|
|
|
|
await sleep(3000);
|
2020-04-02 19:03:57 -03:00
|
|
|
currentText = '';
|
|
|
|
state = State.InText;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else if (c === '!') {
|
2020-04-03 02:50:33 -03:00
|
|
|
state = State.InImageBegin;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else if (c === '[') {
|
2020-04-03 02:50:33 -03:00
|
|
|
state = State.InEmbedBegin;
|
|
|
|
} else {
|
|
|
|
currentText = currentText.concat('\n', '\n', c);
|
|
|
|
state = State.InText;
|
|
|
|
}
|
|
|
|
break;
|
2020-04-01 15:42:57 -03:00
|
|
|
case State.InEmbedBegin:
|
|
|
|
if (c === '=') {
|
|
|
|
if (currentText !== '') {
|
|
|
|
if (mobile === null) {
|
|
|
|
await step.context.sendActivity(currentText);
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-06-05 16:09:47 -03:00
|
|
|
await this.sendToMobile(min, mobile, currentText);
|
2020-04-01 15:42:57 -03:00
|
|
|
}
|
|
|
|
await sleep(3000);
|
|
|
|
}
|
|
|
|
currentText = '';
|
|
|
|
state = State.InEmbedAddressBegin;
|
|
|
|
}
|
2020-04-03 02:50:33 -03:00
|
|
|
|
2020-04-01 15:42:57 -03:00
|
|
|
break;
|
|
|
|
case State.InEmbedAddressBegin:
|
|
|
|
if (c === ']') {
|
|
|
|
state = State.InEmbedEnd;
|
2020-09-19 21:57:00 -03:00
|
|
|
let url = currentEmbedUrl.startsWith('http')
|
|
|
|
? currentEmbedUrl
|
|
|
|
: urlJoin(GBServer.globals.publicAddress, currentEmbedUrl);
|
2020-04-01 15:42:57 -03:00
|
|
|
await this.sendFile(min, step, mobile, url, null);
|
|
|
|
await sleep(5000);
|
|
|
|
currentEmbedUrl = '';
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-04-01 15:42:57 -03:00
|
|
|
currentEmbedUrl = currentEmbedUrl.concat(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case State.InEmbedEnd:
|
|
|
|
if (c === ']') {
|
|
|
|
state = State.InText;
|
|
|
|
}
|
|
|
|
break;
|
2020-03-30 14:03:12 -03:00
|
|
|
case State.InImageBegin:
|
|
|
|
if (c === '[') {
|
|
|
|
if (currentText !== '') {
|
2020-03-31 09:11:04 -03:00
|
|
|
if (mobile === null) {
|
2020-03-30 14:03:12 -03:00
|
|
|
await step.context.sendActivity(currentText);
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-06-05 16:09:47 -03:00
|
|
|
await this.sendToMobile(min, mobile, currentText);
|
2020-03-31 09:11:04 -03:00
|
|
|
}
|
2020-06-05 16:09:47 -03:00
|
|
|
await sleep(2900);
|
2020-03-30 14:03:12 -03:00
|
|
|
}
|
|
|
|
currentText = '';
|
|
|
|
state = State.InImageCaption;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-03-30 14:03:12 -03:00
|
|
|
state = State.InText;
|
|
|
|
currentText = currentText.concat('!').concat(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case State.InImageCaption:
|
|
|
|
if (c === ']') {
|
|
|
|
state = State.InImageAddressBegin;
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-03-31 09:11:04 -03:00
|
|
|
currentCaption = currentCaption.concat(c);
|
|
|
|
}
|
2020-03-30 14:03:12 -03:00
|
|
|
break;
|
|
|
|
case State.InImageAddressBegin:
|
|
|
|
if (c === '(') {
|
|
|
|
state = State.InImageAddressBody;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case State.InImageAddressBody:
|
|
|
|
if (c === ')') {
|
|
|
|
state = State.InText;
|
2020-09-19 21:57:00 -03:00
|
|
|
let url = currentImage.startsWith('http')
|
|
|
|
? currentImage
|
|
|
|
: urlJoin(GBServer.globals.publicAddress, currentImage);
|
2020-03-31 09:11:04 -03:00
|
|
|
await this.sendFile(min, step, mobile, url, currentCaption);
|
|
|
|
currentCaption = '';
|
2020-06-05 16:09:47 -03:00
|
|
|
await sleep(4500);
|
2020-03-30 14:03:12 -03:00
|
|
|
currentImage = '';
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-03-30 14:03:12 -03:00
|
|
|
currentImage = currentImage.concat(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (currentText !== '') {
|
2020-03-31 09:11:04 -03:00
|
|
|
if (mobile === null) {
|
2020-03-30 14:03:12 -03:00
|
|
|
await step.context.sendActivity(currentText);
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-06-05 16:09:47 -03:00
|
|
|
await this.sendToMobile(min, mobile, currentText);
|
2020-03-30 14:03:12 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
public async routeNLP(step: GBDialogStep, min: GBMinInstance, text: string): Promise<boolean> {
|
2020-06-15 00:40:25 -03:00
|
|
|
if (min.instance.nlpAppId === null || min.instance.nlpAppId === undefined) {
|
2019-08-22 19:36:23 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-10-24 18:56:55 -03:00
|
|
|
const saved = step.context.activity.text
|
|
|
|
step.context.activity.text = text;
|
2018-11-01 21:06:11 -03:00
|
|
|
nlp = await model.recognize(step.context);
|
2020-10-24 18:56:55 -03:00
|
|
|
step.context.activity.text = saved;
|
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
|
|
|
|
2020-08-19 13:00:21 -03:00
|
|
|
return 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 : ''
|
2020-08-07 18:36:42 -03:00
|
|
|
} {error.message; }`;
|
2018-11-04 09:19:03 -02:00
|
|
|
|
2020-08-19 13:00:21 -03:00
|
|
|
throw new Error(msg);
|
2018-11-27 22:56:11 -02:00
|
|
|
}
|
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;
|
2020-10-27 15:08:03 -03:00
|
|
|
let score = 0;
|
2019-07-19 13:35:11 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
Object.keys(nlp.intents).forEach(name => {
|
2020-10-27 15:08:03 -03:00
|
|
|
score = nlp.intents[name].score;
|
2020-03-30 14:03:12 -03:00
|
|
|
if (score > min.instance.nlpScore) {
|
2019-07-19 13:35:11 -03:00
|
|
|
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;
|
|
|
|
if (intent === 'None') {
|
2020-08-19 13:00:21 -03:00
|
|
|
return false;
|
2018-09-24 15:27:26 -03:00
|
|
|
}
|
|
|
|
|
2020-10-27 15:08:03 -03:00
|
|
|
GBLog.info(`NLP called: ${intent}, entities: ${nlp.entities.length}, score: ${score} > required (nlpScore): ${min.instance.nlpScore}`);
|
2018-09-11 19:33:58 -03:00
|
|
|
|
|
|
|
try {
|
2020-10-23 09:55:44 -03:00
|
|
|
step.activeDialog.state.options.entities = nlp.entities;
|
2020-10-14 13:43:58 -03:00
|
|
|
await step.replaceDialog(`/${intent}`, step.activeDialog.state.options);
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2020-08-19 13:00:21 -03:00
|
|
|
return 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}`;
|
|
|
|
|
2020-08-19 13:00:21 -03:00
|
|
|
throw 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
|
|
|
|
2020-08-19 13:00:21 -03:00
|
|
|
return false;
|
2018-09-11 19:33:58 -03:00
|
|
|
}
|
2018-09-14 14:29:44 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
async translate(min: GBMinInstance, key: string, endPoint: string, text: string, language: string): Promise<string> {
|
2020-06-04 20:14:02 -03:00
|
|
|
const translatorEnabled = () => {
|
|
|
|
if (min.instance.params) {
|
|
|
|
const params = JSON.parse(min.instance.params);
|
2020-08-07 18:36:42 -03:00
|
|
|
return params ? params['Enable Worldwide Translator'] === 'TRUE' : false;
|
2020-06-04 20:14:02 -03:00
|
|
|
}
|
|
|
|
return false;
|
2020-08-07 18:36:42 -03:00
|
|
|
}; // TODO: Encapsulate.
|
2020-06-04 20:14:02 -03:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
if (endPoint === null || !translatorEnabled() || process.env.TRANSLATOR_DISABLED === 'true') {
|
2020-05-25 14:53:28 -03:00
|
|
|
return text;
|
2020-05-18 01:03:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:53:28 -03:00
|
|
|
if (text.length > 5000) {
|
|
|
|
text = text.substr(0, 4999);
|
2020-05-24 17:06:05 -03:00
|
|
|
GBLog.warn(`Text that bot will translate will be truncated due to MSFT service limitations.`);
|
|
|
|
}
|
|
|
|
|
2020-05-17 19:05:18 +00:00
|
|
|
let options = {
|
|
|
|
method: 'POST',
|
|
|
|
baseUrl: endPoint,
|
|
|
|
url: 'translate',
|
|
|
|
qs: {
|
|
|
|
'api-version': '3.0',
|
2020-08-07 18:36:42 -03:00
|
|
|
to: [language]
|
2020-05-17 19:05:18 +00:00
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'Ocp-Apim-Subscription-Key': key,
|
|
|
|
'Ocp-Apim-Subscription-Region': 'westeurope',
|
|
|
|
'Content-type': 'application/json',
|
|
|
|
'X-ClientTraceId': uuidv4().toString()
|
|
|
|
},
|
2020-08-07 18:36:42 -03:00
|
|
|
body: [
|
|
|
|
{
|
|
|
|
text: text
|
|
|
|
}
|
|
|
|
],
|
|
|
|
json: true
|
|
|
|
};
|
2020-05-17 19:05:18 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const results = await request(options);
|
2020-05-17 21:30:21 +00:00
|
|
|
|
2020-05-17 19:05:18 +00:00
|
|
|
return results[0].translations[0].text;
|
|
|
|
} catch (error) {
|
|
|
|
const msg = `Error calling Translator service layer. Error is: ${error}.`;
|
|
|
|
|
|
|
|
return Promise.reject(new Error(msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:53:28 -03:00
|
|
|
public async prompt(min: GBMinInstance, step: GBDialogStep, text: string) {
|
2020-05-30 19:30:11 -03:00
|
|
|
const minBoot = GBServer.globals.minBoot as any;
|
2020-05-17 19:05:18 +00:00
|
|
|
|
|
|
|
let sec = new SecService();
|
|
|
|
const member = step.context.activity.from;
|
2020-08-07 18:36:42 -03:00
|
|
|
const user = await sec.ensureUser(min.instance.instanceId, member.id, member.name, '', 'web', member.name);
|
2020-06-03 21:31:00 -03:00
|
|
|
if (text !== null) {
|
2020-08-07 18:36:42 -03:00
|
|
|
text = await min.conversationalService.translate(
|
|
|
|
min,
|
2020-06-03 21:31:00 -03:00
|
|
|
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
|
|
|
|
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
|
|
|
|
text,
|
2020-10-30 08:36:15 -03:00
|
|
|
user.locale ? user.locale : 'en'
|
2020-06-03 21:31:00 -03:00
|
|
|
);
|
2020-10-30 08:36:15 -03:00
|
|
|
GBLog.info(`Translated text(4): ${text}.`);
|
2020-06-03 21:31:00 -03:00
|
|
|
}
|
2020-05-17 19:05:18 +00:00
|
|
|
|
2020-08-07 18:36:42 -03:00
|
|
|
return await step.prompt('textPrompt', text ? text : {});
|
2020-05-17 21:30:21 +00:00
|
|
|
}
|
2020-05-25 14:53:28 -03:00
|
|
|
|
|
|
|
public async sendText(min, step, text) {
|
2020-07-17 10:39:54 -03:00
|
|
|
let sec = new SecService();
|
2020-05-17 21:30:21 +00:00
|
|
|
const member = step.context.activity.from;
|
2020-08-07 18:36:42 -03:00
|
|
|
const user = await sec.ensureUser(min.instance.instanceId, member.id, member.name, '', 'web', member.name);
|
2020-07-17 10:39:54 -03:00
|
|
|
|
2020-06-04 16:18:02 -03:00
|
|
|
if (user) {
|
|
|
|
const minBoot = GBServer.globals.minBoot as any;
|
2020-08-07 18:36:42 -03:00
|
|
|
text = await min.conversationalService.translate(
|
|
|
|
min,
|
2020-06-04 16:18:02 -03:00
|
|
|
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
|
|
|
|
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
|
|
|
|
text,
|
2020-10-30 08:36:15 -03:00
|
|
|
user.locale ? user.locale : 'en'
|
2020-06-04 16:18:02 -03:00
|
|
|
);
|
2020-10-30 08:36:15 -03:00
|
|
|
GBLog.info(`Translated text(5): ${text}.`);
|
|
|
|
|
2020-06-04 16:18:02 -03:00
|
|
|
const analytics = new AnalyticsService();
|
2020-07-17 10:39:54 -03:00
|
|
|
const userProfile = await min.userProfile.get(step.context, {});
|
2020-08-07 18:36:42 -03:00
|
|
|
analytics.createMessage(min.instance.instanceId, userProfile.conversation, null, text);
|
2020-05-25 17:59:02 -03:00
|
|
|
|
2020-06-04 16:18:02 -03:00
|
|
|
if (!isNaN(member.id)) {
|
|
|
|
await min.whatsAppDirectLine.sendToDevice(member.id, text);
|
2020-08-07 18:36:42 -03:00
|
|
|
} else {
|
2020-06-04 16:18:02 -03:00
|
|
|
await step.context.sendActivity(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-18 13:24:19 -03:00
|
|
|
|
|
|
|
public async broadcast(min: GBMinInstance, message: string) {
|
|
|
|
GBLog.info(`Sending broadcast notifications...`);
|
|
|
|
|
|
|
|
let sleep = ms => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const service = new SecService();
|
|
|
|
const users = await service.getAllUsers(min.instance.instanceId);
|
|
|
|
await CollectionUtil.asyncForEach(users, async user => {
|
|
|
|
if (user.conversationReference) {
|
|
|
|
const ref = JSON.parse(user.conversationReference);
|
|
|
|
MicrosoftAppCredentials.trustServiceUrl(ref.serviceUrl);
|
|
|
|
await min.bot['createConversation'](ref, async t1 => {
|
|
|
|
const ref2 = TurnContext.getConversationReference(t1.activity);
|
|
|
|
await min.bot.continueConversation(ref2, async t2 => {
|
|
|
|
await t2.sendActivity(message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
GBLog.info(`User: ${user.systemUserId} with no conversation ID while broadcasting.`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|