This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
/
_skillDialogBase.ts
174 lines (152 loc) · 6.71 KB
/
_skillDialogBase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright(c) Microsoft Corporation.All rights reserved.
* Licensed under the MIT License.
*/
import {
Activity,
ActivityTypes,
BotTelemetryClient,
RecognizerResult,
StatePropertyAccessor } from 'botbuilder';
import { LuisRecognizerTelemetryClient } from 'botbuilder-ai';
import {
ComponentDialog,
DialogContext,
DialogTurnResult,
DialogTurnStatus,
PromptValidatorContext,
WaterfallStepContext} from 'botbuilder-dialogs';
import {
CommonUtil,
ICognitiveModelSet,
IProviderTokenResponse,
MultiProviderAuthDialog,
ResponseManager } from 'botbuilder-solutions';
import { TokenResponse } from 'botframework-schema';
import { SkillState } from '../models/skillState';
import { SharedResponses } from '../responses/shared/sharedResponses';
import { BotServices} from '../services/botServices';
import { IBotSettings } from '../services/botSettings';
export class SkillDialogBase extends ComponentDialog {
private readonly solutionName: string = '<%=skillNameCamelCase%>';
protected settings: Partial<IBotSettings>;
protected services: BotServices;
protected stateAccessor: StatePropertyAccessor<SkillState>;
protected responseManager: ResponseManager;
public constructor(
dialogId: string,
settings: Partial<IBotSettings>,
services: BotServices,
responseManager: ResponseManager,
stateAccessor: StatePropertyAccessor<SkillState>,
telemetryClient: BotTelemetryClient
) {
super(dialogId);
this.services = services;
this.responseManager = responseManager;
this.stateAccessor = stateAccessor;
this.telemetryClient = telemetryClient;
this.settings = settings;
// NOTE: Uncomment the following if your skill requires authentication
// if (!services.authenticationConnections.any())
// {
// throw new Error("You must configure an authentication connection in your bot file before using this component.");
// }
//
// this.addDialog(new EventPrompt(DialogIds.skillModeAuth, "tokens/response", tokenResponseValidator));
// this.addDialog(new MultiProviderAuthDialog(services));
}
protected async onBeginDialog(dc: DialogContext, options?: Object): Promise<DialogTurnResult> {
await this.getLuisResult(dc);
return super.onBeginDialog(dc, options);
}
protected async onContinueDialog(dc: DialogContext): Promise<DialogTurnResult> {
await this.getLuisResult(dc);
return super.onContinueDialog(dc);
}
protected async getAuthToken(sc: WaterfallStepContext): Promise<DialogTurnResult> {
try {
return await sc.prompt(MultiProviderAuthDialog.name, {});
} catch (err) {
await this.handleDialogExceptions(sc, err as Error);
return {
status: DialogTurnStatus.cancelled,
result: CommonUtil.dialogTurnResultCancelAllDialogs
};
}
}
protected async afterGetAuthToken(sc: WaterfallStepContext): Promise<DialogTurnResult> {
try {
// When the user authenticates interactively we pass on the tokens/Response event which surfaces as a JObject
// When the token is cached we get a TokenResponse object.
const providerTokenResponse: IProviderTokenResponse | undefined = sc.result as IProviderTokenResponse;
if (providerTokenResponse !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/tslint/config
const state: any = await this.stateAccessor.get(sc.context);
// tslint:disable-next-line: no-any no-unsafe-any
state.token = providerTokenResponse.tokenResponse.token;
}
return await sc.next();
} catch (err) {
await this.handleDialogExceptions(sc, err as Error);
return {
status: DialogTurnStatus.cancelled,
result: CommonUtil.dialogTurnResultCancelAllDialogs
};
}
}
// Validators
protected async tokenResponseValidator(pc: PromptValidatorContext<Activity>): Promise<boolean> {
const activity: Activity | undefined = pc.recognized.value;
if (activity !== undefined && activity.type === ActivityTypes.Event) {
return Promise.resolve(true);
} else {
return Promise.resolve(false);
}
}
protected async authPromptValidator(promptContext: PromptValidatorContext<TokenResponse>): Promise<boolean> {
const token: TokenResponse | undefined = promptContext.recognized.value;
if (token !== undefined) {
return Promise.resolve(true);
} else {
return Promise.resolve(false);
}
}
// Helpers
protected async getLuisResult(dc: DialogContext): Promise<void> {
if (dc.context.activity.type === ActivityTypes.Message) {
const state: SkillState = await this.stateAccessor.get(dc.context, new SkillState());
const localeConfig: Partial<ICognitiveModelSet> | undefined = this.services.getCognitiveModel();
if (localeConfig.luisServices !== undefined) {
const luisService: LuisRecognizerTelemetryClient | undefined = localeConfig.luisServices.get(this.solutionName);
if (luisService === undefined) {
throw new Error('The specified LUIS Model could not be found in your Bot Services configuration.');
} else {
// Get intent and entities for activity
const result: RecognizerResult = await luisService.recognize(dc.context);
state.luisResult = result;
}
}
}
}
// This method is called by any waterfall step that throws an exception to ensure consistency
protected async handleDialogExceptions(sc: WaterfallStepContext, err: Error): Promise<void> {
// send trace back to emulator
const trace: Partial<Activity> = {
type: ActivityTypes.Trace,
text: `DialogException: ${err.message}, StackTrace: ${err.stack}`
};
await sc.context.sendActivity(trace);
// log exception
this.telemetryClient.trackException({
exception: err
});
// send error message to bot user
await sc.context.sendActivity(this.responseManager.getResponse(SharedResponses.errorMessage));
// clear state
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/tslint/config
const state: any = await this.stateAccessor.get(sc.context);
// tslint:disable-next-line: no-unsafe-any
state.clear();
}
}