This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
/
testSkillHandler.ts
280 lines (253 loc) · 15 KB
/
testSkillHandler.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Activity, ResourceResponse, SkillConversationReference, TurnContext, BotFrameworkAdapter, BotAdapter, SkillConversationIdFactoryBase, ChannelServiceHandler, ActivityHandlerBase, CallerIdConstants, ActivityTypes } from "botbuilder";
import { ClaimsIdentity, JwtTokenValidation, GovernmentConstants, AuthenticationConstants, AppCredentials, ICredentialProvider, AuthenticationConfiguration } from "botframework-connector";
import { SkillConversationReferenceKey } from 'botbuilder-core';
export class TestSkillHandler extends ChannelServiceHandler {
/**
* Used to access the CovnersationReference sent from the Skill to the Parent.
* @remarks
* The value is the same as the SkillConversationReferenceKey exported from botbuilder-core.
*/
public readonly SkillConversationReferenceKey: symbol = SkillConversationReferenceKey;
private readonly adapter: BotAdapter;
private readonly bot: ActivityHandlerBase;
private readonly conversationIdFactory: SkillConversationIdFactoryBase;
/**
* Initializes a new instance of the SkillHandler class.
* @param adapter An instance of the BotAdapter that will handle the request.
* @param bot The ActivityHandlerBase instance.
* @param conversationIdFactory A SkillConversationIdFactoryBase to unpack the conversation ID and map it to the calling bot.
* @param credentialProvider The credential provider.
* @param authConfig The authentication configuration.
* @param channelService The string indicating if the bot is working in Public Azure or in Azure Government (https://aka.ms/AzureGovDocs).
*/
public constructor(
adapter: BotAdapter,
bot: ActivityHandlerBase,
conversationIdFactory: SkillConversationIdFactoryBase,
credentialProvider: ICredentialProvider,
authConfig: AuthenticationConfiguration,
channelService?: string
) {
super(credentialProvider, authConfig, channelService);
if (!adapter) {
throw new Error('missing adapter.');
}
if (!conversationIdFactory) {
throw new Error('missing conversationIdFactory.');
}
this.adapter = adapter;
this.bot = bot;
this.conversationIdFactory = conversationIdFactory;
}
/**
* sendToConversation() API for Skill.
* @remarks
* This method allows you to send an activity to the end of a conversation.
*
* This is slightly different from replyToActivity().
* * sendToConversation(conversationId) - will append the activity to the end
* of the conversation according to the timestamp or semantics of the channel.
* * replyToActivity(conversationId,ActivityId) - adds the activity as a reply
* to another activity, if the channel supports it. If the channel does not
* support nested replies, replyToActivity falls back to sendToConversation.
*
* Use replyToActivity when replying to a specific activity in the conversation.
*
* Use sendToConversation in all other cases.
* @param claimsIdentity ClaimsIdentity for the bot, should have AudienceClaim, AppIdClaim and ServiceUrlClaim.
* @param conversationId Conversation ID.
* @param activity Activity to send.
* @returns A Promise with a ResourceResponse.
*/
protected async onSendToConversation(claimsIdentity: ClaimsIdentity, conversationId: string, activity: Activity): Promise<ResourceResponse> {
return await this.processActivity(claimsIdentity, conversationId, '', activity);
}
/**
* replyToActivity() API for Skill.
* @remarks
* This method allows you to reply to an activity.
*
* This is slightly different from sendToConversation().
* * sendToConversation(conversationId) - will append the activity to the end
* of the conversation according to the timestamp or semantics of the channel.
* * replyToActivity(conversationId,ActivityId) - adds the activity as a reply
* to another activity, if the channel supports it. If the channel does not
* support nested replies, replyToActivity falls back to sendToConversation.
*
* Use replyToActivity when replying to a specific activity in the conversation.
*
* Use sendToConversation in all other cases.
* @param claimsIdentity ClaimsIdentity for the bot, should have AudienceClaim, AppIdClaim and ServiceUrlClaim.
* @param conversationId Conversation ID.
* @param activityId activityId the reply is to.
* @param activity Activity to send.
* @returns A Promise with a ResourceResponse.
*/
protected async onReplyToActivity(claimsIdentity: ClaimsIdentity, conversationId: string, activityId: string, activity: Activity): Promise<ResourceResponse> {
return await this.processActivity(claimsIdentity, conversationId, activityId, activity);
}
private static applyEoCToTurnContextActivity(turnContext: TurnContext,endOfConversationActivity: Activity): void {
// transform the turnContext.activity to be an EndOfConversation Activity.
turnContext.activity.type = endOfConversationActivity.type;
turnContext.activity.text = endOfConversationActivity.text;
turnContext.activity.code = endOfConversationActivity.code;
turnContext.activity.replyToId = endOfConversationActivity.replyToId;
turnContext.activity.value = endOfConversationActivity.value;
turnContext.activity.entities = endOfConversationActivity.entities;
turnContext.activity.locale = endOfConversationActivity.locale;
turnContext.activity.localTimestamp = endOfConversationActivity.localTimestamp;
turnContext.activity.timestamp = endOfConversationActivity.timestamp;
turnContext.activity.channelData = endOfConversationActivity.channelData;
}
private static applyEventToTurnContextActivity(turnContext: TurnContext, eventActivity: Activity): void {
// transform the turnContext.activity to be an Event Activity.
turnContext.activity.type = eventActivity.type;
turnContext.activity.name = eventActivity.name;
turnContext.activity.value = eventActivity.value;
turnContext.activity.relatesTo = eventActivity.relatesTo;
turnContext.activity.replyToId = eventActivity.replyToId;
turnContext.activity.value = eventActivity.value;
turnContext.activity.entities = eventActivity.entities;
turnContext.activity.locale = eventActivity.locale;
turnContext.activity.localTimestamp = eventActivity.localTimestamp;
turnContext.activity.timestamp = eventActivity.timestamp;
turnContext.activity.channelData = eventActivity.channelData;
}
private async processActivity(claimsIdentity: ClaimsIdentity, conversationId: string, replyToActivityId: string, activity: Activity): Promise<ResourceResponse> {
let skillConversationReference: SkillConversationReference;
try {
skillConversationReference = await this.conversationIdFactory.getSkillConversationReference(conversationId);
} catch (err) {
// If the factory has overridden getSkillConversationReference, call the deprecated getConversationReference().
// In this scenario, the oAuthScope paired with the ConversationReference can only be used for talking with
// an official channel, not another bot.
if (err.message === 'Not Implemented') {
const conversationReference = await this.conversationIdFactory.getConversationReference(conversationId);
skillConversationReference = {
conversationReference,
oAuthScope: JwtTokenValidation.isGovernment(this.channelService) ?
GovernmentConstants.ToChannelFromBotOAuthScope :
AuthenticationConstants.ToChannelFromBotOAuthScope
};
} else {
// Re-throw all other errors.
throw err;
}
}
if (!skillConversationReference) {
throw new Error('skillConversationReference not found');
}
if (!skillConversationReference.conversationReference) {
throw new Error('conversationReference not found.');
}
/**
* Callback passed to the BotFrameworkAdapter.createConversation() call.
* This function does the following:
* - Caches the ClaimsIdentity on the TurnContext.turnState
* - Applies the correct ConversationReference to the Activity for sending to the user-router conversation.
* - For EndOfConversation Activities received from the Skill, removes the ConversationReference from the
* ConversationIdFactory
*/
const callback = async (context: TurnContext): Promise<void> => {
const adapter: BotFrameworkAdapter = (context.adapter as BotFrameworkAdapter);
// Cache the ClaimsIdentity and ConnectorClient on the context so that it's available inside of the bot's logic.
context.turnState.set(adapter.BotIdentityKey, claimsIdentity);
context.turnState.set(this.SkillConversationReferenceKey, skillConversationReference);
activity = TurnContext.applyConversationReference(activity, skillConversationReference.conversationReference) as Activity;
const client = adapter.createConnectorClient(activity.serviceUrl);
context.turnState.set(adapter.ConnectorClientKey, client);
context.activity.id = replyToActivityId;
context.activity.callerId = `${ CallerIdConstants.BotToBotPrefix }${ JwtTokenValidation.getAppIdFromClaims(claimsIdentity.claims) }`;
switch (activity.type) {
case ActivityTypes.EndOfConversation:
await this.conversationIdFactory.deleteConversationReference(conversationId);
TestSkillHandler.applyEoCToTurnContextActivity(context, activity);
await this.bot.run(context);
break;
case ActivityTypes.Event:
TestSkillHandler.applyEventToTurnContextActivity(context, activity);
await this.bot.run(context);
break;
default:
await context.sendActivity(activity);
break;
}
};
// Add the channel service URL to the trusted services list so we can send messages back.
// the service URL for skills is trusted because it is applied based on the original request
// received by the root bot.
AppCredentials.trustServiceUrl(skillConversationReference.conversationReference.serviceUrl);
await (this.adapter as BotFrameworkAdapter).continueConversation(skillConversationReference.conversationReference, skillConversationReference.oAuthScope, callback);
return { id: uuid() };
}
protected async onUpdateActivity(claimsIdentity: ClaimsIdentity, conversationId: string, activityId: string, activity: Activity): Promise<ResourceResponse> {
return await this.updateActivity(claimsIdentity, conversationId, activityId, activity);
}
private async updateActivity(claimsIdentity: ClaimsIdentity, conversationId: string, replyToActivityId: string, activity: Activity): Promise<ResourceResponse> {
let skillConversationReference: SkillConversationReference;
try {
skillConversationReference = await this.conversationIdFactory.getSkillConversationReference(conversationId);
} catch (err) {
// If the factory has overridden getSkillConversationReference, call the deprecated getConversationReference().
// In this scenario, the oAuthScope paired with the ConversationReference can only be used for talking with
// an official channel, not another bot.
if (err.message === 'Not Implemented') {
const conversationReference = await this.conversationIdFactory.getConversationReference(conversationId);
skillConversationReference = {
conversationReference,
oAuthScope: JwtTokenValidation.isGovernment(this.channelService) ?
GovernmentConstants.ToChannelFromBotOAuthScope :
AuthenticationConstants.ToChannelFromBotOAuthScope
};
} else {
// Re-throw all other errors.
throw err;
}
}
if (!skillConversationReference) {
throw new Error('skillConversationReference not found');
}
if (!skillConversationReference.conversationReference) {
throw new Error('conversationReference not found.');
}
const activityConversationReference = TurnContext.getConversationReference(activity);
/**
* Callback passed to the BotFrameworkAdapter.createConversation() call.
* This function does the following:
* - Caches the ClaimsIdentity on the TurnContext.turnState
* - Applies the correct ConversationReference to the Activity for sending to the user-router conversation.
* - For EndOfConversation Activities received from the Skill, removes the ConversationReference from the
* ConversationIdFactory
*/
const callback = async (context: TurnContext): Promise<void> => {
const adapter: BotFrameworkAdapter = (context.adapter as BotFrameworkAdapter);
// Cache the ClaimsIdentity and ConnectorClient on the context so that it's available inside of the bot's logic.
context.turnState.set(adapter.BotIdentityKey, claimsIdentity);
context.turnState.set(this.SkillConversationReferenceKey, activityConversationReference);
activity = TurnContext.applyConversationReference(activity, skillConversationReference.conversationReference) as Activity;
const client = adapter.createConnectorClient(activity.serviceUrl);
context.turnState.set(adapter.ConnectorClientKey, client);
context.activity.id = replyToActivityId;
await context.updateActivity(context.activity);
return;
};
// Add the channel service URL to the trusted services list so we can send messages back.
// the service URL for skills is trusted because it is applied based on the original request
// received by the root bot.
AppCredentials.trustServiceUrl(skillConversationReference.conversationReference.serviceUrl);
await (this.adapter as BotFrameworkAdapter).continueConversation(skillConversationReference.conversationReference, skillConversationReference.oAuthScope, callback);
return { id: uuid() };
}
}
// Helper function to generate an UUID.
// Code is from @stevenic: https://github.com/stevenic
function uuid(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c): string => {
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}