Skip to content

Commit

Permalink
fix(core): Bot Server is runnable again after refactory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Rodriguez (pragmatismo.io) committed Nov 30, 2018
1 parent 3f32e48 commit 9379dec
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 564 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"typedoc-plugin-markdown": "^1.1.18",
"typescript": "3.1.6",
"url-join": "4.0.0",
"vbscript-to-typescript": "^1.0.8",
"wait-until": "0.0.2",
"walk-promise": "0.2.0",
"winston": "3.1.0"
Expand Down
1 change: 1 addition & 0 deletions packages/core.gbapp/dialogs/WelcomeDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class WelcomeDialog extends IGBDialog {
await step.replaceDialog('/answer', { query: step.context.activity.text });
}
}

return await step.next();
}
]));
Expand Down
47 changes: 18 additions & 29 deletions packages/core.gbapp/services/GBAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,50 +32,39 @@

'use strict';

import { WaterfallDialog } from 'botbuilder-dialogs';
import { IGBInstance, IGBPackage ,GBMinInstance } from 'botlib';
import { TurnContext } from 'botbuilder';
import { GBMinInstance } from 'botlib';

/**
* @fileoverview General Bots server core.
*/

export class DialogClass {
public min: GBMinInstance;
public context: TurnContext;

constructor(min: GBMinInstance) {
this.min = min;
}
}

public async hear(text: string): Promise<any> {
return new Promise((resolve, reject) => {
this.min.dialogs.add(
new WaterfallDialog('/vmExpect', [
async step => {
await step.prompt('textPrompt', text);
return await step.next();
},
async step => {
resolve(step.result);
return await step.next();
}
])
);
});
}

public post(url: string, data) {

// await this.context.beginDialog('textPrompt', text);
}

public talk(text: string) {
this.min.dialogs.add(
new WaterfallDialog('/vmSend', [
async step => {
await step.context.sendActivity(text);
this.context.sendActivity(text);
}

return await step.next();
}
])
);
/**
* Generic function to call any REST API.
*/
public sendEmail(to, subject, body) {
// tslint:disable-next-line:no-console
console.log(`[E-mail]: to:${to}, subject: ${subject}, body: ${body}.`);
}

/**
* Generic function to call any REST API.
*/
public post(url: string, data) {}
}
100 changes: 31 additions & 69 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const UrlJoin = require('url-join');
const Fs = require('fs');
const WaitUntil = require('wait-until');
const express = require('express');
const child_process = require('child_process');

import { GBMinInstance, IGBCoreService, IGBInstance } from 'botlib';
import { GBError } from 'botlib';
Expand All @@ -67,25 +68,17 @@ export class GBDeployer {
}

public static getConnectionStringFromInstance(instance: GuaribasInstance) {
return `Server=tcp:${
instance.storageServer
}.database.windows.net,1433;Database=${instance.storageName};User ID=${
return `Server=tcp:${instance.storageServer}.database.windows.net,1433;Database=${instance.storageName};User ID=${
instance.storageUsername
};Password=${
instance.storagePassword
};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;`;
};Password=${instance.storagePassword};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;`;
}

/**
*
* Performs package deployment in all .gbai or default.
*
* */
public deployPackages(
core: IGBCoreService,
server: any,
appPackages: IGBPackage[]
) {
public deployPackages(core: IGBCoreService, server: any, appPackages: IGBPackage[]) {
const _this = this;
return new Promise(
(resolve: any, reject: any): any => {
Expand Down Expand Up @@ -122,9 +115,7 @@ export class GBDeployer {
});
}

logger.info(
`Starting looking for packages (.gbot, .gbtheme, .gbkb, .gbapp)...`
);
logger.info(`Starting looking for packages (.gbot, .gbtheme, .gbkb, .gbapp)...`);
paths.forEach(e => {
logger.info(`Looking in: ${e}...`);
doIt(e);
Expand Down Expand Up @@ -174,10 +165,10 @@ export class GBDeployer {
/** Deploys all .gbot files first. */

botPackages.forEach(e => {
if (e!=='packages\\boot.gbot'){
logger.info(`Deploying bot: ${e}...`);
_this.deployBot(e);
logger.info(`Bot: ${e} deployed...`);
if (e !== 'packages\\boot.gbot') {
logger.info(`Deploying bot: ${e}...`);
_this.deployBot(e);
logger.info(`Bot: ${e} deployed...`);
}
});

Expand All @@ -191,27 +182,16 @@ export class GBDeployer {

/** Handles apps for general bots - .gbapp must stay out of deploy folder. */

if (
Path.extname(filename) === '.gbapp' ||
Path.extname(filename) === '.gblib'
) {
if (Path.extname(filename) === '.gbapp' || Path.extname(filename) === '.gblib') {
/** Themes for bots. */
} else if (Path.extname(filename) === '.gbtheme') {
server.use('/themes/' + filenameOnly, express.static(filename));
logger.info(
`Theme (.gbtheme) assets accessible at: ${'/themes/' +
filenameOnly}.`
);
logger.info(`Theme (.gbtheme) assets accessible at: ${'/themes/' + filenameOnly}.`);

/** Knowledge base for bots. */
} else if (Path.extname(filename) === '.gbkb') {
server.use(
'/kb/' + filenameOnly + '/subjects',
express.static(UrlJoin(filename, 'subjects'))
);
logger.info(
`KB (.gbkb) assets accessible at: ${'/kb/' + filenameOnly}.`
);
server.use('/kb/' + filenameOnly + '/subjects', express.static(UrlJoin(filename, 'subjects')));
logger.info(`KB (.gbkb) assets accessible at: ${'/kb/' + filenameOnly}.`);
} else if (Path.extname(filename) === '.gbui') {
// Already Handled
} else if (Path.extname(filename) === '.gbdialog') {
Expand Down Expand Up @@ -253,18 +233,12 @@ export class GBDeployer {
public async deployBot(localPath: string): Promise<IGBInstance> {
const packageType = Path.extname(localPath);
const packageName = Path.basename(localPath);
const instance = await this.importer.importIfNotExistsBotPackage(null,
packageName,
localPath
);
const instance = await this.importer.importIfNotExistsBotPackage(null, packageName, localPath);

return instance;
}

public async deployPackageToStorage(
instanceId: number,
packageName: string
): Promise<GuaribasPackage> {
public async deployPackageToStorage(instanceId: number, packageName: string): Promise<GuaribasPackage> {
return GuaribasPackage.create({
packageName: packageName,
instanceId: instanceId
Expand Down Expand Up @@ -308,18 +282,13 @@ export class GBDeployer {
return vm.loadJS(localPath, min, this.core, this, localPath);

default:
const err = GBError.create(
`GuaribasBusinessError: Unknown package type: ${packageType}.`
);
const err = GBError.create(`GuaribasBusinessError: Unknown package type: ${packageType}.`);
Promise.reject(err);
break;
}
}

public async undeployPackageFromLocalPath(
instance: IGBInstance,
localPath: string
) {
public async undeployPackageFromLocalPath(instance: IGBInstance, localPath: string) {
const packageType = Path.extname(localPath);
const packageName = Path.basename(localPath);

Expand All @@ -345,9 +314,7 @@ export class GBDeployer {
break;

default:
const err = GBError.create(
`GuaribasBusinessError: Unknown package type: ${packageType}.`
);
const err = GBError.create(`GuaribasBusinessError: Unknown package type: ${packageType}.`);
Promise.reject(err);
break;
}
Expand All @@ -361,9 +328,7 @@ export class GBDeployer {
instance.searchIndexer
);

const connectionString = GBDeployer.getConnectionStringFromInstance(
instance
);
const connectionString = GBDeployer.getConnectionStringFromInstance(instance);

const dsName = 'gb';
try {
Expand All @@ -375,13 +340,7 @@ export class GBDeployer {
}
}

await search.createDataSource(
dsName,
dsName,
'GuaribasQuestion',
'azuresql',
connectionString
);
await search.createDataSource(dsName, dsName, 'GuaribasQuestion', 'azuresql', connectionString);

try {
await search.deleteIndex();
Expand All @@ -391,19 +350,22 @@ export class GBDeployer {
throw err;
}
}
await search.createIndex(
AzureDeployerService.getKBSearchSchema(instance.searchIndex),
dsName
);
await search.createIndex(AzureDeployerService.getKBSearchSchema(instance.searchIndex), dsName);
}

public async getPackageByName(
instanceId: number,
packageName: string
): Promise<GuaribasPackage> {
public async getPackageByName(instanceId: number, packageName: string): Promise<GuaribasPackage> {
const where = { packageName: packageName, instanceId: instanceId };
return GuaribasPackage.findOne({
where: where
});
}

public installDefaultGBUI() {
const root = 'packages/default.gbui';
if (!Fs.existsSync(`${root}/build`)) {
Fs.writeFileSync(`${root}/.env`, 'SKIP_PREFLIGHT_CHECK=true');
child_process.execSync('npm install', { cwd: root });
child_process.execSync('npm run build', { cwd: root });
}
}
}
14 changes: 11 additions & 3 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class GBMinService {
* */

public async buildMin(
bootInstance: GuaribasInstance,
server: any,
appPackages: IGBPackage[],
instances: GuaribasInstance[],
Expand All @@ -134,7 +135,11 @@ export class GBMinService {
(async () => {
// Returns the instance object to clients requesting bot info.

const botId = req.params.botId;
let botId = req.params.botId;
if (botId === '[default]'){
botId = bootInstance.botId;
}

const instance = await this.core.loadInstance(botId);

if (instance) {
Expand Down Expand Up @@ -477,8 +482,10 @@ export class GBMinService {
// Processes messages.
} else if (context.activity.type === 'message') {
// Checks for /admin request.

if (context.activity.text === 'admin') {
if (context.activity.text === 'vba') {
min.sandbox.context = context;
min.sandbox['chat'](min.sandbox);
} else if (context.activity.text === 'admin') {
await step.beginDialog('/admin');

// Checks for /menu JSON signature.
Expand All @@ -500,6 +507,7 @@ export class GBMinService {

// Processes events.
} else if (context.activity.type === 'event') {

// Empties dialog stack before going to the target.

await step.endAll();
Expand Down
17 changes: 5 additions & 12 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,17 @@

'use strict';

import { IGBCoreService, IGBInstance } from 'botlib';
import { GBMinInstance } from 'botlib';
const logger = require('../../../src/logger');
import { BotAdapter } from 'botbuilder';
import { WaterfallDialog } from 'botbuilder-dialogs';
import { GBMinInstance, IGBCoreService } from 'botlib';
import * as fs from 'fs';
import { Messages } from '../strings';
import { DialogClass } from './GBAPIService';
import { GBDeployer } from './GBDeployer';
const util = require('util');
const logger = require('../../../src/logger');
const vm = require('vm');
import processExists = require('process-exists');
import { Sequelize } from 'sequelize-typescript';
const UrlJoin = require('url-join');

/**
* @fileoverview General Bots server core.
* @fileoverview Virtualization services for emulation of BASIC.
*/

export class GBVMService implements IGBCoreService {
Expand All @@ -66,12 +60,11 @@ export class GBVMService implements IGBCoreService {
const code: string = fs.readFileSync(localPath, 'utf8');
const sandbox: DialogClass = new DialogClass(min);
const context = vm.createContext(sandbox);

vm.runInContext(code, context);
console.log(util.inspect(sandbox));
sandbox['chat'](sandbox);

await deployer.deployScriptToStorage(min.instanceId, filename);
logger.info(`[GBVMService] Finished loading of ${filename}`);

min.sandbox = sandbox;
}
}

0 comments on commit 9379dec

Please sign in to comment.