Skip to content

Commit

Permalink
fix(core.gbapp): Several fixes and translator swicher.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jun 15, 2020
1 parent 5a99ef0 commit cca1488
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 58 deletions.
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export class GBConversationalService {
const translatorEnabled = () => {
if (min.instance.params) {
const params = JSON.parse(min.instance.params);
return params['Enable Worldwide Translator'] === "TRUE";
return params ? params['Enable Worldwide Translator'] === "TRUE" : false;
}
return false;
} // TODO: Encapsulate.
Expand Down
12 changes: 9 additions & 3 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ export class GBDeployer implements IGBDeployer {
public async deployBlankBot(botId: string) {
let instance = await this.importer.createBotInstance(botId);

const bootInstance = GBServer.globals.bootInstance;
const accessToken = await GBServer.globals.minBoot.adminService
.acquireElevatedToken(GBServer.globals.bootInstance.instanceId);
.acquireElevatedToken(bootInstance.instanceId);

const service = new AzureDeployerService(this);
let application = await service.createApplication(accessToken, botId);
Expand All @@ -168,6 +169,10 @@ export class GBDeployer implements IGBDeployer {
instance.state = 'active';
instance.nlpScore = 0.80; // TODO: Migrate to Excel Config.xlsx.
instance.searchScore = 0.45;
instance.whatsappServiceKey = bootInstance.whatsappServiceKey;
instance.whatsappBotKey = bootInstance.whatsappBotKey;
instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber;
instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl;

await this.core.saveInstance(instance);

Expand Down Expand Up @@ -281,8 +286,9 @@ export class GBDeployer implements IGBDeployer {
let document = res.value.filter(m => {
return m.name === "Config.xlsx"
});
if (document === undefined) {
throw `Config.xlsx not found on .bot folder, check the package.`;
if (document === undefined || document.length === 0) {
GBLog.info(`Config.xlsx not found on .bot folder, check the package.`);
return null;
}

// Creates workbook session that will be discarded.
Expand Down
4 changes: 2 additions & 2 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class GBMinService {
await (GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res);
}
} catch (error) {
GBLog.error(`Error on Whatsapp callback: ${error}`);
GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error}`);
}

});
Expand Down Expand Up @@ -705,7 +705,7 @@ export class GBMinService {
const translatorEnabled = () => {
if (min.instance.params) {
const params = JSON.parse(min.instance.params);
return params['Enable Worldwide Translator'] === "TRUE";
return params?params['Enable Worldwide Translator'] === "TRUE": false;
}
return false;
} // TODO: Encapsulate.
Expand Down
101 changes: 51 additions & 50 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ import { GBLog, GBMinInstance, GBService, IGBCoreService } from 'botlib';
import * as fs from 'fs';
import { GBDeployer } from './GBDeployer';
import { TSCompiler } from './TSCompiler';

import { CollectionUtil } from 'pragmatismo-io-framework';
const walkPromise = require('walk-promise');

const vm = require('vm');
import urlJoin = require('url-join');
import { DialogClass } from './GBAPIService';
Expand Down Expand Up @@ -69,63 +68,65 @@ export class GBVMService extends GBService {
const files = await walkPromise(folder);
this.addHearDialog(min);

return Promise.all(
files.map(async file => {
await CollectionUtil.asyncForEach(files, async file => {
if (!file) {

let filename: string = file.name;
return;
}

if (filename.endsWith('.docx')) {
const wordFile = filename;
const vbsFile = filename.substr(0, filename.indexOf('docx')) + 'vbs';
const fullVbsFile = urlJoin(folder, vbsFile);
const docxStat = fs.statSync(urlJoin(folder, wordFile));
const interval = 30000; // If compiled is older 30 seconds, then recompile.
let writeVBS = true;
if (fs.existsSync(fullVbsFile)) {
const vbsStat = fs.statSync(fullVbsFile);
if (docxStat.mtimeMs < (vbsStat.mtimeMs + interval)) {
writeVBS = false;
}
let filename: string = file.name;

if (filename.endsWith('.docx')) {
const wordFile = filename;
const vbsFile = filename.substr(0, filename.indexOf('docx')) + 'vbs';
const fullVbsFile = urlJoin(folder, vbsFile);
const docxStat = fs.statSync(urlJoin(folder, wordFile));
const interval = 30000; // If compiled is older 30 seconds, then recompile.
let writeVBS = true;
if (fs.existsSync(fullVbsFile)) {
const vbsStat = fs.statSync(fullVbsFile);
if (docxStat.mtimeMs < (vbsStat.mtimeMs + interval)) {
writeVBS = false;
}
if (writeVBS) {
}
if (writeVBS) {

let text = await this.getTextFromWord(folder, wordFile);
fs.writeFileSync(urlJoin(folder, vbsFile), text);
}
let text = await this.getTextFromWord(folder, wordFile);
fs.writeFileSync(urlJoin(folder, vbsFile), text);
}

filename = vbsFile;

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
// fs.watchFile(fullFilename, async () => {
// await this.run(fullFilename, min, deployer, mainName);
// });

filename = vbsFile;

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
// fs.watchFile(fullFilename, async () => {
// await this.run(fullFilename, min, deployer, mainName);
// });

const compiledAt = fs.statSync(fullFilename);
const jsfile = urlJoin(folder, `${filename}.js`);

if (fs.existsSync(jsfile)) {
const jsStat = fs.statSync(jsfile);
const interval = 30000; // If compiled is older 30 seconds, then recompile.
if (compiledAt.isFile() && compiledAt.mtimeMs > (jsStat.mtimeMs + interval)) {
await this.executeBASIC(fullFilename, min, deployer, mainName);
}
else {
const parsedCode: string = fs.readFileSync(jsfile, 'utf8');
this.executeJS(min, deployer, parsedCode, mainName);
}
const compiledAt = fs.statSync(fullFilename);
const jsfile = urlJoin(folder, `${filename}.js`);

if (fs.existsSync(jsfile)) {
const jsStat = fs.statSync(jsfile);
const interval = 30000; // If compiled is older 30 seconds, then recompile.
if (compiledAt.isFile() && compiledAt.mtimeMs > (jsStat.mtimeMs + interval)) {
await this.executeBASIC(fullFilename, min, deployer, mainName);
}
else {
await this.executeBASIC(fullFilename, min, deployer, mainName);
const parsedCode: string = fs.readFileSync(jsfile, 'utf8');
this.executeJS(min, deployer, parsedCode, mainName);
}

}
})
);
else {
await this.executeBASIC(fullFilename, min, deployer, mainName);
}

}
});
}

private async getTextFromWord(folder: string, filename: string) {
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 @@ -103,7 +103,7 @@ export class AskDialog extends IGBDialog {
const translatorEnabled = () => {
if (min.instance.params) {
const params = JSON.parse(min.instance.params);
return params['Enable Worldwide Translator'] === "TRUE";
return params?params['Enable Worldwide Translator'] === "TRUE": false;
}
return false;
} // TODO: Encapsulate.
Expand Down
2 changes: 1 addition & 1 deletion packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class WhatsappDirectLine extends GBService {
}
}

public resetConversationId(number) {
public async resetConversationId(number) {
this.conversationIds[number] = undefined;
}

Expand Down

0 comments on commit cca1488

Please sign in to comment.