Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

[TypeScript][Virtual Assistant] fix: localize qna maker dialog IDs #3752

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,26 @@ export class MainDialog extends ComponentDialog {

// Set up response caching for "repeat" functionality.
innerDc.context.onSendActivities(this.storeOutgoingActivities.bind(this));
if (innerDc.activeDialog?.id === this.faqDialogId) {
// user is in a mult turn FAQ dialog
const qnaDialog: QnAMakerDialog | undefined = this.tryCreateQnADialog(this.faqDialogId, localizedServices);
if (qnaDialog !== undefined) {

// Ensure that faq QnA dialog matching the locale of this dialogis registered
this.registerLocalizedQnADialog(this.faqDialogId, innerDc.context, localizedServices);

return await super.onContinueDialog(innerDc);
}

protected registerLocalizedQnADialog(knowledgebaseId: string, context: TurnContext, cognitiveModels: ICognitiveModelSet): string {
const localizedDialogId = [knowledgebaseId, context.activity.locale].filter(a => a).join('.');

let qnaDialog: Dialog = this.dialogs.find(localizedDialogId);
if (!qnaDialog) {
qnaDialog = this.tryCreateQnADialog(knowledgebaseId, cognitiveModels);
if (qnaDialog) {
qnaDialog.id = localizedDialogId;
this.dialogs.add(qnaDialog);
}
}

return await super.onContinueDialog(innerDc);
return localizedDialogId;
}

protected tryCreateQnADialog(knowledgebaseId: string, cognitiveModels: ICognitiveModelSet): QnAMakerDialog | undefined {
Expand Down Expand Up @@ -370,20 +381,14 @@ export class MainDialog extends ComponentDialog {
return await stepContext.beginDialog(dispatchIntentSkill, skillDialogArgs);
} else if (dispatchIntent === 'q_faq') {
DialogContextEx.suppressCompletionMessage(stepContext, true);

const knowledgebaseId: string = this.faqDialogId;
const qnaDialog: QnAMakerDialog | undefined = this.tryCreateQnADialog(knowledgebaseId, localizedServices);
if (qnaDialog !== undefined) {
this.dialogs.add(qnaDialog);
}

return await stepContext.beginDialog('faq');
const dialogId = this.registerLocalizedQnADialog(this.faqDialogId, stepContext.context, localizedServices);

return await stepContext.beginDialog(dialogId);
} else if (this.shouldBeginChitChatDialog(stepContext, dispatchIntent, dispatchScore)) {
DialogContextEx.suppressCompletionMessage(stepContext, true);
const knowledgebaseId = 'chitchat';
this.registerQnADialog(knowledgebaseId, localizedServices, stepContext.context.activity.locale as string);
const dialogId = this.registerLocalizedQnADialog('chitchat', stepContext.context, localizedServices)

return await stepContext.beginDialog(knowledgebaseId);
return await stepContext.beginDialog(dialogId);
} else {
DialogContextEx.suppressCompletionMessage(stepContext, true);
await stepContext.context.sendActivity(this.templateManager.generateActivityForLocale('UnsupportedMessage', userProfile));
Expand All @@ -404,32 +409,6 @@ export class MainDialog extends ComponentDialog {
return await stepContext.replaceDialog(this.initialDialogId, this.templateManager.generateActivityForLocale('CompletedMessage'));
}

private registerQnADialog(knowledgebaseId: string, cognitiveModels: ICognitiveModelSet, locale: string): void {
const qnaEndpoint: QnAMakerEndpoint | undefined = cognitiveModels.qnaConfiguration.get(knowledgebaseId);
if (qnaEndpoint == undefined){
throw new Error(`Could not find QnA Maker knowledge base configuration with id: ${ knowledgebaseId }.`);
}

if (this.dialogs.find(knowledgebaseId) == undefined) {
const qnaDialog: QnAMakerDialog = new QnAMakerDialog(
qnaEndpoint.knowledgeBaseId,
qnaEndpoint.endpointKey,
// The following line is a workaround until the method getQnAClient of QnAMakerDialog is fixed
// as per issue https://github.com/microsoft/botbuilder-js/issues/1885
new URL(qnaEndpoint.host).hostname.split('.')[0],
this.templateManager.generateActivityForLocale('UnsupportedMessage') as Activity,
// Before, instead of 'undefined' a '0.3' value was used in the following line
undefined,
this.templateManager.generateActivityForLocale('QnaMakerAdaptiveLearningCardTitle').text,
this.templateManager.generateActivityForLocale('QnaMakerNoMatchText').text
);

qnaDialog.id = knowledgebaseId;

this.addDialog(qnaDialog);
}
}

private async logUserOut(dc: DialogContext): Promise<void> {
const tokenProvider: BotFrameworkAdapter = dc.context.adapter as BotFrameworkAdapter;
if (tokenProvider !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,26 @@ export class MainDialog extends ComponentDialog {

// Set up response caching for "repeat" functionality.
innerDc.context.onSendActivities(this.storeOutgoingActivities.bind(this));
if (innerDc.activeDialog?.id === this.faqDialogId) {
// user is in a mult turn FAQ dialog
const qnaDialog: QnAMakerDialog | undefined = this.tryCreateQnADialog(this.faqDialogId, localizedServices);
if (qnaDialog !== undefined) {

// Ensure that faq QnA dialog matching the locale of this dialogis registered
this.registerLocalizedQnADialog(this.faqDialogId, innerDc.context, localizedServices);

return await super.onContinueDialog(innerDc);
}

protected registerLocalizedQnADialog(knowledgebaseId: string, context: TurnContext, cognitiveModels: ICognitiveModelSet): string {
const localizedDialogId = [knowledgebaseId, context.activity.locale].filter(a => a).join('.');

let qnaDialog: Dialog = this.dialogs.find(localizedDialogId);
if (!qnaDialog) {
qnaDialog = this.tryCreateQnADialog(knowledgebaseId, cognitiveModels);
if (qnaDialog) {
qnaDialog.id = localizedDialogId;
this.dialogs.add(qnaDialog);
}
}

return await super.onContinueDialog(innerDc);
return localizedDialogId;
}

protected tryCreateQnADialog(knowledgebaseId: string, cognitiveModels: ICognitiveModelSet): QnAMakerDialog | undefined {
Expand Down Expand Up @@ -370,20 +381,14 @@ export class MainDialog extends ComponentDialog {
return await stepContext.beginDialog(dispatchIntentSkill, skillDialogArgs);
} else if (dispatchIntent === 'q_faq') {
DialogContextEx.suppressCompletionMessage(stepContext, true);

const knowledgebaseId: string = this.faqDialogId;
const qnaDialog: QnAMakerDialog | undefined = this.tryCreateQnADialog(knowledgebaseId, localizedServices);
if (qnaDialog !== undefined) {
this.dialogs.add(qnaDialog);
}

return await stepContext.beginDialog('faq');
const dialogId = this.registerLocalizedQnADialog(this.faqDialogId, stepContext.context, localizedServices);

return await stepContext.beginDialog(dialogId);
} else if (this.shouldBeginChitChatDialog(stepContext, dispatchIntent, dispatchScore)) {
DialogContextEx.suppressCompletionMessage(stepContext, true);
const knowledgebaseId = 'chitchat';
this.registerQnADialog(knowledgebaseId, localizedServices, stepContext.context.activity.locale as string);
const dialogId = this.registerLocalizedQnADialog('chitchat', stepContext.context, localizedServices)

return await stepContext.beginDialog(knowledgebaseId);
return await stepContext.beginDialog(dialogId);
} else {
DialogContextEx.suppressCompletionMessage(stepContext, true);
await stepContext.context.sendActivity(this.templateManager.generateActivityForLocale('UnsupportedMessage', userProfile));
Expand All @@ -404,32 +409,6 @@ export class MainDialog extends ComponentDialog {
return await stepContext.replaceDialog(this.initialDialogId, this.templateManager.generateActivityForLocale('CompletedMessage'));
}

private registerQnADialog(knowledgebaseId: string, cognitiveModels: ICognitiveModelSet, locale: string): void {
const qnaEndpoint: QnAMakerEndpoint | undefined = cognitiveModels.qnaConfiguration.get(knowledgebaseId);
if (qnaEndpoint == undefined){
throw new Error(`Could not find QnA Maker knowledge base configuration with id: ${ knowledgebaseId }.`);
}

if (this.dialogs.find(knowledgebaseId) == undefined) {
const qnaDialog: QnAMakerDialog = new QnAMakerDialog(
qnaEndpoint.knowledgeBaseId,
qnaEndpoint.endpointKey,
// The following line is a workaround until the method getQnAClient of QnAMakerDialog is fixed
// as per issue https://github.com/microsoft/botbuilder-js/issues/1885
new URL(qnaEndpoint.host).hostname.split('.')[0],
this.templateManager.generateActivityForLocale('UnsupportedMessage') as Activity,
// Before, instead of 'undefined' a '0.3' value was used in the following line
undefined,
this.templateManager.generateActivityForLocale('QnaMakerAdaptiveLearningCardTitle').text,
this.templateManager.generateActivityForLocale('QnaMakerNoMatchText').text
);

qnaDialog.id = knowledgebaseId;

this.addDialog(qnaDialog);
}
}

private async logUserOut(dc: DialogContext): Promise<void> {
const tokenProvider: BotFrameworkAdapter = dc.context.adapter as BotFrameworkAdapter;
if (tokenProvider !== undefined) {
Expand Down