Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

[4.4]fix token response issue where it's not getting to the skill properly #1137

Merged
merged 1 commit into from
Apr 23, 2019
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 @@ -6,7 +6,7 @@ namespace Microsoft.Bot.Builder.Skills
{
public interface ISkillTransport
{
Task<bool> ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Func<Activity, Activity> tokenRequestHandler = null);
Task<bool> ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Action<Activity> tokenRequestHandler = null);

Task CancelRemoteDialogsAsync(ITurnContext turnContext);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
Expand Down Expand Up @@ -26,6 +27,9 @@ public class SkillDialog : ComponentDialog
private ISkillTransport _skillTransport;
private Models.Manifest.Action _action;

private Queue<Activity> _queuedResponses = new Queue<Activity>();
private object _lockObject = new object();

/// <summary>
/// Initializes a new instance of the <see cref="SkillDialog"/> class.
/// SkillDialog constructor that accepts the manifest description of a Skill along with TelemetryClient for end to end telemetry.
Expand Down Expand Up @@ -161,6 +165,13 @@ public override async Task EndDialogAsync(ITurnContext turnContext, DialogInstan

var dialogResult = await ForwardToSkillAsync(innerDc, activity);

// if there's any response we need to send to the skill queued
// forward to skill and start a new turn
while (_queuedResponses.Count > 0)
{
await ForwardToSkillAsync(innerDc, _queuedResponses.Dequeue());
}

_skillTransport.Disconnect();

return dialogResult;
Expand Down Expand Up @@ -196,7 +207,7 @@ private async Task<DialogTurnResult> ForwardToSkillAsync(DialogContext innerDc,
}
}

private Func<Activity, Activity> GetTokenRequestCallback(DialogContext dialogContext)
private Action<Activity> GetTokenRequestCallback(DialogContext dialogContext)
{
return (activity) =>
{
Expand All @@ -212,13 +223,12 @@ private Func<Activity, Activity> GetTokenRequestCallback(DialogContext dialogCon
tokenEvent.Name = TokenEvents.TokenResponseEventName;
tokenEvent.Value = authResult.Result as ProviderTokenResponse;

return tokenEvent;
}
else
{
return null;
lock (_lockObject)
{
_queuedResponses.Enqueue(tokenEvent);
}
}
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Disconnect()
// doesn't have to do any disconnect for http
}

public async Task<bool> ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Func<Activity, Activity> tokenRequestHandler = null)
public async Task<bool> ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Action<Activity> tokenRequestHandler = null)
{
// Serialize the activity and POST to the Skill endpoint
var httpRequest = new HttpRequestMessage();
Expand Down Expand Up @@ -83,11 +83,7 @@ public async Task<bool> ForwardToSkillAsync(ITurnContext dialogContext, Activity
{
if (tokenRequestHandler != null)
{
var tokenResponseActivity = tokenRequestHandler(skillResponse);
if (tokenResponseActivity != null)
{
return await ForwardToSkillAsync(dialogContext, tokenResponseActivity);
}
tokenRequestHandler(skillResponse);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public SkillWebSocketTransport(SkillManifest skillManifest, MicrosoftAppCredenti
_microsoftAppCredentialsEx = microsoftAppCredentialsEx ?? throw new ArgumentNullException(nameof(microsoftAppCredentialsEx));
}

public async Task<bool> ForwardToSkillAsync(ITurnContext turnContext, Activity activity, Func<Activity, Activity> tokenRequestHandler = null)
public async Task<bool> ForwardToSkillAsync(ITurnContext turnContext, Activity activity, Action<Activity> tokenRequestHandler = null)
{
if (_webSocketClient == null)
{
Expand Down Expand Up @@ -77,18 +77,13 @@ public void Disconnect()
}
}

private Action<Activity> GetTokenCallback(ITurnContext turnContext, Func<Activity, Activity> tokenRequestHandler)
private Action<Activity> GetTokenCallback(ITurnContext turnContext, Action<Activity> tokenRequestHandler)
{
return async (activity) =>
return (activity) =>
{
if (tokenRequestHandler != null)
{
var tokenResponseActivity = tokenRequestHandler(activity);

if (tokenResponseActivity != null)
{
await ForwardToSkillAsync(turnContext, tokenResponseActivity);
}
tokenRequestHandler(activity);
}
};
}
Expand Down