Skip to content

Commit

Permalink
fix(all): Create bot working again.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jul 14, 2023
1 parent e971730 commit b92fbca
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 77 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
"winston": "3.8.2",
"winston-logs-display": "1.0.0",
"ws": "8.12.1",
"yarn": "1.22.19"
"yarn": "1.22.19",
"ngrok": "4.3.3"
},
"devDependencies": {
"@types/qrcode": "1.5.0",
Expand All @@ -193,7 +194,6 @@
"dependency-check": "4.1.0",
"git-issues": "1.0.0",
"license-checker": "25.0.1",
"ngrok": "4.3.3",
"prettier-standard": "15.0.1",
"semantic-release": "17.2.4",
"simple-commit-message": "4.0.13",
Expand Down
10 changes: 7 additions & 3 deletions packages/azuredeployer.gbapp/dialogs/StartDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ import * as Fs from 'fs';
import { GBAdminService } from '../../../packages/admin.gbapp/services/GBAdminService.js';
import { GBConfigService } from '../../../packages/core.gbapp/services/GBConfigService.js';
import scanf from 'scanf';
import { AzureDeployerService } from '../services/AzureDeployerService.js';

/**
* Handles command-line dialog for getting info for Boot Bot.
*/
export class StartDialog {
public static async createBaseInstance (installationDeployer: IGBInstallationDeployer) {
public static async createBaseInstance (deployer, freeTier) {
// No .env so asks for cloud credentials to start a new farm.

if (!Fs.existsSync(`.env`)) {
Expand Down Expand Up @@ -76,10 +77,13 @@ export class StartDialog {

let subscriptionId: string;
while (subscriptionId === undefined) {
const list = await installationDeployer.getSubscriptions(credentials);
const list = await (new AzureDeployerService()).getSubscriptions(credentials);
subscriptionId = this.retrieveSubscriptionId(list);
}

const installationDeployer = await AzureDeployerService.createInstanceWithADALCredentials(
deployer, freeTier, subscriptionId, credentials);

let location: string;
while (location === undefined) {
location = this.retrieveLocation();
Expand Down Expand Up @@ -108,7 +112,7 @@ export class StartDialog {
instance.marketplacePassword = appPassword;
instance.adminPass = GBAdminService.getRndPassword();

return { instance, credentials, subscriptionId };
return { instance, credentials, subscriptionId , installationDeployer};
}

private static retrieveUsername () {
Expand Down
56 changes: 34 additions & 22 deletions packages/azuredeployer.gbapp/services/AzureDeployerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,36 @@ export class AzureDeployerService implements IGBInstallationDeployer {
await this.deployer.rebuildIndex(instance, this.getKBSearchSchema(instance.searchIndex));
}


public static async createInstance(deployer: GBDeployer, freeTier: boolean = true): Promise<AzureDeployerService> {
const username = GBConfigService.get('CLOUD_USERNAME');
const password = GBConfigService.get('CLOUD_PASSWORD');
const subscriptionId = GBConfigService.get('CLOUD_SUBSCRIPTIONID');
return await this.createInstanceWithCredentials(deployer, freeTier, subscriptionId, username, password);
}

public static async createInstanceWithADALCredentials(deployer: GBDeployer, freeTier: boolean = true,
subscriptionId: string, credentials): Promise<AzureDeployerService> {
const service = new AzureDeployerService();

service.core = deployer.core;
service.deployer = deployer;
service.freeTier = freeTier;

const credentials = await GBAdminService.getADALCredentialsFromUsername(username, password);
const token = credentials['tokenCache']._entries[0];

await service.initServices(token.accessToken, token.expiresOn, subscriptionId);

return service;
}

public static async createInstanceWithCredentials(deployer: GBDeployer, freeTier: boolean = true,
subscriptionId: string, username: string, password: string): Promise<AzureDeployerService> {
const service = new AzureDeployerService();
const credentials = await GBAdminService.getADALCredentialsFromUsername(username, password);
return await this.createInstanceWithADALCredentials(deployer, freeTier, subscriptionId, credentials);
}

private static createRequestObject(url: string, accessToken: string, verb: HttpMethods, body: string) {
const req = new WebResource();
req.method = verb;
Expand Down Expand Up @@ -399,24 +411,24 @@ export class AzureDeployerService implements IGBInstallationDeployer {
instance.searchIndexer = 'azuresql-indexer';
instance.searchKey = searchKeys.primaryKey;

GBLog.info(`Deploying Speech...`);
const speech = await this.createSpeech(name, `${name}speech`, instance.cloudLocation);
keys = await this.cognitiveClient.accounts.listKeys(name, speech.name);
instance.speechEndpoint = speech.properties.endpoint;
instance.speechKey = keys.key1;
// GBLog.info(`Deploying Speech...`);
// const speech = await this.createSpeech(name, `${name}speech`, instance.cloudLocation);
// keys = await this.cognitiveClient.accounts.listKeys(name, speech.name);
// instance.speechEndpoint = speech.properties.endpoint;
// instance.speechKey = keys.key1;

GBLog.info(`Deploying Text Analytics...`);
const textAnalytics = await this.createTextAnalytics(name, `${name}-textanalytics`, instance.cloudLocation);
instance.textAnalyticsEndpoint = textAnalytics.properties.endpoint.replace(`/text/analytics/v2.0`, '');
// GBLog.info(`Deploying Text Analytics...`);
// const textAnalytics = await this.createTextAnalytics(name, `${name}-textanalytics`, instance.cloudLocation);
// instance.textAnalyticsEndpoint = textAnalytics.properties.endpoint.replace(`/text/analytics/v2.0`, '');

GBLog.info(`Deploying SpellChecker...`);
const spellChecker = await this.createSpellChecker(name, `${name}-spellchecker`);
instance.spellcheckerEndpoint = spellChecker.properties.endpoint;

GBLog.info(`Deploying NLP...`);
const nlp = await this.createNLP(name, `${name}-nlp`, instance.cloudLocation);
const nlpa = await this.createNLPAuthoring(name, `${name}-nlpa`, instance.cloudLocation);
instance.nlpEndpoint = nlp.properties.endpoint;
// GBLog.info(`Deploying NLP...`);
// const nlp = await this.createNLP(name, `${name}-nlp`, instance.cloudLocation);
// const nlpa = await this.createNLPAuthoring(name, `${name}-nlpa`, instance.cloudLocation);
// instance.nlpEndpoint = nlp.properties.endpoint;

const sleep = ms => {
return new Promise(resolve => {
Expand All @@ -443,19 +455,19 @@ export class AzureDeployerService implements IGBInstallationDeployer {
instance.cloudSubscriptionId
);

GBLog.info(`Waiting one minute to finishing NLP service and keys creation...`);
GBLog.info(`Waiting one minute to finish NLP service and keys creation...`);
await sleep(60000);
keys = await this.cognitiveClient.accounts.listKeys(name, textAnalytics.name);
instance.textAnalyticsKey = keys.key1;
// keys = await this.cognitiveClient.accounts.listKeys(name, textAnalytics.name);
// instance.textAnalyticsKey = keys.key1;
keys = await this.cognitiveClient.accounts.listKeys(name, spellChecker.name);
instance.spellcheckerKey = keys.key1;
let authoringKeys = await this.cognitiveClient.accounts.listKeys(name, nlpa.name);
keys = await this.cognitiveClient.accounts.listKeys(name, nlp.name);
instance.nlpKey = keys.key1;
// let authoringKeys = await this.cognitiveClient.accounts.listKeys(name, nlpa.name);
// keys = await this.cognitiveClient.accounts.listKeys(name, nlp.name);
// instance.nlpKey = keys.key1;

instance.nlpAuthoringKey = authoringKeys.key1;
const nlpAppId = await this.createNLPService(name, name, instance.cloudLocation, culture, instance.nlpAuthoringKey);
instance.nlpAppId = nlpAppId;
// instance.nlpAuthoringKey = authoringKeys.key1;
// const nlpAppId = await this.createNLPService(name, name, instance.cloudLocation, culture, instance.nlpAuthoringKey);
// instance.nlpAppId = nlpAppId;

GBLog.info('Updating server environment variables...');
await this.updateWebisteConfig(name, serverName, serverFarm.id, instance);
Expand Down
20 changes: 11 additions & 9 deletions packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import PizZip from 'pizzip';
import Docxtemplater from 'docxtemplater';
import pptxTemplaterModule from 'pptxtemplater';
import _ from 'lodash';
import { pdfToPng, PngPageOutput } from 'pdf-to-png-converter';
// TODO: canvas error import { pdfToPng, PngPageOutput } from 'pdf-to-png-converter';
import sharp from 'sharp';
import ImageModule from 'open-docxtemplater-image-module';
import DynamicsWebApi from 'dynamics-web-api';
Expand Down Expand Up @@ -350,14 +350,16 @@ export class SystemKeywords {

// Converts the PDF to PNG.

const pngPages: PngPageOutput[] = await pdfToPng(gbfile.data, {
disableFontFace: false,
useSystemFonts: false,
viewportScale: 2.0,
pagesToProcess: [1],
strictPagesToProcess: false,
verbosityLevel: 0
});
const pngPages /*: PngPageOutput*/ = [];
// TODO: https://github.com/GeneralBots/BotServer/issues/350
// await pdfToPng(gbfile.data, {
// disableFontFace: false,
// useSystemFonts: false,
// viewportScale: 2.0,
// pagesToProcess: [1],
// strictPagesToProcess: false,
// verbosityLevel: 0
// });

// Prepare an image on cache and return the GBFILE information.

Expand Down
9 changes: 9 additions & 0 deletions packages/core.gbapp/services/GBConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ export class GBConfigService {
case 'DEV_GBAI':
value = undefined;
break;
case 'FREE_TIER':
value = true;
break;
case 'BOT_URL':
value = undefined;
break;
case 'STORAGE_SERVER':
value = undefined;
break;
default:
GBLog.warn(`Invalid key on .env file: '${key}'`);
break;
Expand Down
36 changes: 26 additions & 10 deletions packages/core.gbapp/services/GBCoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { GBCustomerSatisfactionPackage } from '../../customer-satisfaction.gbapp
import { GBKBPackage } from '../../kb.gbapp/index.js';
import { GBSecurityPackage } from '../../security.gbapp/index.js';
import { GBWhatsappPackage } from '../../whatsapp.gblib/index.js';
import { GuaribasInstance, GuaribasLog} from '../models/GBModel.js';
import { GuaribasInstance, GuaribasLog } from '../models/GBModel.js';
import { GBConfigService } from './GBConfigService.js';
import { GBAzureDeployerPackage } from '../../azuredeployer.gbapp/index.js';
import { GBSharePointPackage } from '../../sharepoint.gblib/index.js';
Expand Down Expand Up @@ -105,7 +105,7 @@ export class GBCoreService implements IGBCoreService {
constructor() {
this.adminService = new GBAdminService(this);
}
public async ensureInstances(instances: IGBInstance[], bootInstance: any, core: IGBCoreService) {}
public async ensureInstances(instances: IGBInstance[], bootInstance: any, core: IGBCoreService) { }

/**
* Gets database config and connect to storage. Currently two databases
Expand Down Expand Up @@ -134,8 +134,8 @@ export class GBCoreService implements IGBCoreService {
const logging: boolean | Function =
GBConfigService.get('STORAGE_LOGGING') === 'true'
? (str: string): void => {
GBLog.info(str);
}
GBLog.info(str);
}
: false;

const encrypt: boolean = GBConfigService.get('STORAGE_ENCRYPT') === 'true';
Expand Down Expand Up @@ -318,10 +318,10 @@ ENDPOINT_UPDATE=true
*/
public async ensureProxy(port): Promise<string> {
try {
if (Fs.existsSync('node_modules/ngrok/bin/ngrok.exe') || Fs.existsSync('node_modules/ngrok/bin/ngrok')) {
if (Fs.existsSync('node_modules/ngrok/bin/ngrok.exe') || Fs.existsSync('node_modules/.bin/ngrok')) {
return await ngrok.connect({ port: port });
} else {
GBLog.warn('ngrok executable not found (only tested on Windows). Check installation or node_modules folder.');
GBLog.warn('ngrok executable not found. Check installation or node_modules folder.');

return 'https://localhost';
}
Expand Down Expand Up @@ -500,20 +500,36 @@ ENDPOINT_UPDATE=true
}
}


public async createBootInstance(
core: GBCoreService,
installationDeployer: IGBInstallationDeployer,
proxyAddress: string
) {
return await this.createBootInstanceEx(
core,
installationDeployer,
proxyAddress, null,
GBConfigService.get('FREE_TIER'));

}
/**
* Creates the first bot instance (boot instance) used to "boot" the server.
* At least one bot is required to perform conversational administrative tasks.
* So a base main bot is always deployed and will act as root bot for
* configuration tree with three levels: .env > root bot > all other bots.
*/
public async createBootInstance(
public async createBootInstanceEx(
core: GBCoreService,
installationDeployer: IGBInstallationDeployer,
proxyAddress: string
proxyAddress: string,
deployer,
freeTier
) {
GBLog.info(`Deploying cognitive infrastructure (on the cloud / on premises)...`);
try {
const { instance, credentials, subscriptionId } = await StartDialog.createBaseInstance(installationDeployer);
const { instance, credentials, subscriptionId, installationDeployer }
= await StartDialog.createBaseInstance(deployer, freeTier);
installationDeployer['core'] = this;
const changedInstance = await installationDeployer.deployFarm(
proxyAddress,
Expand All @@ -528,7 +544,7 @@ ENDPOINT_UPDATE=true
await this.openStorageFrontier(installationDeployer);
await this.initStorage();

return changedInstance;
return [changedInstance, installationDeployer];
} catch (error) {
GBLog.warn(
`There is an error being thrown, so please cleanup any infrastructure objects
Expand Down
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBSSR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export class GBSSR {
if (GBServer.globals.wwwroot && url === '/') {
path = GBServer.globals.wwwroot + "/index.html"; // TODO.
}
if (!min && !url.startsWith("/static")) {
if (!min && !url.startsWith("/static") && GBServer.globals.wwwroot) {
path = Path.join(GBServer.globals.wwwroot, url);
}
if (Fs.existsSync(path)) {
Expand Down
11 changes: 5 additions & 6 deletions packages/security.gbapp/dialogs/ProfileDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import { GBLog, GBMinInstance, IGBDialog } from 'botlib';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
import { Messages } from '../strings.js';
import * as phone from 'google-libphonenumber';
import libphonenumber from 'google-libphonenumber';

/**
* Dialogs for handling Menu control.
Expand Down Expand Up @@ -101,17 +101,16 @@ export class ProfileDialog extends IGBDialog {
},
async step => {
const locale = step.context.activity.locale;
let phoneNumber;
let phoneNumber = step.context.activity.text;
try {
let p = phone.PhoneNumberUtil.getInstance();
phoneNumber = p(step.result, 'BRA')[0]; // https://github.com/GeneralBots/BotServer/issues/307
phoneNumber = phone.phoneUtil.parse(phoneNumber);
let p = libphonenumber.PhoneNumberUtil.getInstance();
phoneNumber = p.parse(phoneNumber);
} catch (error) {
await step.context.sendActivity(Messages[locale].validation_enter_valid_mobile);

return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
}
if (!phone.phoneUtil.isPossibleNumber(phoneNumber)) {
if (!libphonenumber.phoneUtil.isPossibleNumber(phoneNumber)) {
await step.context.sendActivity(Messages[locale].validation_enter_valid_mobile);

return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
Expand Down
Loading

0 comments on commit b92fbca

Please sign in to comment.