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

Commit

Permalink
Merge pull request #3752 from microsoft/jpg/localized-qna-dialogs
Browse files Browse the repository at this point in the history
[TypeScript][Virtual Assistant] fix: localize qna maker dialog IDs
  • Loading branch information
lauren-mills committed Feb 9, 2021
2 parents e0af29b + 2045ad2 commit 65df3be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 84 deletions.
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

0 comments on commit 65df3be

Please sign in to comment.