From eed995e46094b304602cc9f1b0271d46f42e990e Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 25 Feb 2020 12:37:10 -0300 Subject: [PATCH] fix(core.gbapp): GB Apps can now publish bots and replace root web application. --- .../services/AzureDeployerService.ts | 27 +++++++++++++++++-- packages/core.gbapp/dialogs/WelcomeDialog.ts | 7 +++++ .../services/GBConversationalService.ts | 2 +- packages/core.gbapp/services/GBCoreService.ts | 5 ++++ packages/core.gbapp/services/GBDeployer.ts | 18 ++++++++++--- packages/core.gbapp/services/GBMinService.ts | 2 +- .../services/CSService.ts | 4 +-- src/app.ts | 1 + 8 files changed, 57 insertions(+), 9 deletions(-) diff --git a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts index a66c54f3..d410d1fb 100644 --- a/packages/azuredeployer.gbapp/services/AzureDeployerService.ts +++ b/packages/azuredeployer.gbapp/services/AzureDeployerService.ts @@ -49,6 +49,7 @@ import { GBAdminService } from '../../../packages/admin.gbapp/services/GBAdminSe import { GBCorePackage } from '../../../packages/core.gbapp'; import { GBConfigService } from '../../../packages/core.gbapp/services/GBConfigService'; import { GBDeployer } from '../../../packages/core.gbapp/services/GBDeployer'; +const MicrosoftGraph = require("@microsoft/microsoft-graph-client"); const Spinner = require('cli-spinner').Spinner; // tslint:disable-next-line: no-submodule-imports @@ -337,7 +338,7 @@ export class AzureDeployerService implements IGBInstallationDeployer { endIpAddress: ip }; await storageClient.firewallRules.createOrUpdate(groupName, serverName, 'gb', params); - + // AllowAllWindowsAzureIps must be created that way, so the Azure Search can // access SQL Database to index its contents. @@ -372,7 +373,7 @@ export class AzureDeployerService implements IGBInstallationDeployer { keys = await this.cognitiveClient.accounts.listKeys(name, nlp.name); const nlpAppId = await this.createNLPService(name, name, instance.cloudLocation, culture, instance.nlpAuthoringKey); - instance.nlpEndpoint = urlJoin(nlp.endpoint, 'apps'); + instance.nlpEndpoint = urlJoin(nlp.endpoint, 'apps'); instance.nlpKey = keys.key1; instance.nlpAppId = nlpAppId; @@ -581,6 +582,28 @@ export class AzureDeployerService implements IGBInstallationDeployer { return await this.storageClient.servers.createOrUpdate(group, name, params); } + public async createApplication(token: string, name: string) { + return new Promise((resolve, reject) => { + let client = MicrosoftGraph.Client.init({ + authProvider: done => { + done(null, token); + } + }); + const app = { + displayName: name + }; + + client.api(`/applications`).post(app, (err, res) => { + if (err) { + reject(err) + } + else { + resolve(res); + } + }); + }); + } + private async registerProviders(subscriptionId, baseUrl, accessToken) { const query = `subscriptions/${subscriptionId}/providers/${this.provider}/register?api-version=2018-02-01`; const requestUrl = urlJoin(baseUrl, query); diff --git a/packages/core.gbapp/dialogs/WelcomeDialog.ts b/packages/core.gbapp/dialogs/WelcomeDialog.ts index cfc026f7..a247871c 100644 --- a/packages/core.gbapp/dialogs/WelcomeDialog.ts +++ b/packages/core.gbapp/dialogs/WelcomeDialog.ts @@ -40,6 +40,7 @@ import { BotAdapter } from 'botbuilder'; import {WaterfallDialog } from 'botbuilder-dialogs'; import { GBMinInstance, IGBDialog } from 'botlib'; import { Messages } from '../strings'; +import { GBServer } from '../../../src/app'; /** * Dialog for Welcoming people. @@ -56,6 +57,12 @@ export class WelcomeDialog extends IGBDialog { min.dialogs.add(new WaterfallDialog('/', [ async step => { + if (GBServer.globals.entryPointDialog !== undefined) + { + + return step.replaceDialog(GBServer.globals.entryPointDialog); + } + const user = await min.userProfile.get(context, {}); const locale = step.context.activity.locale; diff --git a/packages/core.gbapp/services/GBConversationalService.ts b/packages/core.gbapp/services/GBConversationalService.ts index 24b579b2..bf1a3edb 100644 --- a/packages/core.gbapp/services/GBConversationalService.ts +++ b/packages/core.gbapp/services/GBConversationalService.ts @@ -60,7 +60,7 @@ export class GBConversationalService implements IGBConversationalService { this.coreService = coreService; } - public static getNewMobileCode() { + public getNewMobileCode() { const passwordGenerator = new PasswordGenerator(); const options = { upperCaseAlpha: false, diff --git a/packages/core.gbapp/services/GBCoreService.ts b/packages/core.gbapp/services/GBCoreService.ts index 3b5a9677..5bdc8bf5 100644 --- a/packages/core.gbapp/services/GBCoreService.ts +++ b/packages/core.gbapp/services/GBCoreService.ts @@ -288,6 +288,11 @@ STORAGE_SYNC=true } } + public setEntryPointDialog(dialogName: string) + { + GBServer.globals.entryPointDialog = dialogName; + } + public setWWWRoot(localPath: string) { GBServer.globals.wwwroot = localPath; diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index ab4eec26..5c31507a 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -163,11 +163,23 @@ export class GBDeployer { ); } - public async deployBlankBot(botId: string){ let instance = await this.importer.createBotInstance(botId); - return this.deployBotFull(instance, GBServer.globals.publicAddress); + + const username = GBConfigService.get('CLOUD_USERNAME'); + const password = GBConfigService.get('CLOUD_PASSWORD'); + const accessToken = await GBAdminService.getADALTokenFromUsername(username, password); + const service = new AzureDeployerService(this); + let application = service.createApplication(accessToken, botId); + + instance.marketplaceId = (application as any).appId; + instance.marketplacePassword = (application as any).passwordCredentials[0]; + instance.adminPass = GBAdminService.getRndPassword(); + + await this.core.saveInstance(instance); + + return this.deployBotFull(instance, GBServer.globals.publicAddress); } /** @@ -179,9 +191,9 @@ export class GBDeployer { const service = new AzureDeployerService(this); const username = GBConfigService.get('CLOUD_USERNAME'); const password = GBConfigService.get('CLOUD_PASSWORD'); + const accessToken = await GBAdminService.getADALTokenFromUsername(username, password); const group = GBConfigService.get('CLOUD_GROUP'); const subscriptionId = GBConfigService.get('CLOUD_SUBSCRIPTIONID'); - const accessToken = await GBAdminService.getADALTokenFromUsername(username, password); if (await service.botExists(instance.botId, group)) { await service.updateBot( diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index ad7c0c0e..f9ec4aae 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -385,7 +385,7 @@ export class GBMinService { min.core = this.core; min.conversationalService = this.conversationalService; min.adminService = this.adminService; - min.deployer = this.deployer; + min.deployService = this.deployer; min.instance = await this.core.loadInstance(min.botId); min.cbMap = {}; min.scriptMap = {}; diff --git a/packages/customer-satisfaction.gbapp/services/CSService.ts b/packages/customer-satisfaction.gbapp/services/CSService.ts index c403e052..e8d8b447 100644 --- a/packages/customer-satisfaction.gbapp/services/CSService.ts +++ b/packages/customer-satisfaction.gbapp/services/CSService.ts @@ -32,7 +32,7 @@ import { GuaribasConversation } from '../../analytics.gblib/models'; import { GuaribasQuestionAlternate } from '../models'; -import { GuaribasQuestion } from 'packages/kb.gbapp/models'; +import { GuaribasQuestion } from '../../../packages/kb.gbapp/models'; /** * Customer Satisfaction Service Layer. @@ -57,7 +57,7 @@ export class CSService { question = await GuaribasQuestion.findOne({ where: { instanceId: instanceId, - questionId: questionAlternate.questionTyped; + questionId: questionAlternate.questionTyped } }); } diff --git a/src/app.ts b/src/app.ts index d8950c9f..91512c19 100644 --- a/src/app.ts +++ b/src/app.ts @@ -65,6 +65,7 @@ export class RootData { public minInstances: any[]; // public minBoot: GBMinInstance; public wwwroot: string; // .gbui or a static webapp. + public entryPointDialog: string; // To replace default welcome dialog. } /**