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

Commit

Permalink
[Skills] fix the issue where in webchat skill responses are not sent …
Browse files Browse the repository at this point in the history
…back to user properly (#1540)

* set recipient id back to original after message sent to skill

* fix indentation
  • Loading branch information
lzc850612 authored and darrenj committed Jun 13, 2019
1 parent a0cf7c0 commit 039680f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public class SkillCallingRequestHandler : RequestHandler
{
private readonly Router _router;
private readonly ITurnContext _turnContext;
private readonly IBotTelemetryClient _botTelemetryClient;
private readonly Action<Activity> _tokenRequestHandler;
private readonly Action<Activity> _handoffActivityHandler;

public SkillCallingRequestHandler(ITurnContext turnContext, Action<Activity> tokenRequestHandler = null, Action<Activity> handoffActivityHandler = null)
public SkillCallingRequestHandler(ITurnContext turnContext, IBotTelemetryClient botTelemetryClient, Action<Activity> tokenRequestHandler = null, Action<Activity> handoffActivityHandler = null)
{
_turnContext = turnContext ?? throw new ArgumentNullException(nameof(turnContext));
_botTelemetryClient = botTelemetryClient;
_tokenRequestHandler = tokenRequestHandler;
_handoffActivityHandler = handoffActivityHandler;

Expand Down Expand Up @@ -121,8 +123,9 @@ public override async Task<Response> ProcessRequestAsync(ReceiveRequest request,
var responseBody = await routeContext.Action.Action(request, routeContext.RouteData).ConfigureAwait(false);
return Response.OK(new StringContent(JsonConvert.SerializeObject(responseBody, SerializationSettings.DefaultSerializationSettings), Encoding.UTF8, SerializationSettings.ApplicationJson));
}
catch
catch (Exception ex)
{
_botTelemetryClient.TrackException(ex);
return Response.InternalServerError();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public SkillDialog(
: base(skillManifest.Id)
{
_skillManifest = skillManifest ?? throw new ArgumentNullException(nameof(SkillManifest));
_serviceClientCredentials = serviceClientCredentials ?? throw new ArgumentNullException(nameof(serviceClientCredentials));
_serviceClientCredentials = serviceClientCredentials ?? throw new ArgumentNullException(nameof(serviceClientCredentials));
_telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient));
_userState = userState;
_skillTransport = skillTransport ?? new SkillWebSocketTransport(_skillManifest, _serviceClientCredentials);
_skillTransport = skillTransport ?? new SkillWebSocketTransport(_skillManifest, _serviceClientCredentials, telemetryClient);

if (authDialog != null)
if (authDialog != null)
{
_authDialog = authDialog;
AddDialog(authDialog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ public class SkillWebSocketTransport : ISkillTransport
private IStreamingTransportClient _streamingTransportClient;
private readonly SkillManifest _skillManifest;
private readonly IServiceClientCredentials _serviceClientCredentials;
private readonly IBotTelemetryClient _botTelemetryClient;
private bool endOfConversation = false;

public SkillWebSocketTransport(
SkillManifest skillManifest,
IServiceClientCredentials serviceCLientCredentials,
IStreamingTransportClient streamingTransportClient = null)
SkillManifest skillManifest,
IServiceClientCredentials serviceCLientCredentials,
IBotTelemetryClient botTelemetryClient,
IStreamingTransportClient streamingTransportClient = null)
{
_skillManifest = skillManifest ?? throw new ArgumentNullException(nameof(skillManifest));
_serviceClientCredentials = serviceCLientCredentials ?? throw new ArgumentNullException(nameof(serviceCLientCredentials));
_streamingTransportClient = streamingTransportClient;
}
_botTelemetryClient = botTelemetryClient;
_streamingTransportClient = streamingTransportClient;
}

public async Task<bool> ForwardToSkillAsync(ITurnContext turnContext, Activity activity, Action<Activity> tokenRequestHandler = null)
{
Expand All @@ -44,29 +47,30 @@ public async Task<bool> ForwardToSkillAsync(ITurnContext turnContext, Activity a
var headers = new Dictionary<string, string>();
headers.Add("Authorization", $"Bearer {token}");

// establish websocket connection
_streamingTransportClient = new WebSocketClient(
// establish websocket connection
_streamingTransportClient = new WebSocketClient(
EnsureWebSocketUrl(_skillManifest.Endpoint.ToString()),
new SkillCallingRequestHandler(
turnContext,
_botTelemetryClient,
GetTokenCallback(turnContext, tokenRequestHandler),
GetHandoffActivityCallback()),
headers);

await _streamingTransportClient.ConnectAsync();
}

// set recipient to the skill
if (activity.Recipient == null)
{
activity.Recipient = new ChannelAccount();
}

activity.Recipient.Id = _skillManifest.MSAappId;
// set recipient to the skill
var recipientId = activity.Recipient.Id;
activity.Recipient.Id = _skillManifest.MSAappId;

// Serialize the activity and POST to the Skill endpoint
var body = new StringContent(JsonConvert.SerializeObject(activity, SerializationSettings.BotSchemaSerializationSettings), Encoding.UTF8, SerializationSettings.ApplicationJson);
var request = Request.CreatePost(string.Empty, body);

// set back recipient id to make things consistent
activity.Recipient.Id = recipientId;

await _streamingTransportClient.SendAsync(request);

return endOfConversation;
Expand All @@ -86,7 +90,7 @@ public void Disconnect()
{
if (_streamingTransportClient != null)
{
_streamingTransportClient.Disconnect();
_streamingTransportClient.Disconnect();
}
}

Expand Down

0 comments on commit 039680f

Please sign in to comment.