Skip to content

Commit

Permalink
fix(core.gbapp): Faster bot creation, fix on WhatsApp switcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jun 15, 2020
1 parent cca1488 commit 8c68332
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 71 deletions.
36 changes: 18 additions & 18 deletions packages/azuredeployer.gbapp/services/AzureDeployerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export class AzureDeployerService implements IGBInstallationDeployer {
msaAppId: appId,
msaAppPassword: appPassword,
enabledChannels: ['webchat', "skype"],//, "facebook"],
configuredChannels: ['webchat' , "skype"]//, "facebook"]
configuredChannels: ['webchat', "skype"]//, "facebook"]
}
};

Expand All @@ -539,23 +539,23 @@ export class AzureDeployerService implements IGBInstallationDeployer {
return;
}

setTimeout(async () => {
try {
//tslint:disable-next-line:max-line-length
query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/Microsoft.BotService/botServices/${botId}/channels/WebChatChannel/listChannelWithKeys?api-version=${
this.apiVersion
}`;
url = urlJoin(baseUrl, query);
req = AzureDeployerService.createRequestObject(url, accessToken, 'POST', JSON.stringify(parameters));
const resChannel = await httpClient.sendRequest(req);
const key = JSON.parse(resChannel.bodyAsText).properties.properties.sites[0].key;
instance.webchatKey = key;
instance.whatsappBotKey = key;
resolve(instance);
} catch (error) {
reject(error);
}
}, 60000);

try {
//tslint:disable-next-line:max-line-length
query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/Microsoft.BotService/botServices/${botId}/channels/WebChatChannel/listChannelWithKeys?api-version=${
this.apiVersion
}`;
url = urlJoin(baseUrl, query);
req = AzureDeployerService.createRequestObject(url, accessToken, 'POST', JSON.stringify(parameters));
const resChannel = await httpClient.sendRequest(req);
const key = JSON.parse(resChannel.bodyAsText).properties.properties.sites[0].key;
instance.webchatKey = key;
instance.whatsappBotKey = key;
resolve(instance);
} catch (error) {
reject(error);
}

});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export class GBConversationalService {

public async routeNLP(step: GBDialogStep, min: GBMinInstance, text: string): Promise<boolean> {

if (min.instance.nlpAppId === null) {
if (min.instance.nlpAppId === null || min.instance.nlpAppId === undefined) {
return false;
}

Expand Down
3 changes: 0 additions & 3 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export class GBDeployer implements IGBDeployer {
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;

Expand Down Expand Up @@ -215,14 +214,12 @@ export class GBDeployer implements IGBDeployer {
instance.searchIndexer = bootInstance.searchIndexer;
instance.searchKey = bootInstance.searchKey;
instance.whatsappServiceKey = bootInstance.whatsappServiceKey;
instance.whatsappBotKey = bootInstance.whatsappBotKey;
instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber;
instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl;
instance.storageServer = bootInstance.storageServer;
instance.storageName = bootInstance.storageName;
instance.storageUsername = bootInstance.storageUsername;
instance.storagePassword = bootInstance.storagePassword;
instance.webchatKey = bootInstance.webchatKey;
instance.cloudLocation = bootInstance.cloudLocation;
instance.speechEndpoint = bootInstance.speechEndpoint;
instance.speechKey = bootInstance.speechKey;
Expand Down
84 changes: 41 additions & 43 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export class GBMinService {
min.adminService = this.adminService;
min.deployService = this.deployer;
min.kbService = new KBService(this.core.sequelize);
min.instance = await this.core.loadInstanceByBotId(min.botId);
min.instance = instance;
min.cbMap = {};
min.scriptMap = {};
min.sandBoxMap = {};
Expand All @@ -487,7 +487,7 @@ export class GBMinService {
new WhatsappDirectLine(
min,
min.botId,
min.instance.webchatKey,
min.instance.whatsappBotKey,
minBoot.instance.whatsappServiceKey,
minBoot.instance.whatsappServiceNumber,
minBoot.instance.whatsappServiceUrl
Expand Down Expand Up @@ -690,54 +690,52 @@ export class GBMinService {
await step.beginDialog('/menu', JSON.parse(context.activity.text));
// Otherwise, continue to the active dialog in the stack.
} else {
if (!await this.deployer.getStoragePackageByName(min.instance.instanceId, `${min.instance.botId}.gbkb`)) {
await step.context.sendActivity(`Oi, ainda não possuo pacotes de conhecimento publicados. Por favor, aguarde alguns segundos enquanto eu auto-publico alguns pacotes.`);
return await step.beginDialog('/publish', { confirm: true });
}

if (step.activeDialog !== undefined) {
await step.continueDialog();
} else {

if (!await this.deployer.getStoragePackageByName(min.instance.instanceId, `${min.instance.botId}.gbkb`)) {
await step.context.sendActivity(`Oi, ainda não possuo pacotes de conhecimento publicados. Por favor, aguarde alguns segundos enquanto eu auto-publico alguns pacotes.`);
return await step.beginDialog('/publish', { confirm: true });
}
else {

let query = context.activity.text;
let query = context.activity.text;

const translatorEnabled = () => {
if (min.instance.params) {
const params = JSON.parse(min.instance.params);
return params?params['Enable Worldwide Translator'] === "TRUE": false;
}
return false;
} // TODO: Encapsulate.

let locale = 'pt';
if (process.env.TRANSLATOR_DISABLED !== "true" || translatorEnabled()) {
const minBoot = GBServer.globals.minBoot as any; // TODO: Switch keys automatically to master/per bot.
locale = await AzureText.getLocale(minBoot.instance.textAnalyticsKey ?
minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey,
minBoot.instance.textAnalyticsEndpoint ?
minBoot.instance.textAnalyticsEndpoint : minBoot.instance.textAnalyticsEndpoint, query);
const translatorEnabled = () => {
if (min.instance.params) {
const params = JSON.parse(min.instance.params);
return params ? params['Enable Worldwide Translator'] === "TRUE" : false;
}

let sec = new SecService();
const member = step.context.activity.from;

const user = await sec.ensureUser(min.instance.instanceId, member.id,
member.name, "", "web", member.name);
user.locale = locale;
await user.save();
const minBoot = GBServer.globals.minBoot as any;
query = await min.conversationalService.translate(min,
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
query,
'pt');
GBLog.info(`Translated text: ${query}.`)

await step.beginDialog('/answer', {
query: query
});
return false;
} // TODO: Encapsulate.

let locale = 'pt';
if (process.env.TRANSLATOR_DISABLED !== "true" || translatorEnabled()) {
const minBoot = GBServer.globals.minBoot as any; // TODO: Switch keys automatically to master/per bot.
locale = await AzureText.getLocale(minBoot.instance.textAnalyticsKey ?
minBoot.instance.textAnalyticsKey : minBoot.instance.textAnalyticsKey,
minBoot.instance.textAnalyticsEndpoint ?
minBoot.instance.textAnalyticsEndpoint : minBoot.instance.textAnalyticsEndpoint, query);
}

let sec = new SecService();
const member = step.context.activity.from;

const user = await sec.ensureUser(min.instance.instanceId, member.id,
member.name, "", "web", member.name);
user.locale = locale;
await user.save();
const minBoot = GBServer.globals.minBoot as any;
query = await min.conversationalService.translate(min,
min.instance.translatorKey ? min.instance.translatorKey : minBoot.instance.translatorKey,
min.instance.translatorEndpoint ? min.instance.translatorEndpoint : minBoot.instance.translatorEndpoint,
query,
'pt');
GBLog.info(`Translated text: ${query}.`)

await step.beginDialog('/answer', {
query: query
});
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class WhatsappDirectLine extends GBService {
private directLineSecret: string;
private locale: string = 'pt-BR';

public conversationIds = {};
static conversationIds = {};
min: GBMinInstance;

constructor(
Expand Down Expand Up @@ -130,7 +130,7 @@ export class WhatsappDirectLine extends GBService {
}

public async resetConversationId(number) {
this.conversationIds[number] = undefined;
WhatsappDirectLine.conversationIds[number] = undefined;
}

public async check() {
Expand Down Expand Up @@ -200,7 +200,7 @@ export class WhatsappDirectLine extends GBService {
}
}

const conversationId = this.conversationIds[from];
const conversationId = WhatsappDirectLine.conversationIds[from];

let client = await this.directLineClient;
if (user.agentMode === "self") {
Expand Down Expand Up @@ -254,12 +254,12 @@ export class WhatsappDirectLine extends GBService {
}
else if (user.agentMode === "bot" || user.agentMode === null) {

if (this.conversationIds[from] === undefined) {
if (WhatsappDirectLine.conversationIds[from] === undefined) {
GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`);
const response = await client.Conversations.Conversations_StartConversation()
const response = await client.Conversations.Conversations_StartConversation();
const generatedConversationId = response.obj.conversationId;

this.conversationIds[from] = generatedConversationId;
WhatsappDirectLine.conversationIds[from] = generatedConversationId;

this.pollMessages(client, generatedConversationId, from, fromName);
this.inputMessage(client, generatedConversationId, text, from, fromName);
Expand Down

0 comments on commit 8c68332

Please sign in to comment.