7
7
*/
8
8
import { MicrosoftAppCredentials , ConnectorClient } from 'botframework-connector' ;
9
9
import { Activity , ActivityTypes , Middleware , TurnContext , BotState , ConversationReference , StatePropertyAccessor , UserState , ConversationState , Storage } from 'botbuilder-core' ;
10
+ import { teamsGetTeamId } from './teamsActivityHelpers' ;
10
11
11
12
/** @private */
12
13
class TraceActivity {
@@ -279,8 +280,7 @@ export class InspectionMiddleware extends InterceptionMiddleware {
279
280
280
281
private async processAttachCommand ( turnContext : TurnContext , sessionId : string ) : Promise < any > {
281
282
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 ) ) {
284
284
await turnContext . sendActivity ( 'Attached to session, all traffic is being replicated for inspection.' ) ;
285
285
}
286
286
else {
@@ -306,22 +306,20 @@ export class InspectionMiddleware extends InterceptionMiddleware {
306
306
return sessionId ;
307
307
}
308
308
309
- private attachCommand ( conversationId : string , sessions : InspectionSessionsByStatus , sessionId : string ) : boolean {
310
-
309
+ private attachComamnd ( attachId : string , sessions : InspectionSessionsByStatus , sessionId : string ) : boolean {
311
310
var inspectionSessionState = sessions . openedSessions [ sessionId ] ;
312
311
if ( inspectionSessionState !== undefined ) {
313
- sessions . attachedSessions [ conversationId ] = inspectionSessionState ;
312
+ sessions . attachedSessions [ attachId ] = inspectionSessionState ;
314
313
delete sessions . openedSessions [ sessionId ] ;
315
314
return true ;
316
315
}
317
-
318
316
return false ;
319
317
}
320
318
321
319
private async findSession ( turnContext : TurnContext ) : Promise < any > {
322
320
var sessions = await this . inspectionStateAccessor . get ( turnContext , InspectionSessionsByStatus . DefaultValue ) ;
323
321
324
- var conversationReference = sessions . attachedSessions [ turnContext . activity . conversation . id ] ;
322
+ var conversationReference = sessions . attachedSessions [ this . getAttachId ( turnContext . activity ) ] ;
325
323
if ( conversationReference !== undefined ) {
326
324
return new InspectionSession ( conversationReference , this . credentials ) ;
327
325
}
@@ -342,9 +340,16 @@ export class InspectionMiddleware extends InterceptionMiddleware {
342
340
private async cleanUpSession ( turnContext : TurnContext ) : Promise < any > {
343
341
var sessions = await this . inspectionStateAccessor . get ( turnContext , InspectionSessionsByStatus . DefaultValue ) ;
344
342
345
- delete sessions . attachedSessions [ turnContext . activity . conversation . id ] ;
343
+ delete sessions . attachedSessions [ this . getAttachId ( turnContext . activity ) ] ;
346
344
await this . inspectionState . saveChanges ( turnContext , false ) ;
347
345
}
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
+ }
348
353
}
349
354
350
355
/** @private */
0 commit comments