Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.microsoft.bot.schema.AadResourceUrls;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.ActivityTypes;
import com.microsoft.bot.schema.CallerIdConstants;
import com.microsoft.bot.schema.ChannelAccount;
import com.microsoft.bot.schema.ConversationAccount;
import com.microsoft.bot.schema.ConversationParameters;
Expand Down Expand Up @@ -297,10 +298,8 @@ public CompletableFuture<Void> continueConversation(

CompletableFuture<Void> pipelineResult = new CompletableFuture<>();

try (TurnContextImpl context = new TurnContextImpl(
this,
reference.getContinuationActivity()
)) {
try (TurnContextImpl context =
new TurnContextImpl(this, reference.getContinuationActivity())) {
// Hand craft Claims Identity.
HashMap<String, String> claims = new HashMap<String, String>() {
{
Expand Down Expand Up @@ -388,6 +387,7 @@ public CompletableFuture<InvokeResponse> processActivity(
CompletableFuture<InvokeResponse> pipelineResult = new CompletableFuture<>();

try (TurnContextImpl context = new TurnContextImpl(this, activity)) {
activity.setCallerId(generateCallerId(identity).join());
context.getTurnState().add(BOT_IDENTITY_KEY, identity);

pipelineResult = createConnectorClient(activity.getServiceUrl(), identity)
Expand Down Expand Up @@ -425,6 +425,32 @@ public CompletableFuture<InvokeResponse> processActivity(
return pipelineResult;
}

@SuppressWarnings({"PMD"})
private CompletableFuture<String> generateCallerId(ClaimsIdentity claimsIdentity) {
return credentialProvider.isAuthenticationDisabled()
.thenApply(
is_auth_enabled -> {
// Is the bot accepting all incoming messages?
if (!is_auth_enabled) {
return null;
}

// Is the activity from Public Azure?
if (channelProvider == null || channelProvider.isPublicAzure()) {
return CallerIdConstants.PUBLIC_AZURE_CHANNEL;
}

// Is the activity from Azure Gov?
if (channelProvider != null && channelProvider.isGovernment()) {
return CallerIdConstants.US_GOV_CHANNEL;
}

// Return null so that the callerId is cleared.
return null;
}
);
}

/**
* Sends activities to the conversation.
*
Expand Down Expand Up @@ -492,15 +518,14 @@ public CompletableFuture<ResourceResponse[]> sendActivities(
// if it is a Trace activity we only send to the channel if it's the emulator.
response = null;
} else if (!StringUtils.isEmpty(activity.getReplyToId())) {
ConnectorClient connectorClient = context.getTurnState()
.get(CONNECTOR_CLIENT_KEY);
ConnectorClient connectorClient =
context.getTurnState().get(CONNECTOR_CLIENT_KEY);
response = connectorClient.getConversations().replyToActivity(activity).join();
} else {
ConnectorClient connectorClient = context.getTurnState()
.get(CONNECTOR_CLIENT_KEY);
response = connectorClient.getConversations()
.sendToConversation(activity)
.join();
ConnectorClient connectorClient =
context.getTurnState().get(CONNECTOR_CLIENT_KEY);
response =
connectorClient.getConversations().sendToConversation(activity).join();
}

// If No response is set, then default to a "simple" response. This can't really
Expand All @@ -518,9 +543,8 @@ public CompletableFuture<ResourceResponse[]> sendActivities(
// https://github.com/Microsoft/botbuilder-dotnet/issues/460
// bug report : https://github.com/Microsoft/botbuilder-dotnet/issues/465
if (response == null) {
response = new ResourceResponse(
(activity.getId() == null) ? "" : activity.getId()
);
response =
new ResourceResponse((activity.getId() == null) ? "" : activity.getId());
}

responses[index] = response;
Expand Down Expand Up @@ -1314,8 +1338,8 @@ protected CompletableFuture<ConnectorClient> getOrCreateConnectorClient(
usingAppCredentials
);
} else {
AppCredentials emptyCredentials = channelProvider != null
&& channelProvider.isGovernment()
AppCredentials emptyCredentials =
channelProvider != null && channelProvider.isGovernment()
? MicrosoftGovernmentAppCredentials.empty()
: MicrosoftAppCredentials.empty();
connectorClient = new RestConnectorClient(
Expand Down Expand Up @@ -1395,8 +1419,8 @@ public CompletableFuture<Void> onTurn(TurnContext turnContext, NextDelegate next
&& StringUtils.isEmpty(turnContext.getActivity().getConversation().getTenantId())
) {

JsonNode teamsChannelData = new ObjectMapper()
.valueToTree(turnContext.getActivity().getChannelData());
JsonNode teamsChannelData =
new ObjectMapper().valueToTree(turnContext.getActivity().getChannelData());
if (
teamsChannelData != null && teamsChannelData.has("tenant")
&& teamsChannelData.get("tenant").has("id")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.bot.schema;

/**
* Constants for CallerId values.
*/
public final class CallerIdConstants {
private CallerIdConstants() {

}

/**
* The caller ID for any Bot Framework channel.
*/
public static final String PUBLIC_AZURE_CHANNEL = "urn:botframework:azure";

/**
* The caller ID for any Bot Framework US Government cloud channel.
*/
public static final String US_GOV_CHANNEL = "urn:botframework:azureusgov";

/**
* The caller ID prefix when a bot initiates a request to another bot. This
* prefix will be followed by the Azure Active Directory App ID of the bot that
* initiated the call.
*/
public static final String BOT_TO_BOT_PREFIX = "urn:botframework:aadappid:";
}