Skip to content

Commit

Permalink
fix(core.gbapp): BASIC and KB improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jun 4, 2020
1 parent f3a32d0 commit 1bf23cf
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 71 deletions.
2 changes: 1 addition & 1 deletion packages/admin.gbapp/services/GBAdminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class GBAdminService implements IGBAdminService {
maximumLength: 14
};
let password = passwordGenerator.generatePassword(options);
password = password.replace(/[\@\[\=\:\;\?]/g, '#');
password = password.replace(/[\@\[\=\:\;\?]/gi, '#');

return password;
}
Expand Down
74 changes: 39 additions & 35 deletions packages/core.gbapp/services/GBAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,51 +96,55 @@ class SysClass {
}

public async save(file: string, ...args): Promise<any> {
try {
let token =
await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);

let token =
await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
let siteId = process.env.STORAGE_SITE_ID;
let libraryId = process.env.STORAGE_LIBRARY;

let siteId = process.env.STORAGE_SITE_ID;
let libraryId = process.env.STORAGE_LIBRARY;
let client = MicrosoftGraph.Client.init({
authProvider: done => {
done(null, token);
}
});
const botId = this.min.instance.botId;
const path = `/${botId}/${botId}.gbdata`;

let client = MicrosoftGraph.Client.init({
authProvider: done => {
done(null, token);
}
});
const botId = this.min.instance.botId;
const path = `/${botId}/${botId}.gbdata`;
let res = await client.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
.get();

let res = await client.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
.get();
let document = res.value.filter(m => {
return m.name === file
});

let document = res.value.filter(m => {
return m.name === file
});
await client.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A1:Z1')/insert`)
.post({});

await client.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A1:Z1')/insert`)
.post({});
if (document === undefined) {
throw `File '${file}' specified on save GBasic command SAVE not found. Check the .gbdata or the .gbdialog associated.`;
}
if (args.length > 27) {
throw `File '${file}' has a SAVE call with more than 27 arguments. Check the .gbdialog associated.`;
}

if (document === undefined) {
throw `File '${file}' specified on save GBasic command SAVE not found. Check the .gbdata or the .gbdialog associated.`;
}
if (args.length > 27) {
throw `File '${file}' has a SAVE call with more than 27 arguments. Check the .gbdialog associated.`;
}
let body =
{ "values": [[]] };

let body =
{ "values": [[]] };
for (let index = 0; index < 26; index++) {
body.values[0][index] = args[index];
}

for (let index = 0; index < 26; index++) {
body.values[0][index] = args[index];
let res2 = await client.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:Z2')`)
.patch(body);
} catch (error) {
GBLog.error(`SAVE BASIC error: ${error.message}`);
throw error;
}

let res2 = await client.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:Z2')`)
.patch(body);

}

public async find(file: string, ...args): Promise<any> {
Expand Down Expand Up @@ -264,7 +268,7 @@ class SysClass {
}

public async numberOnly(text: string) {
return text.replace(/\D/g, "");
return text.replace(/\D/gi, "");
}

}
Expand Down
16 changes: 9 additions & 7 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,14 @@ export class GBConversationalService {
const member = step.context.activity.from;
const user = await sec.ensureUser(min.instance.instanceId, member.id,
member.name, "", "web", member.name);
text = await min.conversationalService.translate(
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
text,
user.locale ? user.locale : 'pt'
);
if (text !== null) {
text = await min.conversationalService.translate(
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
text,
user.locale ? user.locale : 'pt'
);
}

return await step.prompt("textPrompt", text ? text : {});
}
Expand All @@ -600,7 +602,7 @@ export class GBConversationalService {
text = await min.conversationalService.translate(
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
text,
text,
user.locale ? user.locale : 'pt'
);

Expand Down
7 changes: 4 additions & 3 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,19 @@ export class GBDeployer implements IGBDeployer {

case '.gbkb':
const service = new KBService(this.core.sequelize);
rimraf.sync(localPath);
return await service.undeployKbFromStorage(instance, this, p.packageId);

case '.gbui':

break;

case '.gbtheme':

rimraf.sync(localPath);
break;

case '.gbdialog':

rimraf.sync(localPath);
break;

case '.gblib':
Expand Down
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ export class GBMinService {
}

public static async callVM(text: string, min: GBMinInstance, step: GBDialogStep) {
const mainMethod = text;
const mainMethod = text.toLowerCase();
min.sandBoxMap[mainMethod][mainMethod].bind(min.sandBoxMap[mainMethod]);
return await min.sandBoxMap[mainMethod][mainMethod](step);
}
Expand Down
41 changes: 21 additions & 20 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ export class GBVMService extends GBService {
fs.writeFileSync(urlJoin(folder, filename), text);
}

const mainName = filename.replace(/\s|\-/g, '').split('.')[0];
min.scriptMap[filename] = mainName;
let mainName = filename.replace(/\s|\-/gi, '').split('.')[0];
mainName = mainName.toLowerCase();
min.scriptMap[filename] = mainName.toLowerCase();

const fullFilename = urlJoin(folder, filename);
// TODO: Implement in development mode, how swap for .vbs files
Expand Down Expand Up @@ -138,39 +139,39 @@ export class GBVMService extends GBService {

// Keywords from General Bots BASIC.

code = code.replace(/(hear email)/g, `email = askEmail()`);
code = code.replace(/(hear email)/gi, `email = askEmail()`);

code = code.replace(/(hear)\s*(\w+)/g, ($0, $1, $2) => {
code = code.replace(/(hear)\s*(\w+)/gi, ($0, $1, $2) => {
return `${$2} = hear()`;
});
code = code.replace(/(\w)\s*\=\s*find\s*(.*)/g, ($0, $1, $2, $3) => {
code = code.replace(/(\w)\s*\=\s*find\s*(.*)/gi, ($0, $1, $2, $3) => {
return `${$1} = sys().find(${$2})\n`;
});

code = code.replace(/(wait)\s*(\d+)/g, ($0, $1, $2) => {
code = code.replace(/(wait)\s*(\d+)/gi, ($0, $1, $2) => {
return `sys().wait(${$2})`;
});

code = code.replace(/(get stock for )(.*)/g, ($0, $1, $2) => {
code = code.replace(/(get stock for )(.*)/gi, ($0, $1, $2) => {
return `let stock = sys().getStock(${$2})`;
});

code = code.replace(/(get)(\s)(.*)/g, ($0, $1, $2) => {
code = code.replace(/(get)(\s)(.*)/gi, ($0, $1, $2) => {
return `sys().httpGet (${$2})`;
});

code = code.replace(/(create a bot farm using)(\s)(.*)/g, ($0, $1, $2, $3) => {
code = code.replace(/(create a bot farm using)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `sys().createABotFarmUsing (${$3})`;
});

code = code.replace(/(talk)(\s)(.*)/g, ($0, $1, $2, $3) => {
code = code.replace(/(talk)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `talk (step, ${$3})\n`;
});
code = code.replace(/(send file)(\s*)(.*)/g, ($0, $1, $2, $3) => {
code = code.replace(/(send file)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `sendFile (step, ${$3})\n`;
});

code = code.replace(/(save)(\s)(.*)/g, ($0, $1, $2, $3) => {
code = code.replace(/(save)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `sys().save(${$3})\n`;
});

Expand All @@ -193,7 +194,7 @@ export class GBVMService extends GBService {
// Convert TS into JS.
const tsfile: string = `${filename}.ts`;
let tsCode: string = fs.readFileSync(tsfile, 'utf8');
tsCode = tsCode.replace(/export.*\n/g, `export function ${mainName}(step:any) { let resolve = undefined;`);
tsCode = tsCode.replace(/export.*\n/gi, `export function ${mainName}(step:any) { let resolve = undefined;`);
fs.writeFileSync(tsfile, tsCode);

const tsc = new TSCompiler();
Expand Down Expand Up @@ -275,7 +276,7 @@ export class GBVMService extends GBService {
const sandbox: DialogClass = new DialogClass(min, deployer);
const context = vm.createContext(sandbox);
vm.runInContext(parsedCode, context);
min.sandBoxMap[mainName] = sandbox;
min.sandBoxMap[mainName.toLowerCase()] = sandbox;
GBLog.info(`[GBVMService] Finished loading of ${filename}`);
} catch (error) {
GBLog.error(`[GBVMService] ERROR loading ${error}`);
Expand All @@ -286,20 +287,20 @@ export class GBVMService extends GBService {
private handleThisAndAwait(code: string) {
// this insertion.

code = code.replace(/sys\(\)/g, 'this.sys()');
code = code.replace(/("[^"]*"|'[^']*')|\btalk\b/g, ($0, $1) => {
code = code.replace(/sys\(\)/gi, 'this.sys()');
code = code.replace(/("[^"]*"|'[^']*')|\btalk\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.talk' : $1;
});
code = code.replace(/("[^"]*"|'[^']*')|\bhear\b/g, ($0, $1) => {
code = code.replace(/("[^"]*"|'[^']*')|\bhear\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.hear' : $1;
});
code = code.replace(/("[^"]*"|'[^']*')|\bsendEmail\b/g, ($0, $1) => {
code = code.replace(/("[^"]*"|'[^']*')|\bsendEmail\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.sendEmail' : $1;
});
code = code.replace(/("[^"]*"|'[^']*')|\baskEmail\b/g, ($0, $1) => {
code = code.replace(/("[^"]*"|'[^']*')|\baskEmail\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.askEmail' : $1;
});
code = code.replace(/("[^"]*"|'[^']*')|\bsendFile\b/g, ($0, $1) => {
code = code.replace(/("[^"]*"|'[^']*')|\bsendFile\b/gi, ($0, $1) => {
return $1 === undefined ? 'this.sendFile' : $1;
});

Expand Down
2 changes: 1 addition & 1 deletion packages/kb.gbapp/dialogs/AskDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class AskDialog extends IGBDialog {
const urlSufix = 'url:';

if (answer.content.endsWith('.docx')) {
const mainName = answer.content.replace(/\s|\-/g, '').split('.')[0];
const mainName = answer.content.replace(/\s|\-/gi, '').split('.')[0];
return await GBMinService.callVM(mainName, min, step);
} else {
await service.sendAnswer(min, AskDialog.getChannel(step), step, answer);
Expand Down
5 changes: 2 additions & 3 deletions packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ 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';
import { try } from 'bluebird';

/**
* Result for quey on KB data.
Expand Down Expand Up @@ -302,7 +301,7 @@ export class KBService implements IGBKBService {

let media = null;

if (!answer) {
if (typeof (answer) !== "string" ) {
GBLog.info(`[GBImporter] Answer is NULL related to Question '${question}'.`);
answer = 'Existe um problema na base de conhecimento. Fui treinado para entender sua pergunta, avise a quem me criou que a resposta não foi informada para esta pergunta.';
} else if (answer.indexOf('.md') > -1) {
Expand Down Expand Up @@ -455,7 +454,7 @@ export class KBService implements IGBKBService {

const locale = step.context.activity.locale;
await min.conversationalService.sendText(min, step, Messages[locale].will_answer_projector);
html = html.replace(/src\=\"kb\//g, `src=\"../kb/`);
html = html.replace(/src\=\"kb\//gi, `src=\"../kb/`);
await conversationalService.sendEvent(min, step, 'play', {
playerType: 'markdown',
data: {
Expand Down

0 comments on commit 1bf23cf

Please sign in to comment.