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

Commit e4e33d8

Browse files
xhr0804bobokids
authored andcommitted
[skills] update to ActivityHandler (#2667)
1 parent 2b0a7ee commit e4e33d8

23 files changed

+254
-201
lines changed

skills/csharp/experimental/automotiveskill/Adapters/AutomotiveSkillWebSocketBotAdapter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Bot.Builder;
88
using Microsoft.Bot.Builder.Azure;
99
using Microsoft.Bot.Builder.Dialogs;
10+
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
1011
using Microsoft.Bot.Builder.Skills;
1112
using Microsoft.Bot.Builder.Solutions.Middleware;
1213
using Microsoft.Bot.Builder.Solutions.Responses;
@@ -21,6 +22,7 @@ public AutomotiveSkillWebSocketBotAdapter(
2122
UserState userState,
2223
ConversationState conversationState,
2324
ResponseManager responseManager,
25+
TelemetryInitializerMiddleware telemetryMiddleware,
2426
IBotTelemetryClient telemetryClient)
2527
: base(null, telemetryClient)
2628
{
@@ -32,6 +34,7 @@ public AutomotiveSkillWebSocketBotAdapter(
3234
telemetryClient.TrackException(exception);
3335
};
3436

37+
Use(telemetryMiddleware);
3538
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
3639
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
3740
Use(new SetLocaleMiddleware(settings.DefaultLocale ?? "en-us"));

skills/csharp/experimental/automotiveskill/Adapters/DefaultAdapter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using AutomotiveSkill.Services;
77
using Microsoft.Bot.Builder;
88
using Microsoft.Bot.Builder.Azure;
9+
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
910
using Microsoft.Bot.Builder.Integration.AspNet.Core;
1011
using Microsoft.Bot.Builder.Solutions.Middleware;
1112
using Microsoft.Bot.Builder.Solutions.Responses;
@@ -19,6 +20,7 @@ public class DefaultAdapter : BotFrameworkHttpAdapter
1920
public DefaultAdapter(
2021
BotSettings settings,
2122
ICredentialProvider credentialProvider,
23+
TelemetryInitializerMiddleware telemetryMiddleware,
2224
IBotTelemetryClient telemetryClient,
2325
ResponseManager responseManager)
2426
: base(credentialProvider)
@@ -31,6 +33,7 @@ public DefaultAdapter(
3133
telemetryClient.TrackException(exception);
3234
};
3335

36+
Use(telemetryMiddleware);
3437
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
3538
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
3639
Use(new ShowTypingMiddleware());

skills/csharp/experimental/automotiveskill/AutomotiveSkill.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<PackageReference Include="Microsoft.Bot.Builder.Dialogs" Version="4.6.1" />
2828
<PackageReference Include="Microsoft.Bot.Builder.Integration.ApplicationInsights.Core" Version="4.6.1" />
2929
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.6.1" />
30-
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.5.4" />
31-
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.5.4" />
30+
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.6.0-preview-rc2" />
31+
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.6.0-preview-rc2" />
3232
<PackageReference Include="Microsoft.Bot.Builder.TemplateManager" Version="4.6.1" />
3333
<PackageReference Include="Microsoft.Bot.Configuration" Version="4.6.1" />
3434
<PackageReference Include="Microsoft.Bot.Connector" Version="4.6.1" />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.Bot.Builder;
9+
using Microsoft.Bot.Builder.Dialogs;
10+
using Microsoft.Bot.Schema;
11+
using Microsoft.Extensions.DependencyInjection;
12+
13+
namespace AutomotiveSkill.Bots
14+
{
15+
public class DefaultActivityHandler<T> : ActivityHandler
16+
where T : Dialog
17+
{
18+
private readonly Dialog _dialog;
19+
private readonly BotState _conversationState;
20+
private readonly BotState _userState;
21+
private IStatePropertyAccessor<DialogState> _dialogStateAccessor;
22+
23+
public DefaultActivityHandler(IServiceProvider serviceProvider, T dialog)
24+
{
25+
_dialog = dialog;
26+
_conversationState = serviceProvider.GetService<ConversationState>();
27+
_userState = serviceProvider.GetService<UserState>();
28+
_dialogStateAccessor = _conversationState.CreateProperty<DialogState>(nameof(DialogState));
29+
}
30+
31+
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
32+
{
33+
await base.OnTurnAsync(turnContext, cancellationToken);
34+
35+
// Save any state changes that might have occured during the turn.
36+
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
37+
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
38+
}
39+
40+
protected override Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
41+
{
42+
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
43+
}
44+
45+
protected override Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
46+
{
47+
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
48+
}
49+
50+
protected override Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
51+
{
52+
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
53+
}
54+
}
55+
}

skills/csharp/experimental/automotiveskill/Bots/DialogBot.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

skills/csharp/experimental/automotiveskill/Dialogs/MainDialog.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace AutomotiveSkill.Dialogs
2525
{
26-
public class MainDialog : RouterDialog
26+
public class MainDialog : ActivityHandlerDialog
2727
{
2828
private BotServices _services;
2929
private ResponseManager _responseManager;
@@ -50,13 +50,13 @@ public MainDialog(
5050
AddDialog(vehicleSettingsDialog ?? throw new ArgumentNullException(nameof(vehicleSettingsDialog)));
5151
}
5252

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

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

@@ -105,7 +105,7 @@ public MainDialog(
105105
}
106106
}
107107

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

173-
await CompleteAsync(dc);
173+
await OnDialogCompleteAsync(dc);
174174
await dc.CancelAllDialogsAsync();
175-
return InterruptionAction.StartedDialog;
175+
return InterruptionAction.End;
176176
}
177177

178178
private async Task<InterruptionAction> OnHelp(DialogContext dc)
179179
{
180180
await dc.Context.SendActivityAsync(_responseManager.GetResponse(AutomotiveSkillMainResponses.HelpMessage));
181-
return InterruptionAction.MessageSentToUser;
181+
return InterruptionAction.Resume;
182182
}
183183
}
184184
}

skills/csharp/experimental/automotiveskill/Startup.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace AutomotiveSkill
1212
using AutomotiveSkill.Responses.VehicleSettings;
1313
using AutomotiveSkill.Services;
1414
using Microsoft.ApplicationInsights;
15+
using Microsoft.ApplicationInsights.Extensibility;
1516
using Microsoft.AspNetCore.Builder;
1617
using Microsoft.AspNetCore.Hosting;
1718
using Microsoft.Bot.Builder;
@@ -82,9 +83,11 @@ public void ConfigureServices(IServiceCollection services)
8283

8384
// Configure telemetry
8485
services.AddApplicationInsightsTelemetry();
85-
var telemetryClient = new BotTelemetryClient(new TelemetryClient());
86-
services.AddSingleton<IBotTelemetryClient>(telemetryClient);
87-
services.AddBotApplicationInsights(telemetryClient);
86+
services.AddSingleton<IBotTelemetryClient, BotTelemetryClient>();
87+
services.AddSingleton<ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();
88+
services.AddSingleton<ITelemetryInitializer, TelemetryBotIdInitializer>();
89+
services.AddSingleton<TelemetryInitializerMiddleware>();
90+
services.AddSingleton<TelemetryLoggerMiddleware>();
8891

8992
// Configure bot services
9093
services.AddSingleton<BotServices>();
@@ -114,7 +117,7 @@ public void ConfigureServices(IServiceCollection services)
114117

115118
// Configure bot
116119
services.AddTransient<MainDialog>();
117-
services.AddTransient<IBot, DialogBot<MainDialog>>();
120+
services.AddTransient<IBot, DefaultActivityHandler<MainDialog>>();
118121
}
119122

120123
/// <summary>
@@ -125,8 +128,7 @@ public void ConfigureServices(IServiceCollection services)
125128
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
126129
{
127130
_isProduction = env.IsProduction();
128-
app.UseBotApplicationInsights()
129-
.UseDefaultFiles()
131+
app.UseDefaultFiles()
130132
.UseStaticFiles()
131133
.UseWebSockets()
132134
.UseMvc();

skills/csharp/experimental/bingsearchskill/Adapters/CustomSkillAdapter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using BingSearchSkill.Services;
77
using Microsoft.Bot.Builder;
88
using Microsoft.Bot.Builder.Azure;
9+
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
910
using Microsoft.Bot.Builder.Dialogs;
1011
using Microsoft.Bot.Builder.Skills;
1112
using Microsoft.Bot.Builder.Solutions.Middleware;
@@ -20,6 +21,7 @@ public CustomSkillAdapter(
2021
BotSettings settings,
2122
UserState userState,
2223
ConversationState conversationState,
24+
TelemetryInitializerMiddleware telemetryMiddleware,
2325
ResponseManager responseManager,
2426
IBotTelemetryClient telemetryClient)
2527
: base(null, telemetryClient)
@@ -32,6 +34,7 @@ public CustomSkillAdapter(
3234
telemetryClient.TrackException(exception);
3335
};
3436

37+
Use(telemetryMiddleware);
3538
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
3639
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
3740
Use(new ShowTypingMiddleware());

skills/csharp/experimental/bingsearchskill/Adapters/DefaultAdapter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using BingSearchSkill.Services;
77
using Microsoft.Bot.Builder;
88
using Microsoft.Bot.Builder.Azure;
9+
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
910
using Microsoft.Bot.Builder.Integration.AspNet.Core;
1011
using Microsoft.Bot.Builder.Solutions.Middleware;
1112
using Microsoft.Bot.Builder.Solutions.Responses;
@@ -19,6 +20,7 @@ public class DefaultAdapter : BotFrameworkHttpAdapter
1920
public DefaultAdapter(
2021
BotSettings settings,
2122
ICredentialProvider credentialProvider,
23+
TelemetryInitializerMiddleware telemetryMiddleware,
2224
IBotTelemetryClient telemetryClient,
2325
ResponseManager responseManager)
2426
: base(credentialProvider)
@@ -31,6 +33,7 @@ public DefaultAdapter(
3133
telemetryClient.TrackException(exception);
3234
};
3335

36+
Use(telemetryMiddleware);
3437
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
3538
Use(new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true));
3639
Use(new ShowTypingMiddleware());

skills/csharp/experimental/bingsearchskill/BingSearchSkill.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
<PackageReference Include="Microsoft.Bot.Builder.Dialogs" Version="4.6.1" />
7676
<PackageReference Include="Microsoft.Bot.Builder.Integration.ApplicationInsights.Core" Version="4.6.1" />
7777
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.6.1" />
78-
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.5.4" />
79-
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.5.4" />
78+
<PackageReference Include="Microsoft.Bot.Builder.Skills" Version="4.6.0-preview-rc2" />
79+
<PackageReference Include="Microsoft.Bot.Builder.Solutions" Version="4.6.0-preview-rc2" />
8080
<PackageReference Include="Microsoft.Bot.Builder.TemplateManager" Version="4.6.1" />
8181
<PackageReference Include="Microsoft.Bot.Configuration" Version="4.6.1" />
8282
<PackageReference Include="Microsoft.Bot.Connector" Version="4.6.1" />

0 commit comments

Comments
 (0)