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

Commit

Permalink
[skills] update to ActivityHandler (#2667)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhr0804 authored and bobokids committed Nov 8, 2019
1 parent 2b0a7ee commit e4e33d8
Show file tree
Hide file tree
Showing 23 changed files with 254 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Builder.Solutions.Middleware;
using Microsoft.Bot.Builder.Solutions.Responses;
Expand All @@ -21,6 +22,7 @@ public AutomotiveSkillWebSocketBotAdapter(
UserState userState,
ConversationState conversationState,
ResponseManager responseManager,
TelemetryInitializerMiddleware telemetryMiddleware,
IBotTelemetryClient telemetryClient)
: base(null, telemetryClient)
{
Expand All @@ -32,6 +34,7 @@ public AutomotiveSkillWebSocketBotAdapter(
telemetryClient.TrackException(exception);
};

Use(telemetryMiddleware);
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
Use(new SetLocaleMiddleware(settings.DefaultLocale ?? "en-us"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AutomotiveSkill.Services;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.Solutions.Middleware;
using Microsoft.Bot.Builder.Solutions.Responses;
Expand All @@ -19,6 +20,7 @@ public class DefaultAdapter : BotFrameworkHttpAdapter
public DefaultAdapter(
BotSettings settings,
ICredentialProvider credentialProvider,
TelemetryInitializerMiddleware telemetryMiddleware,
IBotTelemetryClient telemetryClient,
ResponseManager responseManager)
: base(credentialProvider)
Expand All @@ -31,6 +33,7 @@ public DefaultAdapter(
telemetryClient.TrackException(exception);
};

Use(telemetryMiddleware);
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
Use(new ShowTypingMiddleware());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<PackageReference Include="Microsoft.Bot.Builder.Dialogs" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.ApplicationInsights.Core" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.5.4" />
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.5.4" />
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.6.0-preview-rc2" />
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.6.0-preview-rc2" />
<PackageReference Include="Microsoft.Bot.Builder.TemplateManager" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Configuration" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Connector" Version="4.6.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.DependencyInjection;

namespace AutomotiveSkill.Bots
{
public class DefaultActivityHandler<T> : ActivityHandler
where T : Dialog
{
private readonly Dialog _dialog;
private readonly BotState _conversationState;
private readonly BotState _userState;
private IStatePropertyAccessor<DialogState> _dialogStateAccessor;

public DefaultActivityHandler(IServiceProvider serviceProvider, T dialog)
{
_dialog = dialog;
_conversationState = serviceProvider.GetService<ConversationState>();
_userState = serviceProvider.GetService<UserState>();
_dialogStateAccessor = _conversationState.CreateProperty<DialogState>(nameof(DialogState));
}

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
await base.OnTurnAsync(turnContext, cancellationToken);

// Save any state changes that might have occured during the turn.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}

protected override Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
}

protected override Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
}

protected override Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
}
}
}
46 changes: 0 additions & 46 deletions skills/csharp/experimental/automotiveskill/Bots/DialogBot.cs

This file was deleted.

14 changes: 7 additions & 7 deletions skills/csharp/experimental/automotiveskill/Dialogs/MainDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace AutomotiveSkill.Dialogs
{
public class MainDialog : RouterDialog
public class MainDialog : ActivityHandlerDialog
{
private BotServices _services;
private ResponseManager _responseManager;
Expand All @@ -50,13 +50,13 @@ public MainDialog(
AddDialog(vehicleSettingsDialog ?? throw new ArgumentNullException(nameof(vehicleSettingsDialog)));
}

protected override async Task OnStartAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
protected override async Task OnMembersAddedAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
{
// send a greeting if we're in local mode
await dc.Context.SendActivityAsync(_responseManager.GetResponse(AutomotiveSkillMainResponses.WelcomeMessage));
}

protected override async Task RouteAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
protected override async Task OnMessageActivityAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
{
var state = await _stateAccessor.GetAsync(dc.Context, () => new AutomotiveSkillState());

Expand Down Expand Up @@ -105,7 +105,7 @@ public MainDialog(
}
}

protected override async Task CompleteAsync(DialogContext dc, DialogTurnResult result = null, CancellationToken cancellationToken = default(CancellationToken))
protected override async Task OnDialogCompleteAsync(DialogContext dc, object result = null, CancellationToken cancellationToken = default(CancellationToken))
{
// workaround. if connect skill directly to teams, the following response does not work.
if (dc.Context.Adapter is IRemoteUserTokenProvider remoteInvocationAdapter || Channel.GetChannelId(dc.Context) != Channels.Msteams)
Expand Down Expand Up @@ -170,15 +170,15 @@ private async Task<InterruptionAction> OnCancel(DialogContext dc)
var response = _responseManager.GetResponse(AutomotiveSkillMainResponses.CancelMessage);
await dc.Context.SendActivityAsync(response);

await CompleteAsync(dc);
await OnDialogCompleteAsync(dc);
await dc.CancelAllDialogsAsync();
return InterruptionAction.StartedDialog;
return InterruptionAction.End;
}

private async Task<InterruptionAction> OnHelp(DialogContext dc)
{
await dc.Context.SendActivityAsync(_responseManager.GetResponse(AutomotiveSkillMainResponses.HelpMessage));
return InterruptionAction.MessageSentToUser;
return InterruptionAction.Resume;
}
}
}
14 changes: 8 additions & 6 deletions skills/csharp/experimental/automotiveskill/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace AutomotiveSkill
using AutomotiveSkill.Responses.VehicleSettings;
using AutomotiveSkill.Services;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Bot.Builder;
Expand Down Expand Up @@ -82,9 +83,11 @@ public void ConfigureServices(IServiceCollection services)

// Configure telemetry
services.AddApplicationInsightsTelemetry();
var telemetryClient = new BotTelemetryClient(new TelemetryClient());
services.AddSingleton<IBotTelemetryClient>(telemetryClient);
services.AddBotApplicationInsights(telemetryClient);
services.AddSingleton<IBotTelemetryClient, BotTelemetryClient>();
services.AddSingleton<ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, TelemetryBotIdInitializer>();
services.AddSingleton<TelemetryInitializerMiddleware>();
services.AddSingleton<TelemetryLoggerMiddleware>();

// Configure bot services
services.AddSingleton<BotServices>();
Expand Down Expand Up @@ -114,7 +117,7 @@ public void ConfigureServices(IServiceCollection services)

// Configure bot
services.AddTransient<MainDialog>();
services.AddTransient<IBot, DialogBot<MainDialog>>();
services.AddTransient<IBot, DefaultActivityHandler<MainDialog>>();
}

/// <summary>
Expand All @@ -125,8 +128,7 @@ public void ConfigureServices(IServiceCollection services)
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
_isProduction = env.IsProduction();
app.UseBotApplicationInsights()
.UseDefaultFiles()
app.UseDefaultFiles()
.UseStaticFiles()
.UseWebSockets()
.UseMvc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using BingSearchSkill.Services;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Builder.Solutions.Middleware;
Expand All @@ -20,6 +21,7 @@ public CustomSkillAdapter(
BotSettings settings,
UserState userState,
ConversationState conversationState,
TelemetryInitializerMiddleware telemetryMiddleware,
ResponseManager responseManager,
IBotTelemetryClient telemetryClient)
: base(null, telemetryClient)
Expand All @@ -32,6 +34,7 @@ public CustomSkillAdapter(
telemetryClient.TrackException(exception);
};

Use(telemetryMiddleware);
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
Use(new ShowTypingMiddleware());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using BingSearchSkill.Services;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.Solutions.Middleware;
using Microsoft.Bot.Builder.Solutions.Responses;
Expand All @@ -19,6 +20,7 @@ public class DefaultAdapter : BotFrameworkHttpAdapter
public DefaultAdapter(
BotSettings settings,
ICredentialProvider credentialProvider,
TelemetryInitializerMiddleware telemetryMiddleware,
IBotTelemetryClient telemetryClient,
ResponseManager responseManager)
: base(credentialProvider)
Expand All @@ -31,6 +33,7 @@ public DefaultAdapter(
telemetryClient.TrackException(exception);
};

Use(telemetryMiddleware);
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
Use(new ShowTypingMiddleware());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
<PackageReference Include="Microsoft.Bot.Builder.Dialogs" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.ApplicationInsights.Core" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.5.4" />
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.5.4" />
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.6.0-preview-rc2" />
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.6.0-preview-rc2" />
<PackageReference Include="Microsoft.Bot.Builder.TemplateManager" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Configuration" Version="4.6.1" />
<PackageReference Include="Microsoft.Bot.Connector" Version="4.6.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.DependencyInjection;

namespace BingSearchSkill.Bots
{
public class DefaultActivityHandler<T> : ActivityHandler
where T : Dialog
{
private readonly Dialog _dialog;
private readonly BotState _conversationState;
private readonly BotState _userState;
private IStatePropertyAccessor<DialogState> _dialogStateAccessor;

public DefaultActivityHandler(IServiceProvider serviceProvider, T dialog)
{
_dialog = dialog;
_conversationState = serviceProvider.GetService<ConversationState>();
_userState = serviceProvider.GetService<UserState>();
_dialogStateAccessor = _conversationState.CreateProperty<DialogState>(nameof(DialogState));
}

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
await base.OnTurnAsync(turnContext, cancellationToken);

// Save any state changes that might have occured during the turn.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
}

protected override Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
}

protected override Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
}

protected override Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
}
}
}
Loading

0 comments on commit e4e33d8

Please sign in to comment.