This commit is contained in:
Rodrigo Rodriguez 2020-05-27 23:01:44 -03:00
parent 3abe85ca30
commit e87031255e
7 changed files with 100 additions and 73 deletions

View file

@ -534,7 +534,7 @@ export class GBConversationalService {
language: string
): Promise<string> {
if (process.env.TRANSLATOR_DISABLED === "true") {
if (endPoint === null || process.env.TRANSLATOR_DISABLED === "true") {
return text;
}

View file

@ -165,6 +165,7 @@ export class GBDeployer implements IGBDeployer {
instance.adminPass = GBAdminService.getRndPassword();
instance.title = botId;
instance.activationCode = instance.botId;
instance.state = 'active';
await this.core.saveInstance(instance);

View file

@ -144,12 +144,12 @@ export class GBMinService {
}
let activeMin;
if (process.env.WHATSAPP_WELCOME_DISABLED !== "true") {
// TODO: Active in two modes.
// TODO: Active in two modes.
const toSwitchMin = GBServer.globals.minInstances.filter(p => p.instance.botId === text)[0];
activeMin = toSwitchMin ? toSwitchMin : GBServer.globals.minBoot;
let sec = new SecService();
let user = await sec.getUserFromSystemId(id);
if (user === null) {
@ -345,7 +345,7 @@ export class GBMinService {
}
let instance = await this.core.loadInstanceByBotId(botId);
if (instance === null){
if (instance === null) {
instance = await this.core.loadInstanceByActivationCode(botId);
}
@ -687,11 +687,14 @@ export class GBMinService {
} else {
let query = context.activity.text;
let locale = 'pt';
if (process.env.TRANSLATOR_DISABLED !== "true"){
locale = await AzureText.getLocale(min.instance.textAnalyticsKey,
min.instance.textAnalyticsEndpoint, query);
if (process.env.TRANSLATOR_DISABLED !== "true") {
const minBoot = GBServer.globals.minBoot as any; // TODO: Switch keys automatically to master/per bot.
locale = await AzureText.getLocale(minBoot.instance.textAnalyticsKey ?
minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey,
minBoot.instance.textAnalyticsEndpoint ?
minBoot.instance.textAnalyticsEndpoint : minBoot.instance.textAnalyticsKeyEndpoint, query);
}
let sec = new SecService();

View file

@ -43,7 +43,7 @@ import { AzureText } from 'pragmatismo-io-framework';
import { CSService } from '../services/CSService';
import { Messages } from '../strings';
import { SecService } from '../../security.gblib/services/SecService';
import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService';
import { GBServer } from '../../../src/app';
/**
* Dialog for feedback collecting.
@ -81,9 +81,9 @@ export class FeedbackDialog extends IGBDialog {
await min.conversationalService.sendText(min, step, Messages[locale].please_wait_transfering);
let agentSystemId = await sec.assignHumanAgent(from, min.instance.instanceId);
await min.whatsAppDirectLine.sendToDevice(agentSystemId,
await min.whatsAppDirectLine.sendToDevice(agentSystemId,
Messages[locale].notify_agent(step.context.activity.from.name));
return await step.next();
}
])
@ -100,7 +100,7 @@ export class FeedbackDialog extends IGBDialog {
await sec.updateCurrentAgent(from, min.instance.instanceId, null);
await min.conversationalService.sendText(min, step, Messages[locale].notify_end_transfer(min.instance.botId));
return await step.next();
}
])
@ -134,13 +134,17 @@ export class FeedbackDialog extends IGBDialog {
await min.conversationalService.sendText(min, step, Messages[locale].about_suggestions);
step.activeDialog.state.cbId = (step.options as any).id;
return await min.conversationalService.prompt (min, step, Messages[locale].what_about_service);
return await min.conversationalService.prompt(min, step, Messages[locale].what_about_service);
},
async step => {
const minBoot = GBServer.globals.minBoot as any;
const locale = step.context.activity.locale;
const rate = await AzureText.getSentiment(
min.instance.textAnalyticsKey,
min.instance.textAnalyticsEndpoint,
minBoot.instance.textAnalyticsKey ?
minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey,
minBoot.instance.textAnalyticsEndpoint ?
minBoot.instance.textAnalyticsEndpoint : minBoot.instance.textAnalyticsKeyEndpoint,
min.conversationalService.getCurrentLanguage(step),
step.result
);

View file

@ -36,6 +36,7 @@
'use strict';
import { GBServer } from '../../../src/app';
import { BotAdapter } from 'botbuilder';
import { WaterfallDialog } from 'botbuilder-dialogs';
import { GBLog, GBMinInstance, IGBDialog } from 'botlib';
@ -104,11 +105,14 @@ export class AskDialog extends IGBDialog {
let query = step.result;
let locale = 'pt';
if (process.env.TRANSLATOR_DISABLED !== "true"){
locale = await AzureText.getLocale(min.instance.textAnalyticsKey,
min.instance.textAnalyticsEndpoint, query);
}
if (process.env.TRANSLATOR_DISABLED !== "true") {
const minBoot = GBServer.globals.minBoot as any;
locale = await AzureText.getLocale(minBoot.instance.textAnalyticsKey ?
minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey,
minBoot.instance.textAnalyticsEndpoint ?
minBoot.instance.textAnalyticsEndpoint : minBoot.instance.textAnalyticsKeyEndpoint, query);
}
let sec = new SecService();
const member = step.context.activity.from;
@ -146,7 +150,7 @@ export class AskDialog extends IGBDialog {
min.instance.translatorEndpoint,
text,
userDb.locale ? userDb.locale : 'pt'
);
);
if (!text) {
throw new Error(`/answer being called with no args query text.`);

View file

@ -55,6 +55,7 @@ import { Messages } from '../strings';
import { GBConfigService } from './../../core.gbapp/services/GBConfigService';
import { CSService } from '../../customer-satisfaction.gbapp/services/CSService';
import { SecService } from '../../security.gblib/services/SecService';
import { CollectionUtil } from 'pragmatismo-io-framework';
/**
* Result for quey on KB data.
@ -382,7 +383,7 @@ export class KBService implements IGBKBService {
await this.playMarkdown(min, answer, channel, step, min.conversationalService);
} else if (answer.content.endsWith('.ogg')) {
} else if (answer.content.endsWith('.ogg') && process.env.AUDIO_DISABLED !== "true") {
await this.playAudio(min, answer, channel, step, min.conversationalService);
} else {
@ -411,7 +412,7 @@ export class KBService implements IGBKBService {
html = marked(answer.content);
if (channel === 'webchat' &&
GBConfigService.get('DISABLE_WEB') !== 'true') {
await this.sendMarkdownToWeb(min, step, conversationalService, html, answer);
}
else if (channel === 'whatsapp') {
@ -425,7 +426,7 @@ export class KBService implements IGBKBService {
answer.content,
user.locale ? user.locale : 'pt'
);
await conversationalService.sendMarkdownToMobile(min, step, null, answer.content);
}
else {
@ -434,7 +435,7 @@ export class KBService implements IGBKBService {
}
private async sendMarkdownToWeb(min, step: GBDialogStep, conversationalService: IGBConversationalService, html: string, answer: GuaribasAnswer) {
let sec = new SecService();
const member = step.context.activity.from;
const user = await sec.ensureUser(min.instance.instanceId, member.id,
@ -447,7 +448,7 @@ export class KBService implements IGBKBService {
);
const locale = step.context.activity.locale;
await min.conversationalService.sendText( min, step, Messages[locale].will_answer_projector);
await min.conversationalService.sendText(min, step, Messages[locale].will_answer_projector);
html = html.replace(/src\=\"kb\//g, `src=\"../kb/`);
await conversationalService.sendEvent(min, step, 'play', {
playerType: 'markdown',
@ -476,7 +477,11 @@ export class KBService implements IGBKBService {
// Imports subjects tree into database and return it.
await this.importSubjectFile(packageStorage.packageId, urlJoin(localPath, 'subjects.json'), instance);
const subjectFile = urlJoin(localPath, 'subjects.json');
if (Fs.existsSync(subjectFile)) {
await this.importSubjectFile(packageStorage.packageId, subjectFile, instance);
}
// Import tabular files in the tabular directory.
@ -493,39 +498,45 @@ export class KBService implements IGBKBService {
public async importRemainingArticles(localPath: string, instance: IGBInstance, packageId: number): Promise<any> {
const files = await walkPromise(urlJoin(localPath, 'articles'));
return Promise.all(
files.map(async file => {
if (file.name.endsWith('.md')) {
await CollectionUtil.asyncForEach(files, async file => {
if (file !== null && file.name.endsWith('.md')) {
let content = await this.getAnswerTextByMediaName(instance.instanceId, file.name);
let content = await this.getAnswerTextByMediaName(instance.instanceId, file.name);
if (content === null) {
if (content === null) {
const fullFilename = urlJoin(file.root, file.name);
content = Fs.readFileSync(fullFilename, 'utf-8');
const fullFilename = urlJoin(file.root, file.name);
content = Fs.readFileSync(fullFilename, 'utf-8');
await GuaribasAnswer.create({
instanceId: instance.instanceId,
content: content,
format: ".md",
media: file.name,
packageId: packageId,
prevId: 0 // TODO: Calculate total rows and increment.
});
}
await GuaribasAnswer.create({
instanceId: instance.instanceId,
content: content,
format: ".md",
media: file.name,
packageId: packageId,
prevId: 0 // TODO: Calculate total rows and increment.
});
}
}));
}
});
}
public async importKbTabularDirectory(localPath: string, instance: IGBInstance, packageId: number): Promise<any> {
const files = await walkPromise(urlJoin(localPath, 'tabular'));
let files = await walkPromise(localPath);
await CollectionUtil.asyncForEach(files, async file => {
if (file !== null && file.name.endsWith('.xlsx')) {
return await this.importKbTabularFile(urlJoin(file.root, file.name), instance.instanceId, packageId);
}
})
files = await walkPromise(urlJoin(localPath, 'tabular'));
await CollectionUtil.asyncForEach(files, async file => {
if (file !== null && file.name.endsWith('.xlsx')) {
return await this.importKbTabularFile(urlJoin(file.root, file.name), instance.instanceId, packageId);
}
});
return Promise.all(
files.map(async file => {
if (file.name.endsWith('.xlsx')) {
return await this.importKbTabularFile(urlJoin(file.root, file.name), instance.instanceId, packageId);
}
})
);
}
public async importSubjectFile(packageId: number, filename: string, instance: IGBInstance): Promise<any> {
@ -582,7 +593,7 @@ export class KBService implements IGBKBService {
const packageType = Path.extname(localPath);
const packageName = Path.basename(localPath);
GBLog.info(`[GBDeployer] Opening package: ${localPath}`);
const packageObject = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'package.json'), 'utf8'));
const instance = await core.loadInstanceByBotId(min.botId);
GBLog.info(`[GBDeployer] Importing: ${localPath}`);

View file

@ -171,23 +171,27 @@ export class WhatsappDirectLine extends GBService {
const user = await sec.ensureUser(this.min.instance.instanceId, id,
senderName, "", "whatsapp", senderName);
const locale = user.locale ? user.locale : 'pt';
const locale = user.locale ? user.locale : 'pt';
if (message.type === "ptt") {
const options = {
url: message.body,
method: 'GET',
encoding: 'binary'
};
const res = await request(options);
let buf = Buffer.from(res, 'binary');
text = await GBConversationalService.getTextFromAudioBuffer(
this.min.instance.speechKey,
this.min.instance.cloudLocation,
buf, locale
);
if (process.env.AUDIO_DISABLED !== "true") {
const options = {
url: message.body,
method: 'GET',
encoding: 'binary'
};
const res = await request(options);
let buf = Buffer.from(res, 'binary');
text = await GBConversationalService.getTextFromAudioBuffer(
this.min.instance.speechKey,
this.min.instance.cloudLocation,
buf, locale
);
}
else{
await this.sendToDevice(user.userSystemId, `No momento estou apenas conseguindo ler mensagens de texto.`);
}
}
const conversationId = this.conversationIds[from];
@ -207,7 +211,7 @@ export class WhatsappDirectLine extends GBService {
if (message === null) {
await this.sendToDeviceEx(user.userSystemId, `File ${filename} not found in any .gbkb published. Check the name or publish again the associated .gbkb.`,
locale);
locale);
} else {
await this.min.conversationalService.sendMarkdownToMobile(this.min, null, user.userSystemId, message);
}
@ -271,7 +275,7 @@ export class WhatsappDirectLine extends GBService {
conversationId: conversationId,
activity: {
textFormat: 'plain',
text: text,
text: text,
type: 'message',
from: {
id: from,
@ -369,7 +373,7 @@ export class WhatsappDirectLine extends GBService {
try {
// tslint:disable-next-line: await-promise
const result = await request.post(options);
GBLog.info( `File ${url} sent to ${to}: ${result}`);
GBLog.info(`File ${url} sent to ${to}: ${result}`);
} catch (error) {
GBLog.error(`Error sending file to Whatsapp provider ${error.message}`);
}
@ -392,7 +396,7 @@ export class WhatsappDirectLine extends GBService {
try {
// tslint:disable-next-line: await-promise
const result = await request.post(options);
GBLog.info( `Audio ${url} sent to ${to}: ${result}`);
GBLog.info(`Audio ${url} sent to ${to}: ${result}`);
} catch (error) {
GBLog.error(`Error sending audio message to Whatsapp provider ${error.message}`);
}
@ -422,7 +426,7 @@ export class WhatsappDirectLine extends GBService {
public async sendToDevice(to, msg) {
const cmd = '/audio ';
if (msg.startsWith(cmd)) {
if (msg.startsWith(cmd) && process.env.AUDIO_DISABLED !== 'true') {
msg = msg.substr(cmd.length);
return await this.sendTextAsAudioToDevice(to, msg);
}
@ -444,7 +448,7 @@ export class WhatsappDirectLine extends GBService {
try {
// tslint:disable-next-line: await-promise
const result = await request.post(options);
GBLog.info( `Message [${msg}] sent to ${to}: ${result}`);
GBLog.info(`Message [${msg}] sent to ${to}: ${result}`);
} catch (error) {
GBLog.error(`Error sending message to Whatsapp provider ${error.message}`);