Skip to content

Commit

Permalink
fix(gbdialog): VBA loop done - one thing left to automate: Hear wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Rodriguez (pragmatismo.io) committed Dec 1, 2018
1 parent ce04290 commit 776fe03
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"botbuilder-choices": "^4.0.0-preview1.2",
"botbuilder-dialogs": "^4.1.5",
"botbuilder-prompts": "^4.0.0-preview1.2",
"botlib": "^0.1.7",
"botlib": "0.1.8",
"chai": "4.2.0",
"child_process": "^1.0.2",
"chokidar": "2.0.4",
Expand Down
6 changes: 3 additions & 3 deletions packages/core.gbapp/services/GBAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export class DialogClass {
}

public async hear(cb) {
const id = Math.floor(Math.random() * 1000000000000);
this.min.cbMap[id] = cb;
await this.step.beginDialog('/feedback', { id: id });
let idCallback = Math.floor(Math.random() * 1000000000000);
this.min.cbMap[idCallback] = cb;
await this.step.beginDialog('/hear', { id: idCallback});
}

public async talk(text: string) {
Expand Down
4 changes: 1 addition & 3 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export class GBMinService {
min.conversationalService = this.conversationalService;
min.adminService = this.adminService;
min.instance = await this.core.loadInstance(min.botId);

min.cbMap = {};
min.userProfile = conversationState.createProperty('userProfile');
const dialogState = conversationState.createProperty('dialogState');

Expand Down Expand Up @@ -408,14 +408,12 @@ export class GBMinService {
}

// Processes messages.

} else if (context.activity.type === 'message') {
// Checks for /admin request.
if (context.activity.text === 'vba') {
min.sandbox.context = context;
min.sandbox.step = step;
min.sandbox['bot'].bind(min.sandbox);

await min.sandbox['bot']();
} else if (context.activity.text === 'admin') {
await step.beginDialog('/admin');
Expand Down
29 changes: 29 additions & 0 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import * as fs from 'fs';
import { DialogClass } from './GBAPIService';
import { GBDeployer } from './GBDeployer';
import { TSCompiler } from './TSCompiler';
import { WaterfallDialog } from 'botbuilder-dialogs';
const util = require('util');
const logger = require('../../../src/logger');
const vm = require('vm');
Expand Down Expand Up @@ -66,6 +67,31 @@ export class GBVMService implements IGBCoreService {
await this.run(source, path, localPath, min, deployer, filename);
});
await this.run(source, path, localPath, min, deployer, filename);
this.addHearDialog(min);
}

private addHearDialog(min) {
min.dialogs.add(
new WaterfallDialog('/hear', [
async step => {
step.activeDialog.state.cbId = step.options['id'];
step.activeDialog.state.idResolve = step.options['idResolve'];

return await step.prompt('textPrompt', {});
},
async step => {
min.sandbox.context = step.context;
min.sandbox.step = step;

const cbId = step.activeDialog.state.cbId;
const cb = min.cbMap[cbId];
cb.bind({ step: step, context: step.context }); // TODO: Necessary or min.sandbox
await cb();

return await step.next();
}
])
);
}

private async run(source: any, path: string, localPath: string, min: any, deployer: GBDeployer, filename: string) {
Expand All @@ -80,11 +106,14 @@ export class GBVMService implements IGBCoreService {
// Run JS into the GB context.
const jsfile = `bot.js`;
localPath = UrlJoin(path, jsfile);

if (fs.existsSync(localPath)) {
let code: string = fs.readFileSync(localPath, 'utf8');
code = code.replace(/^.*exports.*$/gm, '');
code = code.replace(/this\./gm, 'await this.');
code = code.replace(/function/gm, 'async function');
//code = code.replace(/this\.hear\(\){/gm, 'this.hear(async () => { ');

const sandbox: DialogClass = new DialogClass(min);
const context = vm.createContext(sandbox);
vm.runInContext(code, context);
Expand Down
39 changes: 14 additions & 25 deletions packages/customer-satisfaction.gbapp/dialogs/FeedbackDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,24 @@ export class FeedbackDialog extends IGBDialog {
return await step.prompt('textPrompt', Messages[locale].what_about_service);
},
async step => {

console.log(step.result);

// min.sandbox.context = step.context;
// min.sandbox.step = step;

let cbId = step.activeDialog.state.cbId;
let cb = min.cbMap[cbId];
cb.bind({ step: step, context: step.context });
await cb();

// const locale = step.context.activity.locale;
// const rate = await AzureText.getSentiment(
// min.instance.textAnalyticsKey,
// min.instance.textAnalyticsEndpoint,
// min.conversationalService.getCurrentLanguage(step),
// step.result
// );
const locale = step.context.activity.locale;
const rate = await AzureText.getSentiment(
min.instance.textAnalyticsKey,
min.instance.textAnalyticsEndpoint,
min.conversationalService.getCurrentLanguage(step),
step.result
);

// if (rate > 0.5) {
// await step.context.sendActivity(Messages[locale].glad_you_liked);
// } else {
// await step.context.sendActivity(Messages[locale].we_will_improve);
if (rate > 0.5) {
await step.context.sendActivity(Messages[locale].glad_you_liked);
} else {
await step.context.sendActivity(Messages[locale].we_will_improve);

// // TODO: Record.
// }
// await step.replaceDialog('/ask', { isReturning: true });
// TODO: Record.
}
return await step.replaceDialog('/ask', { isReturning: true });

return await step.next();
}
])
);
Expand Down

0 comments on commit 776fe03

Please sign in to comment.