3030import com .microsoft .bot .schema .AadResourceUrls ;
3131import com .microsoft .bot .schema .Activity ;
3232import com .microsoft .bot .schema .ActivityTypes ;
33+ import com .microsoft .bot .schema .CallerIdConstants ;
3334import com .microsoft .bot .schema .ChannelAccount ;
3435import com .microsoft .bot .schema .ConversationAccount ;
3536import com .microsoft .bot .schema .ConversationParameters ;
@@ -297,10 +298,8 @@ public CompletableFuture<Void> continueConversation(
297298
298299 CompletableFuture <Void > pipelineResult = new CompletableFuture <>();
299300
300- try (TurnContextImpl context = new TurnContextImpl (
301- this ,
302- reference .getContinuationActivity ()
303- )) {
301+ try (TurnContextImpl context =
302+ new TurnContextImpl (this , reference .getContinuationActivity ())) {
304303 // Hand craft Claims Identity.
305304 HashMap <String , String > claims = new HashMap <String , String >() {
306305 {
@@ -388,6 +387,7 @@ public CompletableFuture<InvokeResponse> processActivity(
388387 CompletableFuture <InvokeResponse > pipelineResult = new CompletableFuture <>();
389388
390389 try (TurnContextImpl context = new TurnContextImpl (this , activity )) {
390+ activity .setCallerId (generateCallerId (identity ).join ());
391391 context .getTurnState ().add (BOT_IDENTITY_KEY , identity );
392392
393393 pipelineResult = createConnectorClient (activity .getServiceUrl (), identity )
@@ -425,6 +425,32 @@ public CompletableFuture<InvokeResponse> processActivity(
425425 return pipelineResult ;
426426 }
427427
428+ @ SuppressWarnings ({"PMD" })
429+ private CompletableFuture <String > generateCallerId (ClaimsIdentity claimsIdentity ) {
430+ return credentialProvider .isAuthenticationDisabled ()
431+ .thenApply (
432+ is_auth_enabled -> {
433+ // Is the bot accepting all incoming messages?
434+ if (!is_auth_enabled ) {
435+ return null ;
436+ }
437+
438+ // Is the activity from Public Azure?
439+ if (channelProvider == null || channelProvider .isPublicAzure ()) {
440+ return CallerIdConstants .PUBLIC_AZURE_CHANNEL ;
441+ }
442+
443+ // Is the activity from Azure Gov?
444+ if (channelProvider != null && channelProvider .isGovernment ()) {
445+ return CallerIdConstants .US_GOV_CHANNEL ;
446+ }
447+
448+ // Return null so that the callerId is cleared.
449+ return null ;
450+ }
451+ );
452+ }
453+
428454 /**
429455 * Sends activities to the conversation.
430456 *
@@ -492,15 +518,14 @@ public CompletableFuture<ResourceResponse[]> sendActivities(
492518 // if it is a Trace activity we only send to the channel if it's the emulator.
493519 response = null ;
494520 } else if (!StringUtils .isEmpty (activity .getReplyToId ())) {
495- ConnectorClient connectorClient = context . getTurnState ()
496- .get (CONNECTOR_CLIENT_KEY );
521+ ConnectorClient connectorClient =
522+ context . getTurnState () .get (CONNECTOR_CLIENT_KEY );
497523 response = connectorClient .getConversations ().replyToActivity (activity ).join ();
498524 } else {
499- ConnectorClient connectorClient = context .getTurnState ()
500- .get (CONNECTOR_CLIENT_KEY );
501- response = connectorClient .getConversations ()
502- .sendToConversation (activity )
503- .join ();
525+ ConnectorClient connectorClient =
526+ context .getTurnState ().get (CONNECTOR_CLIENT_KEY );
527+ response =
528+ connectorClient .getConversations ().sendToConversation (activity ).join ();
504529 }
505530
506531 // If No response is set, then default to a "simple" response. This can't really
@@ -518,9 +543,8 @@ public CompletableFuture<ResourceResponse[]> sendActivities(
518543 // https://github.com/Microsoft/botbuilder-dotnet/issues/460
519544 // bug report : https://github.com/Microsoft/botbuilder-dotnet/issues/465
520545 if (response == null ) {
521- response = new ResourceResponse (
522- (activity .getId () == null ) ? "" : activity .getId ()
523- );
546+ response =
547+ new ResourceResponse ((activity .getId () == null ) ? "" : activity .getId ());
524548 }
525549
526550 responses [index ] = response ;
@@ -1314,8 +1338,8 @@ protected CompletableFuture<ConnectorClient> getOrCreateConnectorClient(
13141338 usingAppCredentials
13151339 );
13161340 } else {
1317- AppCredentials emptyCredentials = channelProvider != null
1318- && channelProvider .isGovernment ()
1341+ AppCredentials emptyCredentials =
1342+ channelProvider != null && channelProvider .isGovernment ()
13191343 ? MicrosoftGovernmentAppCredentials .empty ()
13201344 : MicrosoftAppCredentials .empty ();
13211345 connectorClient = new RestConnectorClient (
@@ -1395,8 +1419,8 @@ public CompletableFuture<Void> onTurn(TurnContext turnContext, NextDelegate next
13951419 && StringUtils .isEmpty (turnContext .getActivity ().getConversation ().getTenantId ())
13961420 ) {
13971421
1398- JsonNode teamsChannelData = new ObjectMapper ()
1399- .valueToTree (turnContext .getActivity ().getChannelData ());
1422+ JsonNode teamsChannelData =
1423+ new ObjectMapper () .valueToTree (turnContext .getActivity ().getChannelData ());
14001424 if (
14011425 teamsChannelData != null && teamsChannelData .has ("tenant" )
14021426 && teamsChannelData .get ("tenant" ).has ("id" )
0 commit comments