diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index a9a3d737e..4211954b4 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -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. diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index af09eb3e2..1bf00cd97 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -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); @@ -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); @@ -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. diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 54d92a98b..d6b20dde4 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -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}`); } }); @@ -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. diff --git a/packages/core.gbapp/services/GBVMService.ts b/packages/core.gbapp/services/GBVMService.ts index 1a67916c4..75ee26714 100644 --- a/packages/core.gbapp/services/GBVMService.ts +++ b/packages/core.gbapp/services/GBVMService.ts @@ -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'; @@ -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) { diff --git a/packages/kb.gbapp/dialogs/AskDialog.ts b/packages/kb.gbapp/dialogs/AskDialog.ts index ba0bae152..ff5dd2491 100644 --- a/packages/kb.gbapp/dialogs/AskDialog.ts +++ b/packages/kb.gbapp/dialogs/AskDialog.ts @@ -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. diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 460be378b..36cd8cedd 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -129,7 +129,7 @@ export class WhatsappDirectLine extends GBService { } } - public resetConversationId(number) { + public async resetConversationId(number) { this.conversationIds[number] = undefined; }