Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 1a3af4c

Browse files
authored
Merge pull request #454 from microsoft/trboehre/callerid
CallerId is getting set on incoming Activity
2 parents f42fc99 + 980b5a9 commit 1a3af4c

File tree

2 files changed

+72
-18
lines changed

2 files changed

+72
-18
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.microsoft.bot.schema.AadResourceUrls;
3131
import com.microsoft.bot.schema.Activity;
3232
import com.microsoft.bot.schema.ActivityTypes;
33+
import com.microsoft.bot.schema.CallerIdConstants;
3334
import com.microsoft.bot.schema.ChannelAccount;
3435
import com.microsoft.bot.schema.ConversationAccount;
3536
import 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")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.bot.schema;
5+
6+
/**
7+
* Constants for CallerId values.
8+
*/
9+
public final class CallerIdConstants {
10+
private CallerIdConstants() {
11+
12+
}
13+
14+
/**
15+
* The caller ID for any Bot Framework channel.
16+
*/
17+
public static final String PUBLIC_AZURE_CHANNEL = "urn:botframework:azure";
18+
19+
/**
20+
* The caller ID for any Bot Framework US Government cloud channel.
21+
*/
22+
public static final String US_GOV_CHANNEL = "urn:botframework:azureusgov";
23+
24+
/**
25+
* The caller ID prefix when a bot initiates a request to another bot. This
26+
* prefix will be followed by the Azure Active Directory App ID of the bot that
27+
* initiated the call.
28+
*/
29+
public static final String BOT_TO_BOT_PREFIX = "urn:botframework:aadappid:";
30+
}

0 commit comments

Comments
 (0)