From e4e33d8fca31e0eb71aaf81279b45de9e9c3207d Mon Sep 17 00:00:00 2001 From: xhr0804 <44257569+xhr0804@users.noreply.github.com> Date: Fri, 8 Nov 2019 10:15:44 +0800 Subject: [PATCH] [skills] update to ActivityHandler (#2667) --- .../AutomotiveSkillWebSocketBotAdapter.cs | 3 + .../Adapters/DefaultAdapter.cs | 3 + .../automotiveskill/AutomotiveSkill.csproj | 4 +- .../Bots/DefaultActivityHandler.cs | 55 +++++++++++++++++++ .../automotiveskill/Bots/DialogBot.cs | 46 ---------------- .../automotiveskill/Dialogs/MainDialog.cs | 14 ++--- .../experimental/automotiveskill/Startup.cs | 14 +++-- .../Adapters/CustomSkillAdapter.cs | 3 + .../Adapters/DefaultAdapter.cs | 3 + .../bingsearchskill/BingSearchSkill.csproj | 4 +- .../Bots/DefaultActivityHandler.cs | 55 +++++++++++++++++++ .../bingsearchskill/Bots/DialogBot.cs | 46 ---------------- .../bingsearchskill/Dialogs/MainDialog.cs | 18 +++--- .../experimental/bingsearchskill/Startup.cs | 14 +++-- .../musicskill/Adapters/CustomSkillAdapter.cs | 3 + .../musicskill/Adapters/DefaultAdapter.cs | 3 + .../musicskill/Bots/DefaultActivityHandler.cs | 55 +++++++++++++++++++ .../experimental/musicskill/Bots/DialogBot.cs | 46 ---------------- .../musicskill/Dialogs/MainDialog.cs | 18 +++--- .../experimental/musicskill/MusicSkill.csproj | 4 +- .../csharp/experimental/musicskill/Startup.cs | 14 +++-- .../Flow/AutomotiveSkillTestBase.cs | 4 +- .../Flow/Fakes/MockLuisRecognizer.cs | 26 +++++---- 23 files changed, 254 insertions(+), 201 deletions(-) create mode 100644 skills/csharp/experimental/automotiveskill/Bots/DefaultActivityHandler.cs delete mode 100644 skills/csharp/experimental/automotiveskill/Bots/DialogBot.cs create mode 100644 skills/csharp/experimental/bingsearchskill/Bots/DefaultActivityHandler.cs delete mode 100644 skills/csharp/experimental/bingsearchskill/Bots/DialogBot.cs create mode 100644 skills/csharp/experimental/musicskill/Bots/DefaultActivityHandler.cs delete mode 100644 skills/csharp/experimental/musicskill/Bots/DialogBot.cs diff --git a/skills/csharp/experimental/automotiveskill/Adapters/AutomotiveSkillWebSocketBotAdapter.cs b/skills/csharp/experimental/automotiveskill/Adapters/AutomotiveSkillWebSocketBotAdapter.cs index d5c219ae0b..1b28a9da30 100644 --- a/skills/csharp/experimental/automotiveskill/Adapters/AutomotiveSkillWebSocketBotAdapter.cs +++ b/skills/csharp/experimental/automotiveskill/Adapters/AutomotiveSkillWebSocketBotAdapter.cs @@ -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; @@ -21,6 +22,7 @@ public AutomotiveSkillWebSocketBotAdapter( UserState userState, ConversationState conversationState, ResponseManager responseManager, + TelemetryInitializerMiddleware telemetryMiddleware, IBotTelemetryClient telemetryClient) : base(null, telemetryClient) { @@ -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")); diff --git a/skills/csharp/experimental/automotiveskill/Adapters/DefaultAdapter.cs b/skills/csharp/experimental/automotiveskill/Adapters/DefaultAdapter.cs index 06e3e6a27a..f351826326 100644 --- a/skills/csharp/experimental/automotiveskill/Adapters/DefaultAdapter.cs +++ b/skills/csharp/experimental/automotiveskill/Adapters/DefaultAdapter.cs @@ -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; @@ -19,6 +20,7 @@ public class DefaultAdapter : BotFrameworkHttpAdapter public DefaultAdapter( BotSettings settings, ICredentialProvider credentialProvider, + TelemetryInitializerMiddleware telemetryMiddleware, IBotTelemetryClient telemetryClient, ResponseManager responseManager) : base(credentialProvider) @@ -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()); diff --git a/skills/csharp/experimental/automotiveskill/AutomotiveSkill.csproj b/skills/csharp/experimental/automotiveskill/AutomotiveSkill.csproj index 957eb706f8..db31f89346 100644 --- a/skills/csharp/experimental/automotiveskill/AutomotiveSkill.csproj +++ b/skills/csharp/experimental/automotiveskill/AutomotiveSkill.csproj @@ -27,8 +27,8 @@ - - + + diff --git a/skills/csharp/experimental/automotiveskill/Bots/DefaultActivityHandler.cs b/skills/csharp/experimental/automotiveskill/Bots/DefaultActivityHandler.cs new file mode 100644 index 0000000000..79f3c1c8c7 --- /dev/null +++ b/skills/csharp/experimental/automotiveskill/Bots/DefaultActivityHandler.cs @@ -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 : ActivityHandler + where T : Dialog + { + private readonly Dialog _dialog; + private readonly BotState _conversationState; + private readonly BotState _userState; + private IStatePropertyAccessor _dialogStateAccessor; + + public DefaultActivityHandler(IServiceProvider serviceProvider, T dialog) + { + _dialog = dialog; + _conversationState = serviceProvider.GetService(); + _userState = serviceProvider.GetService(); + _dialogStateAccessor = _conversationState.CreateProperty(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 membersAdded, ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + + protected override Task OnMessageActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + + protected override Task OnEventActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + } +} \ No newline at end of file diff --git a/skills/csharp/experimental/automotiveskill/Bots/DialogBot.cs b/skills/csharp/experimental/automotiveskill/Bots/DialogBot.cs deleted file mode 100644 index 7785e7b4ce..0000000000 --- a/skills/csharp/experimental/automotiveskill/Bots/DialogBot.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -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 DialogBot : IBot - where T : Dialog - { - private readonly Dialog _dialog; - private readonly BotState _conversationState; - private readonly BotState _userState; - private readonly IBotTelemetryClient _telemetryClient; - - public DialogBot(IServiceProvider serviceProvider, T dialog) - { - _dialog = dialog; - _conversationState = serviceProvider.GetService(); - _userState = serviceProvider.GetService(); - _telemetryClient = serviceProvider.GetService(); - } - - public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) - { - // Client notifying this bot took to long to respond (timed out) - if (turnContext.Activity.Code == EndOfConversationCodes.BotTimedOut) - { - _telemetryClient.TrackTrace($"Timeout in {turnContext.Activity.ChannelId} channel: Bot took too long to respond.", Severity.Information, null); - return; - } - - await _dialog.RunAsync(turnContext, _conversationState.CreateProperty(nameof(DialogState)), cancellationToken); - - // Save any state changes that might have occured during the turn. - await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken); - await _userState.SaveChangesAsync(turnContext, false, cancellationToken); - } - } -} \ No newline at end of file diff --git a/skills/csharp/experimental/automotiveskill/Dialogs/MainDialog.cs b/skills/csharp/experimental/automotiveskill/Dialogs/MainDialog.cs index de5eb44de1..7fadcf412d 100644 --- a/skills/csharp/experimental/automotiveskill/Dialogs/MainDialog.cs +++ b/skills/csharp/experimental/automotiveskill/Dialogs/MainDialog.cs @@ -23,7 +23,7 @@ namespace AutomotiveSkill.Dialogs { - public class MainDialog : RouterDialog + public class MainDialog : ActivityHandlerDialog { private BotServices _services; private ResponseManager _responseManager; @@ -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()); @@ -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) @@ -170,15 +170,15 @@ private async Task 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 OnHelp(DialogContext dc) { await dc.Context.SendActivityAsync(_responseManager.GetResponse(AutomotiveSkillMainResponses.HelpMessage)); - return InterruptionAction.MessageSentToUser; + return InterruptionAction.Resume; } } } \ No newline at end of file diff --git a/skills/csharp/experimental/automotiveskill/Startup.cs b/skills/csharp/experimental/automotiveskill/Startup.cs index be4f06b57f..d07d836658 100644 --- a/skills/csharp/experimental/automotiveskill/Startup.cs +++ b/skills/csharp/experimental/automotiveskill/Startup.cs @@ -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; @@ -82,9 +83,11 @@ public void ConfigureServices(IServiceCollection services) // Configure telemetry services.AddApplicationInsightsTelemetry(); - var telemetryClient = new BotTelemetryClient(new TelemetryClient()); - services.AddSingleton(telemetryClient); - services.AddBotApplicationInsights(telemetryClient); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); // Configure bot services services.AddSingleton(); @@ -114,7 +117,7 @@ public void ConfigureServices(IServiceCollection services) // Configure bot services.AddTransient(); - services.AddTransient>(); + services.AddTransient>(); } /// @@ -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(); diff --git a/skills/csharp/experimental/bingsearchskill/Adapters/CustomSkillAdapter.cs b/skills/csharp/experimental/bingsearchskill/Adapters/CustomSkillAdapter.cs index cdd2346c14..8917ee0a22 100644 --- a/skills/csharp/experimental/bingsearchskill/Adapters/CustomSkillAdapter.cs +++ b/skills/csharp/experimental/bingsearchskill/Adapters/CustomSkillAdapter.cs @@ -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; @@ -20,6 +21,7 @@ public CustomSkillAdapter( BotSettings settings, UserState userState, ConversationState conversationState, + TelemetryInitializerMiddleware telemetryMiddleware, ResponseManager responseManager, IBotTelemetryClient telemetryClient) : base(null, telemetryClient) @@ -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()); diff --git a/skills/csharp/experimental/bingsearchskill/Adapters/DefaultAdapter.cs b/skills/csharp/experimental/bingsearchskill/Adapters/DefaultAdapter.cs index b39ca9b564..09e533d975 100644 --- a/skills/csharp/experimental/bingsearchskill/Adapters/DefaultAdapter.cs +++ b/skills/csharp/experimental/bingsearchskill/Adapters/DefaultAdapter.cs @@ -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; @@ -19,6 +20,7 @@ public class DefaultAdapter : BotFrameworkHttpAdapter public DefaultAdapter( BotSettings settings, ICredentialProvider credentialProvider, + TelemetryInitializerMiddleware telemetryMiddleware, IBotTelemetryClient telemetryClient, ResponseManager responseManager) : base(credentialProvider) @@ -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()); diff --git a/skills/csharp/experimental/bingsearchskill/BingSearchSkill.csproj b/skills/csharp/experimental/bingsearchskill/BingSearchSkill.csproj index f6087c1107..91b9f97a18 100644 --- a/skills/csharp/experimental/bingsearchskill/BingSearchSkill.csproj +++ b/skills/csharp/experimental/bingsearchskill/BingSearchSkill.csproj @@ -75,8 +75,8 @@ - - + + diff --git a/skills/csharp/experimental/bingsearchskill/Bots/DefaultActivityHandler.cs b/skills/csharp/experimental/bingsearchskill/Bots/DefaultActivityHandler.cs new file mode 100644 index 0000000000..7424a63dac --- /dev/null +++ b/skills/csharp/experimental/bingsearchskill/Bots/DefaultActivityHandler.cs @@ -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 : ActivityHandler + where T : Dialog + { + private readonly Dialog _dialog; + private readonly BotState _conversationState; + private readonly BotState _userState; + private IStatePropertyAccessor _dialogStateAccessor; + + public DefaultActivityHandler(IServiceProvider serviceProvider, T dialog) + { + _dialog = dialog; + _conversationState = serviceProvider.GetService(); + _userState = serviceProvider.GetService(); + _dialogStateAccessor = _conversationState.CreateProperty(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 membersAdded, ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + + protected override Task OnMessageActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + + protected override Task OnEventActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + } +} \ No newline at end of file diff --git a/skills/csharp/experimental/bingsearchskill/Bots/DialogBot.cs b/skills/csharp/experimental/bingsearchskill/Bots/DialogBot.cs deleted file mode 100644 index 66c4fb0ec0..0000000000 --- a/skills/csharp/experimental/bingsearchskill/Bots/DialogBot.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -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 DialogBot : IBot - where T : Dialog - { - private readonly Dialog _dialog; - private readonly BotState _conversationState; - private readonly BotState _userState; - private readonly IBotTelemetryClient _telemetryClient; - - public DialogBot(IServiceProvider serviceProvider, T dialog) - { - _dialog = dialog; - _conversationState = serviceProvider.GetService(); - _userState = serviceProvider.GetService(); - _telemetryClient = serviceProvider.GetService(); - } - - public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) - { - // Client notifying this bot took to long to respond (timed out) - if (turnContext.Activity.Code == EndOfConversationCodes.BotTimedOut) - { - _telemetryClient.TrackTrace($"Timeout in {turnContext.Activity.ChannelId} channel: Bot took too long to respond.", Severity.Information, null); - return; - } - - await _dialog.RunAsync(turnContext, _conversationState.CreateProperty(nameof(DialogState)), cancellationToken); - - // Save any state changes that might have occured during the turn. - await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken); - await _userState.SaveChangesAsync(turnContext, false, cancellationToken); - } - } -} \ No newline at end of file diff --git a/skills/csharp/experimental/bingsearchskill/Dialogs/MainDialog.cs b/skills/csharp/experimental/bingsearchskill/Dialogs/MainDialog.cs index 2539fa7daf..bb25919974 100644 --- a/skills/csharp/experimental/bingsearchskill/Dialogs/MainDialog.cs +++ b/skills/csharp/experimental/bingsearchskill/Dialogs/MainDialog.cs @@ -23,7 +23,7 @@ namespace BingSearchSkill.Dialogs { - public class MainDialog : RouterDialog + public class MainDialog : ActivityHandlerDialog { private BotSettings _settings; private BotServices _services; @@ -54,13 +54,13 @@ public MainDialog( AddDialog(sampleDialog); } - protected override async Task OnStartAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) + protected override async Task OnMembersAddedAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) { var locale = CultureInfo.CurrentUICulture; await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.WelcomeMessage)); } - protected override async Task RouteAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) + protected override async Task OnMessageActivityAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) { // get current activity locale var locale = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; @@ -101,7 +101,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) @@ -115,7 +115,7 @@ public MainDialog( await dc.EndDialogAsync(result); } - protected override async Task OnEventAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) + protected override async Task OnEventActivityAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) { switch (dc.Context.Activity.Name) { @@ -204,15 +204,15 @@ public MainDialog( private async Task OnCancel(DialogContext dc) { await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.CancelMessage)); - await CompleteAsync(dc); + await OnDialogCompleteAsync(dc); await dc.CancelAllDialogsAsync(); - return InterruptionAction.StartedDialog; + return InterruptionAction.End; } private async Task OnHelp(DialogContext dc) { await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.HelpMessage)); - return InterruptionAction.MessageSentToUser; + return InterruptionAction.Resume; } private async Task OnLogout(DialogContext dc) @@ -239,7 +239,7 @@ private async Task OnLogout(DialogContext dc) await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.LogOut)); - return InterruptionAction.StartedDialog; + return InterruptionAction.End; } private async Task PopulateStateFromSkillContext(ITurnContext context) diff --git a/skills/csharp/experimental/bingsearchskill/Startup.cs b/skills/csharp/experimental/bingsearchskill/Startup.cs index 33091cc465..be159d4fc3 100644 --- a/skills/csharp/experimental/bingsearchskill/Startup.cs +++ b/skills/csharp/experimental/bingsearchskill/Startup.cs @@ -10,6 +10,7 @@ using BingSearchSkill.Responses.Shared; using BingSearchSkill.Services; using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -78,9 +79,11 @@ public void ConfigureServices(IServiceCollection services) // Configure telemetry services.AddApplicationInsightsTelemetry(); - var telemetryClient = new BotTelemetryClient(new TelemetryClient()); - services.AddSingleton(telemetryClient); - services.AddBotApplicationInsights(telemetryClient); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); // Configure bot services services.AddSingleton(); @@ -110,7 +113,7 @@ public void ConfigureServices(IServiceCollection services) // Configure bot services.AddTransient(); - services.AddTransient>(); + services.AddTransient>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -121,8 +124,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseDeveloperExceptionPage(); } - app.UseBotApplicationInsights() - .UseDefaultFiles() + app.UseDefaultFiles() .UseStaticFiles() .UseWebSockets() .UseMvc(); diff --git a/skills/csharp/experimental/musicskill/Adapters/CustomSkillAdapter.cs b/skills/csharp/experimental/musicskill/Adapters/CustomSkillAdapter.cs index 9dab43cf83..ef44cecc66 100644 --- a/skills/csharp/experimental/musicskill/Adapters/CustomSkillAdapter.cs +++ b/skills/csharp/experimental/musicskill/Adapters/CustomSkillAdapter.cs @@ -5,6 +5,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; @@ -21,6 +22,7 @@ public CustomSkillAdapter( UserState userState, ConversationState conversationState, ResponseManager responseManager, + TelemetryInitializerMiddleware telemetryMiddleware, IBotTelemetryClient telemetryClient) : base(null, telemetryClient) { @@ -34,6 +36,7 @@ public CustomSkillAdapter( // Uncomment the following line for local development without Azure Storage // Use(new TranscriptLoggerMiddleware(new MemoryTranscriptStore())); + 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")); diff --git a/skills/csharp/experimental/musicskill/Adapters/DefaultAdapter.cs b/skills/csharp/experimental/musicskill/Adapters/DefaultAdapter.cs index de4ac8914d..53d087553a 100644 --- a/skills/csharp/experimental/musicskill/Adapters/DefaultAdapter.cs +++ b/skills/csharp/experimental/musicskill/Adapters/DefaultAdapter.cs @@ -4,6 +4,7 @@ using System.Globalization; 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; @@ -19,6 +20,7 @@ public class DefaultAdapter : BotFrameworkHttpAdapter public DefaultAdapter( BotSettings settings, ICredentialProvider credentialProvider, + TelemetryInitializerMiddleware telemetryMiddleware, IBotTelemetryClient telemetryClient, ResponseManager responseManager) : base(credentialProvider) @@ -33,6 +35,7 @@ public DefaultAdapter( // Uncomment the following line for local development without Azure Storage // Use(new TranscriptLoggerMiddleware(new MemoryTranscriptStore())); + Use(telemetryMiddleware); Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container))); Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true)); Use(new ShowTypingMiddleware()); diff --git a/skills/csharp/experimental/musicskill/Bots/DefaultActivityHandler.cs b/skills/csharp/experimental/musicskill/Bots/DefaultActivityHandler.cs new file mode 100644 index 0000000000..d9b4fb9df3 --- /dev/null +++ b/skills/csharp/experimental/musicskill/Bots/DefaultActivityHandler.cs @@ -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 MusicSkill.Bots +{ + public class DefaultActivityHandler : ActivityHandler + where T : Dialog + { + private readonly Dialog _dialog; + private readonly BotState _conversationState; + private readonly BotState _userState; + private IStatePropertyAccessor _dialogStateAccessor; + + public DefaultActivityHandler(IServiceProvider serviceProvider, T dialog) + { + _dialog = dialog; + _conversationState = serviceProvider.GetService(); + _userState = serviceProvider.GetService(); + _dialogStateAccessor = _conversationState.CreateProperty(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 membersAdded, ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + + protected override Task OnMessageActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + + protected override Task OnEventActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken) + { + return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); + } + } +} \ No newline at end of file diff --git a/skills/csharp/experimental/musicskill/Bots/DialogBot.cs b/skills/csharp/experimental/musicskill/Bots/DialogBot.cs deleted file mode 100644 index 5d8069767b..0000000000 --- a/skills/csharp/experimental/musicskill/Bots/DialogBot.cs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -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 MusicSkill.Bots -{ - public class DialogBot : IBot - where T : Dialog - { - private readonly Dialog _dialog; - private readonly BotState _conversationState; - private readonly BotState _userState; - private readonly IBotTelemetryClient _telemetryClient; - - public DialogBot(IServiceProvider serviceProvider, T dialog) - { - _dialog = dialog; - _conversationState = serviceProvider.GetService(); - _userState = serviceProvider.GetService(); - _telemetryClient = serviceProvider.GetService(); - } - - public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) - { - // Client notifying this bot took to long to respond (timed out) - if (turnContext.Activity.Code == EndOfConversationCodes.BotTimedOut) - { - _telemetryClient.TrackTrace($"Timeout in {turnContext.Activity.ChannelId} channel: Bot took too long to respond.", Severity.Information, null); - return; - } - - await _dialog.RunAsync(turnContext, _conversationState.CreateProperty(nameof(DialogState)), cancellationToken); - - // Save any state changes that might have occured during the turn. - await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken); - await _userState.SaveChangesAsync(turnContext, false, cancellationToken); - } - } -} \ No newline at end of file diff --git a/skills/csharp/experimental/musicskill/Dialogs/MainDialog.cs b/skills/csharp/experimental/musicskill/Dialogs/MainDialog.cs index 308132a630..60b3fe3f89 100644 --- a/skills/csharp/experimental/musicskill/Dialogs/MainDialog.cs +++ b/skills/csharp/experimental/musicskill/Dialogs/MainDialog.cs @@ -22,7 +22,7 @@ namespace MusicSkill.Dialogs { - public class MainDialog : RouterDialog + public class MainDialog : ActivityHandlerDialog { private BotSettings _settings; private BotServices _services; @@ -53,13 +53,13 @@ public MainDialog( AddDialog(playMusicDialog); } - protected override async Task OnStartAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) + protected override async Task OnMembersAddedAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) { var locale = CultureInfo.CurrentUICulture; await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.WelcomeMessage)); } - protected override async Task RouteAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) + protected override async Task OnMessageActivityAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) { // get current activity locale var locale = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; @@ -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)) { var response = dc.Context.Activity.CreateReply(); response.Type = ActivityTypes.Handoff; @@ -113,7 +113,7 @@ public MainDialog( await dc.EndDialogAsync(result); } - protected override async Task OnEventAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) + protected override async Task OnEventActivityAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken)) { switch (dc.Context.Activity.Name) { @@ -190,15 +190,15 @@ public MainDialog( private async Task OnCancel(DialogContext dc) { await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.CancelMessage)); - await CompleteAsync(dc); + await OnDialogCompleteAsync(dc); await dc.CancelAllDialogsAsync(); - return InterruptionAction.StartedDialog; + return InterruptionAction.End; } private async Task OnHelp(DialogContext dc) { await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.HelpMessage)); - return InterruptionAction.MessageSentToUser; + return InterruptionAction.Resume; } private async Task OnLogout(DialogContext dc) @@ -225,7 +225,7 @@ private async Task OnLogout(DialogContext dc) await dc.Context.SendActivityAsync(_responseManager.GetResponse(MainResponses.LogOut)); - return InterruptionAction.StartedDialog; + return InterruptionAction.End; } private async Task PopulateStateFromSemanticAction(ITurnContext context) diff --git a/skills/csharp/experimental/musicskill/MusicSkill.csproj b/skills/csharp/experimental/musicskill/MusicSkill.csproj index 31ccc2a9fe..9d9970902f 100644 --- a/skills/csharp/experimental/musicskill/MusicSkill.csproj +++ b/skills/csharp/experimental/musicskill/MusicSkill.csproj @@ -36,8 +36,8 @@ - - + + diff --git a/skills/csharp/experimental/musicskill/Startup.cs b/skills/csharp/experimental/musicskill/Startup.cs index 26db64436f..543809a17e 100644 --- a/skills/csharp/experimental/musicskill/Startup.cs +++ b/skills/csharp/experimental/musicskill/Startup.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Bot.Builder; using Microsoft.Bot.Builder.ApplicationInsights; +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Bot.Builder.Azure; using Microsoft.Bot.Builder.BotFramework; using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core; @@ -66,9 +67,11 @@ public void ConfigureServices(IServiceCollection services) // Configure telemetry services.AddApplicationInsightsTelemetry(); - var telemetryClient = new BotTelemetryClient(new TelemetryClient()); - services.AddSingleton(telemetryClient); - services.AddBotApplicationInsights(telemetryClient); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); // Configure bot services services.AddSingleton(); @@ -109,7 +112,7 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton(); // Configure bot - services.AddTransient>(); + services.AddTransient>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -120,8 +123,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseDeveloperExceptionPage(); } - app.UseBotApplicationInsights() - .UseDefaultFiles() + app.UseDefaultFiles() .UseStaticFiles() .UseWebSockets() .UseMvc(); diff --git a/skills/csharp/tests/automotiveskill.tests/Flow/AutomotiveSkillTestBase.cs b/skills/csharp/tests/automotiveskill.tests/Flow/AutomotiveSkillTestBase.cs index 66475f42bb..4cb4d0403f 100644 --- a/skills/csharp/tests/automotiveskill.tests/Flow/AutomotiveSkillTestBase.cs +++ b/skills/csharp/tests/automotiveskill.tests/Flow/AutomotiveSkillTestBase.cs @@ -45,7 +45,7 @@ public override void Initialize() { "en", new CognitiveModelSet() { - LuisServices = new Dictionary + LuisServices = new Dictionary { { "General", new MockLuisRecognizer() }, { "Settings", new MockLuisRecognizer() }, @@ -80,7 +80,7 @@ public override void Initialize() Services.AddSingleton(); Services.AddTransient(); Services.AddTransient(); - Services.AddTransient>(); + Services.AddTransient>(); // Mock HttpContext for image path resolution var mockHttpContext = new DefaultHttpContext(); diff --git a/skills/csharp/tests/automotiveskill.tests/Flow/Fakes/MockLuisRecognizer.cs b/skills/csharp/tests/automotiveskill.tests/Flow/Fakes/MockLuisRecognizer.cs index 2c0e83d040..10ce1164f3 100644 --- a/skills/csharp/tests/automotiveskill.tests/Flow/Fakes/MockLuisRecognizer.cs +++ b/skills/csharp/tests/automotiveskill.tests/Flow/Fakes/MockLuisRecognizer.cs @@ -12,23 +12,26 @@ namespace AutomotiveSkill.Tests.Flow.Fakes { - public class MockLuisRecognizer : ITelemetryRecognizer + public class MockLuisRecognizer : LuisRecognizer { - public MockLuisRecognizer() + private static LuisApplication mockApplication = new LuisApplication() { - } - - public bool LogPersonalInformation { get; set; } = false; + ApplicationId = "testappid", + Endpoint = "testendpoint", + EndpointKey = "testendpointkey" + }; - public IBotTelemetryClient TelemetryClient { get; set; } = new NullBotTelemetryClient(); + public MockLuisRecognizer() + : base(application: mockApplication) + { + } - public Task RecognizeAsync(ITurnContext turnContext, CancellationToken cancellationToken) + public override Task RecognizeAsync(ITurnContext turnContext, CancellationToken cancellationToken) { throw new NotImplementedException(); } - public Task RecognizeAsync(ITurnContext turnContext, CancellationToken cancellationToken) - where T : IRecognizerConvert, new() + public override Task RecognizeAsync(ITurnContext turnContext, CancellationToken cancellationToken) { var mockResult = new T(); @@ -72,13 +75,12 @@ public Task RecognizeAsync(ITurnContext turnContext, CancellationToken can throw new NotImplementedException(); } - public Task RecognizeAsync(ITurnContext turnContext, Dictionary telemetryProperties, Dictionary telemetryMetrics, CancellationToken cancellationToken = default(CancellationToken)) + public override Task RecognizeAsync(ITurnContext turnContext, Dictionary telemetryProperties, Dictionary telemetryMetrics, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } - public Task RecognizeAsync(ITurnContext turnContext, Dictionary telemetryProperties, Dictionary telemetryMetrics, CancellationToken cancellationToken = default(CancellationToken)) - where T : IRecognizerConvert, new() + public override Task RecognizeAsync(ITurnContext turnContext, Dictionary telemetryProperties, Dictionary telemetryMetrics, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); }