Skip to content

Commit a78a69f

Browse files
committed
allows Inspection to attach anywhere within a Teams Team
1 parent b24d205 commit a78a69f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

libraries/botbuilder/src/inspectionMiddleware.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { MicrosoftAppCredentials, ConnectorClient } from 'botframework-connector';
99
import { Activity, ActivityTypes, Middleware, TurnContext, BotState, ConversationReference, StatePropertyAccessor, UserState, ConversationState, Storage } from 'botbuilder-core';
10+
import { teamsGetTeamId } from './teamsActivityHelpers';
1011

1112
/** @private */
1213
class TraceActivity {
@@ -279,8 +280,7 @@ export class InspectionMiddleware extends InterceptionMiddleware {
279280

280281
private async processAttachCommand(turnContext: TurnContext, sessionId: string): Promise<any> {
281282
var sessions = await this.inspectionStateAccessor.get(turnContext, InspectionSessionsByStatus.DefaultValue);
282-
283-
if (this.attachCommand(turnContext.activity.conversation.id, sessions, sessionId)) {
283+
if (this.attachComamnd(this.getAttachId(turnContext.activity), sessions, sessionId)) {
284284
await turnContext.sendActivity('Attached to session, all traffic is being replicated for inspection.');
285285
}
286286
else {
@@ -306,22 +306,20 @@ export class InspectionMiddleware extends InterceptionMiddleware {
306306
return sessionId;
307307
}
308308

309-
private attachCommand(conversationId: string, sessions: InspectionSessionsByStatus, sessionId: string): boolean {
310-
309+
private attachComamnd(attachId: string, sessions: InspectionSessionsByStatus, sessionId: string): boolean {
311310
var inspectionSessionState = sessions.openedSessions[sessionId];
312311
if (inspectionSessionState !== undefined) {
313-
sessions.attachedSessions[conversationId] = inspectionSessionState;
312+
sessions.attachedSessions[attachId] = inspectionSessionState;
314313
delete sessions.openedSessions[sessionId];
315314
return true;
316315
}
317-
318316
return false;
319317
}
320318

321319
private async findSession(turnContext: TurnContext): Promise<any> {
322320
var sessions = await this.inspectionStateAccessor.get(turnContext, InspectionSessionsByStatus.DefaultValue);
323321

324-
var conversationReference = sessions.attachedSessions[turnContext.activity.conversation.id];
322+
var conversationReference = sessions.attachedSessions[this.getAttachId(turnContext.activity)];
325323
if (conversationReference !== undefined) {
326324
return new InspectionSession(conversationReference, this.credentials);
327325
}
@@ -342,9 +340,16 @@ export class InspectionMiddleware extends InterceptionMiddleware {
342340
private async cleanUpSession(turnContext: TurnContext): Promise<any> {
343341
var sessions = await this.inspectionStateAccessor.get(turnContext, InspectionSessionsByStatus.DefaultValue);
344342

345-
delete sessions.attachedSessions[turnContext.activity.conversation.id];
343+
delete sessions.attachedSessions[this.getAttachId(turnContext.activity)];
346344
await this.inspectionState.saveChanges(turnContext, false);
347345
}
346+
347+
private getAttachId(activity: Activity) : string {
348+
// If we are running in a Microsoft Teams Team the conversation Id will reflect a particular thread the bot is in.
349+
// So if we are in a Team then we will associate the "attach" with the Team Id rather than the more restrictive conversation Id.
350+
const teamId = teamsGetTeamId(activity);
351+
return teamId ? teamId : activity.conversation.id;
352+
}
348353
}
349354

350355
/** @private */

0 commit comments

Comments
 (0)