Skip to content

Commit

Permalink
EoC should only be handled when coming from parent in RunAsync (#3540) (
Browse files Browse the repository at this point in the history
#3543)

* Added check to only cancel dialogs in RunAsync when EoC comes from a parent bot (root or skill).
* Applied URI prefix to caller ID as per OBI spec
https://github.com/microsoft/botframework-obi/blob/master/protocols/botframework-activity/botframework-activity.md#bot-calling-skill

(cherry picked from commit 850be38)
  • Loading branch information
gabog authored Mar 12, 2020
1 parent f0e6350 commit 2acec1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions libraries/Microsoft.Bot.Builder.Dialogs/DialogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public static async Task RunAsync(this Dialog dialog, ITurnContext turnContext,
if (turnContext.TurnState.Get<IIdentity>(BotAdapter.BotIdentityKey) is ClaimsIdentity claimIdentity && SkillValidation.IsSkillClaim(claimIdentity.Claims))
{
// The bot is running as a skill.
if (turnContext.Activity.Type == ActivityTypes.EndOfConversation && dialogContext.Stack.Any())
if (turnContext.Activity.Type == ActivityTypes.EndOfConversation && dialogContext.Stack.Any() && IsEocComingFromParent(turnContext))
{
// Handle remote cancellation request if we have something in the stack.
// Handle remote cancellation request from parent.
var activeDialogContext = GetActiveDialogContext(dialogContext);

var remoteCancelText = "Skill was canceled by a request from the host.";
var remoteCancelText = "Skill was canceled through an EndOfConversation activity from the parent.";
await turnContext.TraceActivityAsync($"{typeof(Dialog).Name}.RunAsync()", label: $"{remoteCancelText}", cancellationToken: cancellationToken).ConfigureAwait(false);

// Send cancellation message to the top dialog in the stack to ensure all the parents are canceled in the right order.
Expand Down Expand Up @@ -90,6 +90,16 @@ public static async Task RunAsync(this Dialog dialog, ITurnContext turnContext,
}
}

// We should only cancel the current dialog stack if the EoC activity is coming from a parent (a root bot or another skill).
// When the EoC is coming back from a child, we should just process that EoC normally through the
// dialog stack and let the child dialogs handle that.
private static bool IsEocComingFromParent(ITurnContext turnContext)
{
// To determine the direction we check callerId property which is set to the parent bot
// by the BotFrameworkHttpClient on outgoing requests.
return !string.IsNullOrWhiteSpace(turnContext.Activity.CallerId);
}

// Recursively walk up the DC stack to find the active DC.
private static DialogContext GetActiveDialogContext(DialogContext dialogContext)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override async Task<InvokeResponse<T>> PostActivityAsync<T>(string fromBo
};
activity.Conversation.Id = conversationId;
activity.ServiceUrl = serviceUrl.ToString();
activity.CallerId = fromBotId;
activity.CallerId = $"urn:botframework:aadappid:{fromBotId}";

using (var jsonContent = new StringContent(JsonConvert.SerializeObject(activity, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json"))
{
Expand Down

0 comments on commit 2acec1c

Please sign in to comment.