This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
/
skillDialogBase.ts
131 lines (114 loc) · 4.87 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
/**
* Copyright(c) Microsoft Corporation.All rights reserved.
* Licensed under the MIT License.
*/
import {
Activity,
ActivityTypes,
StatePropertyAccessor } from 'botbuilder';
import {
ComponentDialog,
DialogTurnResult,
DialogTurnStatus,
PromptValidatorContext,
WaterfallStepContext} from 'botbuilder-dialogs';
import {
CommonUtil,
IProviderTokenResponse,
MultiProviderAuthDialog,
LocaleTemplateManager } from 'bot-solutions';
import { TokenResponse } from 'botframework-schema';
import { SkillState } from '../models';
import { BotServices} from '../services/botServices';
import { IBotSettings } from '../services/botSettings';
export class SkillDialogBase extends ComponentDialog {
protected settings: Partial<IBotSettings>;
protected services: BotServices;
protected stateAccessor: StatePropertyAccessor<SkillState>;
protected templateEngine: LocaleTemplateManager;
public constructor(
dialogId: string,
settings: Partial<IBotSettings>,
services: BotServices,
stateAccessor: StatePropertyAccessor<SkillState>,
templateEngine: LocaleTemplateManager
) {
super(dialogId);
this.services = services;
this.stateAccessor = stateAccessor;
this.settings = settings;
this.templateEngine = templateEngine;
// NOTE: Uncomment the following if your skill requires authentication
// if (!settings.oauthConnections || settings.oauthConnections.length === 0)
// {
// throw new Error('You must configure an authentication connection in your bot file before using this component.');
// }
// this.addDialog(new MultiProviderAuthDialog(settings.oauthConnections, this.settings.defaultLocale as string, []));
}
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
const state: SkillState = await this.stateAccessor.get(sc.context, new SkillState());
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);
}
}
// 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.templateEngine.generateActivityForLocale('ErrorMessage', sc.context.activity.locale));
// clear state
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const state: SkillState = await this.stateAccessor.get(sc.context, new SkillState());
state.clear();
}
}