Skip to content

Updates to SkillHttpClient and BotFrameworkClient #4232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2020
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
6 changes: 5 additions & 1 deletion libraries/Microsoft.Bot.Builder/InvokeResponse{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class InvokeResponse<T> : InvokeResponse
/// The POST that is generated in response to the incoming invoke activity
/// will have a body generated by JSON serializing the object in this field.
/// </remarks>
public new T Body { get; set; }
public new T Body
{
get => (T)base.Body;
set => base.Body = value;
}
}
}
4 changes: 2 additions & 2 deletions libraries/Microsoft.Bot.Builder/Skills/BotFrameworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class BotFrameworkClient
/// <param name="toUrl">The URL of the bot receiving the activity.</param>
/// <param name="serviceUrl">The callback Url for the skill host.</param>
/// <param name="conversationId">A conversation ID to use for the conversation with the skill.</param>
/// <param name="activity">activity to forward.</param>
/// <param name="activity">The <see cref="Activity"/> to send to forward.</param>
/// <param name="cancellationToken">cancellation Token.</param>
/// <returns>Async task with optional invokeResponse.</returns>
public abstract Task<InvokeResponse> PostActivityAsync(string fromBotId, string toBotId, Uri toUrl, Uri serviceUrl, string conversationId, Activity activity, CancellationToken cancellationToken = default);
Expand All @@ -34,7 +34,7 @@ public abstract class BotFrameworkClient
/// <param name="toUrl">The URL of the bot receiving the activity.</param>
/// <param name="serviceUrl">The callback Url for the skill host.</param>
/// <param name="conversationId">A conversation ID to use for the conversation with the skill.</param>
/// <param name="activity">activity to forward.</param>
/// <param name="activity">The <see cref="Activity"/> to send to forward.</param>
/// <param name="cancellationToken">cancellation Token.</param>
/// <returns>Async task with optional invokeResponse.</returns>
public abstract Task<InvokeResponse<T>> PostActivityAsync<T>(string fromBotId, string toBotId, Uri toUrl, Uri serviceUrl, string conversationId, Activity activity, CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,55 +130,38 @@ public override async Task<InvokeResponse<T>> PostActivityAsync<T>(string fromBo
// Get token for the skill call
var token = appCredentials == MicrosoftAppCredentials.Empty ? null : await appCredentials.GetTokenAsync().ConfigureAwait(false);

// Capture current activity settings before changing them.
var originalConversationId = activity.Conversation.Id;
var originalServiceUrl = activity.ServiceUrl;
var originalRelatesTo = activity.RelatesTo;
var originalRecipient = activity.Recipient;
try
// Clone the activity so we can modify it before sending without impacting the original object.
var activityClone = JsonConvert.DeserializeObject<Activity>(JsonConvert.SerializeObject(activity));
activityClone.RelatesTo = new ConversationReference
{
activity.RelatesTo = new ConversationReference()
ServiceUrl = activityClone.ServiceUrl,
ActivityId = activityClone.Id,
ChannelId = activityClone.ChannelId,
Locale = activityClone.Locale,
Conversation = new ConversationAccount
{
ServiceUrl = activity.ServiceUrl,
ActivityId = activity.Id,
ChannelId = activity.ChannelId,
Conversation = new ConversationAccount()
{
Id = activity.Conversation.Id,
Name = activity.Conversation.Name,
ConversationType = activity.Conversation.ConversationType,
AadObjectId = activity.Conversation.AadObjectId,
IsGroup = activity.Conversation.IsGroup,
Properties = activity.Conversation.Properties,
Role = activity.Conversation.Role,
TenantId = activity.Conversation.TenantId,
},
Locale = activity.Locale
};
activity.Conversation.Id = conversationId;
activity.ServiceUrl = serviceUrl.ToString();
if (activity.Recipient == null)
{
activity.Recipient = new ChannelAccount();
Id = activityClone.Conversation.Id,
Name = activityClone.Conversation.Name,
ConversationType = activityClone.Conversation.ConversationType,
AadObjectId = activityClone.Conversation.AadObjectId,
IsGroup = activityClone.Conversation.IsGroup,
Properties = activityClone.Conversation.Properties,
Role = activityClone.Conversation.Role,
TenantId = activityClone.Conversation.TenantId,
}
};
activityClone.Conversation.Id = conversationId;
activityClone.ServiceUrl = serviceUrl.ToString();
activityClone.Recipient ??= new ChannelAccount();

return await SecurePostActivityAsync<T>(toUrl, activity, token, cancellationToken).ConfigureAwait(false);
}
finally
{
// Restore activity properties.
activity.Conversation.Id = originalConversationId;
activity.ServiceUrl = originalServiceUrl;
activity.RelatesTo = originalRelatesTo;
activity.Recipient = originalRecipient;
}
return await SecurePostActivityAsync<T>(toUrl, activityClone, token, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Post Activity to the bot using the bot's credentials.
/// </summary>
/// <param name="botId">botId.</param>
/// <param name="botEndpoint">botEndpoint.</param>
/// <param name="botId">The MicrosoftAppId of the bot.</param>
/// <param name="botEndpoint">The URL of the bot.</param>
/// <param name="activity">activity to post.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>InvokeResponse.</returns>
Expand All @@ -191,8 +174,8 @@ public virtual async Task<InvokeResponse> PostActivityAsync(string botId, Uri bo
/// Post Activity to the bot using the bot's credentials.
/// </summary>
/// <typeparam name="T">type of invokeResponse body.</typeparam>
/// <param name="botId">botId.</param>
/// <param name="botEndpoint">botEndpoint.</param>
/// <param name="botId">The MicrosoftAppId of the bot.</param>
/// <param name="botEndpoint">The URL of the bot.</param>
/// <param name="activity">activity to post.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>InvokeResponse<typeparamref name="T"/>.</returns>
Expand All @@ -206,7 +189,7 @@ public virtual async Task<InvokeResponse<T>> PostActivityAsync<T>(string botId,
}

// Get token for the bot to call itself
var token = await appCredentials.GetTokenAsync().ConfigureAwait(false);
var token = appCredentials == MicrosoftAppCredentials.Empty ? null : await appCredentials.GetTokenAsync().ConfigureAwait(false);

// post the activity to the url using the bot's credentials.
Logger.LogInformation($"Posting activity. ActivityId: {activity.Id} from BotId: {botId}");
Expand All @@ -225,7 +208,7 @@ protected virtual async Task<AppCredentials> BuildCredentialsAsync(string appId,
var appPassword = await CredentialProvider.GetAppPasswordAsync(appId).ConfigureAwait(false);
return ChannelProvider != null && ChannelProvider.IsGovernment() ? new MicrosoftGovernmentAppCredentials(appId, appPassword, HttpClient, Logger, oAuthScope) : new MicrosoftAppCredentials(appId, appPassword, HttpClient, Logger, oAuthScope);
}

private static T GetBodyContent<T>(string content)
{
return JsonConvert.DeserializeObject<T>(content);
Expand All @@ -248,7 +231,7 @@ private async Task<InvokeResponse<T>> SecurePostActivityAsync<T>(Uri toUrl, Acti

using (var response = await HttpClient.SendAsync(httpRequestMessage, cancellationToken).ConfigureAwait(false))
{
var content = (response.Content != null) ? await response.Content.ReadAsStringAsync().ConfigureAwait(false) : null;
var content = response.Content != null ? await response.Content.ReadAsStringAsync().ConfigureAwait(false) : null;
return new InvokeResponse<T>
{
Status = (int)response.StatusCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SkillHttpClient(HttpClient httpClient, ICredentialProvider credentialProv
/// <param name="activity">The activity to send.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>Async task with invokeResponse.</returns>
public async Task<InvokeResponse<T>> PostActivityAsync<T>(string originatingAudience, string fromBotId, BotFrameworkSkill toSkill, Uri callbackUrl, Activity activity, CancellationToken cancellationToken)
public virtual async Task<InvokeResponse<T>> PostActivityAsync<T>(string originatingAudience, string fromBotId, BotFrameworkSkill toSkill, Uri callbackUrl, Activity activity, CancellationToken cancellationToken)
{
string skillConversationId;
try
Expand Down Expand Up @@ -78,7 +78,7 @@ public async Task<InvokeResponse<T>> PostActivityAsync<T>(string originatingAudi
/// <param name="activity">activity to forward.</param>
/// <param name="cancellationToken">cancellation Token.</param>
/// <returns>Async task with optional invokeResponse.</returns>
public async Task<InvokeResponse> PostActivityAsync(string fromBotId, BotFrameworkSkill toSkill, Uri callbackUrl, Activity activity, CancellationToken cancellationToken)
public virtual async Task<InvokeResponse> PostActivityAsync(string fromBotId, BotFrameworkSkill toSkill, Uri callbackUrl, Activity activity, CancellationToken cancellationToken)
{
return await PostActivityAsync<object>(fromBotId, toSkill, callbackUrl, activity, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -93,7 +93,7 @@ public async Task<InvokeResponse> PostActivityAsync(string fromBotId, BotFramewo
/// <param name="cancellationToken">cancellation Token.</param>
/// <typeparam name="T">type of the <see cref="InvokeResponse"/> result.</typeparam>
/// <returns>Async task with optional invokeResponse of type T.</returns>
public async Task<InvokeResponse<T>> PostActivityAsync<T>(string fromBotId, BotFrameworkSkill toSkill, Uri callbackUrl, Activity activity, CancellationToken cancellationToken)
public virtual async Task<InvokeResponse<T>> PostActivityAsync<T>(string fromBotId, BotFrameworkSkill toSkill, Uri callbackUrl, Activity activity, CancellationToken cancellationToken)
{
var originatingAudience = ChannelProvider != null && ChannelProvider.IsGovernment() ? GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope : AuthenticationConstants.ToChannelFromBotOAuthScope;
return await PostActivityAsync<T>(originatingAudience, fromBotId, toSkill, callbackUrl, activity, cancellationToken).ConfigureAwait(false);
Expand Down
38 changes: 38 additions & 0 deletions tests/Microsoft.Bot.Builder.Tests/InvokeResponseOfTTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Xunit;

namespace Microsoft.Bot.Builder.Tests
{
public class InvokeResponseOfTTests
{
[Fact]
public void BodyDoesntHideBase()
{
var sut = new InvokeResponse<SomeType>
{
Status = 200,
Body = new SomeType
{
Id = "200",
Value = "blah"
}
};

// Assert that the Body property is not hidden when using InvokeResponse or InvokeResponse<T>.
var sutAsInvokeResponse = (InvokeResponse)sut;
Assert.NotNull(sutAsInvokeResponse.Body);
Assert.Same(sut.Body, sutAsInvokeResponse.Body);
Assert.Equal(sut.Body.Id, ((SomeType)sutAsInvokeResponse.Body).Id);
Assert.Equal(sut.Body.Value, ((SomeType)sutAsInvokeResponse.Body).Value);
}

private class SomeType
{
public string Id { get; set; }

public string Value { get; set; }
}
}
}
Loading