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

update startup for skills to add middlewares for skilladapter #1040

Merged
merged 4 commits into from
Mar 31, 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 @@ -42,7 +42,7 @@ public async Task BotMessage()
/// This API is the endpoint the bot exposes as skill
/// </summary>
/// <returns></returns>
[Route("api/skills")]
[Route("api/skill/messages")]
[HttpPost]
public async Task SkillMessage()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.8.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.5.1" />
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.8.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.3" />
<PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.ContentModerator" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.Language" Version="1.0.1-preview" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.3.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.Skills;

namespace NewsSkill.Controllers
{
[ApiController]
public class BotController : SkillController
{
public BotController(IAdapterIntegration adapter, ISkillAdapter skillAdapter, IBot bot)
: base(adapter, skillAdapter, bot)
public BotController(IBotFrameworkHttpAdapter botFrameworkHttpAdapter, SkillAdapter skillAdapter, IBot bot)
: base(botFrameworkHttpAdapter, skillAdapter, bot)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.5.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.3" />
<PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.Search.NewsSearch" Version="2.0.0" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.3.1" />
<PackageReference Include="Microsoft.Bot.Builder.AI.Luis" Version="4.3.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
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.Skills;
using Microsoft.Bot.Builder.Solutions.Middleware;
using Microsoft.Bot.Builder.Solutions.Proactive;
using Microsoft.Bot.Builder.Solutions.Skills;
using Microsoft.Bot.Builder.Solutions.TaskExtensions;
Expand Down Expand Up @@ -50,8 +52,6 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2);

services.AddSingleton<ISkillAdapter, SkillAdapter>();

// add background task queue
services.AddSingleton<IBackgroundTaskQueue, BackgroundTaskQueue>();
services.AddHostedService<QueuedHostedService>();
Expand Down Expand Up @@ -104,37 +104,68 @@ public void ConfigureServices(IServiceCollection services)

services.AddSingleton(endpointService);

// Add the bot with options
services.AddBot<NewsSkill>(options =>
{
options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);
var defaultLocale = Configuration.GetSection("defaultLocale").Get<string>();

// Add the bot
services.AddTransient<IBot, NewsSkill>();

var credentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);

// Telemetry Middleware (logs activity messages in Application Insights)
var sp = services.BuildServiceProvider();
var telemetryClient = sp.GetService<IBotTelemetryClient>();
var appInsightsLogger = new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true);
options.Middleware.Add(appInsightsLogger);
// Create the middlewares
var telemetryClient = services.BuildServiceProvider().GetService<IBotTelemetryClient>();
var appInsightsLogger = new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true);

var storageService = botConfig.Services.FirstOrDefault(s => s.Type == ServiceTypes.BlobStorage) ?? throw new Exception("Please configure your Azure Storage service in your .bot file.");
var blobStorage = storageService as BlobStorageService;
var transcriptStore = new AzureBlobTranscriptStore(blobStorage.ConnectionString, blobStorage.Container);
var transcriptMiddleware = new TranscriptLoggerMiddleware(transcriptStore);

var typingMiddleware = new ShowTypingMiddleware();
var setLocaleMiddleware = new SetLocaleMiddleware(defaultLocale ?? "en-us");
var eventDebuggerMiddleware = new EventDebuggerMiddleware();
var autoSaveStateMiddleware = new AutoSaveStateMiddleware(userState, conversationState);

Func<ITurnContext, Exception, Task> onTurnError = async (context, exception) =>
{
await context.SendActivityAsync(MainStrings.ERROR);
await context.SendActivityAsync(new Activity(type: ActivityTypes.Trace, text: $"News Skill Error: {exception.Message} | {exception.StackTrace}"));
telemetryClient.TrackExceptionEx(exception, context.Activity);
};

// Catches any errors that occur during a conversation turn and logs them to AppInsights.
options.OnTurnError = async (context, exception) =>
// Add the http adapter with middlewares
services.AddTransient<IBotFrameworkHttpAdapter>(sp =>
{
var botFrameworkHttpAdapter = new BotFrameworkHttpAdapter(credentialProvider)
{
await context.SendActivityAsync(MainStrings.ERROR);
await context.SendActivityAsync(new Activity(type: ActivityTypes.Trace, text: $"News Skill Error: {exception.Message} | {exception.StackTrace}"));
telemetryClient.TrackExceptionEx(exception, context.Activity);
OnTurnError = onTurnError
};

// Transcript Middleware (saves conversation history in a standard format)
var storageService = botConfig.Services.FirstOrDefault(s => s.Type == ServiceTypes.BlobStorage) ?? throw new Exception("Please configure your Azure Storage service in your .bot file.");
var blobStorage = storageService as BlobStorageService;
var transcriptStore = new AzureBlobTranscriptStore(blobStorage.ConnectionString, blobStorage.Container);
var transcriptMiddleware = new TranscriptLoggerMiddleware(transcriptStore);
options.Middleware.Add(transcriptMiddleware);
botFrameworkHttpAdapter.Use(appInsightsLogger);
botFrameworkHttpAdapter.Use(transcriptMiddleware);
botFrameworkHttpAdapter.Use(typingMiddleware);
botFrameworkHttpAdapter.Use(setLocaleMiddleware);
botFrameworkHttpAdapter.Use(eventDebuggerMiddleware);
botFrameworkHttpAdapter.Use(autoSaveStateMiddleware);

return botFrameworkHttpAdapter;
});

// Add the SkillAdapter with middlewares
services.AddTransient(sp =>
{
var skillAdapter = new SkillAdapter(credentialProvider)
{
OnTurnError = onTurnError
};

// Typing Middleware (automatically shows typing when the bot is responding/working)
var typingMiddleware = new ShowTypingMiddleware();
options.Middleware.Add(typingMiddleware);
skillAdapter.Use(appInsightsLogger);
skillAdapter.Use(transcriptMiddleware);
skillAdapter.Use(typingMiddleware);
skillAdapter.Use(setLocaleMiddleware);
skillAdapter.Use(eventDebuggerMiddleware);
skillAdapter.Use(autoSaveStateMiddleware);

options.Middleware.Add(new AutoSaveStateMiddleware(userState, conversationState));
return skillAdapter;
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.Skills;

namespace RestaurantBooking.Controllers
{
[ApiController]
public class BotController : SkillController
{
public BotController(IAdapterIntegration adapter, ISkillAdapter skillAdapter, IBot bot)
: base(adapter, skillAdapter, bot)
public BotController(IBotFrameworkHttpAdapter botFrameworkHttpAdapter, SkillAdapter skillAdapter, IBot bot)
: base(botFrameworkHttpAdapter, skillAdapter, bot)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.5.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.3" />
<PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.3.1" />
<PackageReference Include="Microsoft.Bot.Builder.AI.Luis" Version="4.3.1" />
<PackageReference Include="Microsoft.Bot.Builder.AI.QnA" Version="4.3.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand All @@ -12,6 +13,7 @@
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Builder.Solutions.Middleware;
using Microsoft.Bot.Builder.Solutions.Proactive;
using Microsoft.Bot.Builder.Solutions.Responses;
using Microsoft.Bot.Builder.Solutions.Skills;
Expand Down Expand Up @@ -53,8 +55,6 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2);

services.AddSingleton<ISkillAdapter, SkillAdapter>();

// add background task queue
services.AddSingleton<IBackgroundTaskQueue, BackgroundTaskQueue>();
services.AddHostedService<QueuedHostedService>();
Expand Down Expand Up @@ -86,6 +86,8 @@ public void ConfigureServices(IServiceCollection services)
// Register bot responses for all supported languages.
services.AddSingleton(sp => responseManager);

var defaultLocale = Configuration.GetSection("defaultLocale").Get<string>();

// Initialize Bot State
var cosmosDbService = botConfig.Services.FirstOrDefault(s => s.Type == ServiceTypes.CosmosDB) ?? throw new Exception("Please configure your CosmosDb service in your .bot file.");
var cosmosDb = cosmosDbService as CosmosDbService;
Expand Down Expand Up @@ -122,37 +124,66 @@ public void ConfigureServices(IServiceCollection services)
// HttpContext required for path resolution
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

// Add the bot with options
services.AddBot<RestaurantBooking>(options =>
{
options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);
// Add the bot
services.AddTransient<IBot, RestaurantBooking>();

var credentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);

// Telemetry Middleware (logs activity messages in Application Insights)
var sp = services.BuildServiceProvider();
var telemetryClient = sp.GetService<IBotTelemetryClient>();
var appInsightsLogger = new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true);
options.Middleware.Add(appInsightsLogger);
// Create the middlewares
var telemetryClient = services.BuildServiceProvider().GetService<IBotTelemetryClient>();
var appInsightsLogger = new TelemetryLoggerMiddleware(telemetryClient, logPersonalInformation: true);

var storageService = botConfig.Services.FirstOrDefault(s => s.Type == ServiceTypes.BlobStorage) ?? throw new Exception("Please configure your Azure Storage service in your .bot file.");
var blobStorage = storageService as BlobStorageService;
var transcriptStore = new AzureBlobTranscriptStore(blobStorage.ConnectionString, blobStorage.Container);
var transcriptMiddleware = new TranscriptLoggerMiddleware(transcriptStore);

var typingMiddleware = new ShowTypingMiddleware();
var setLocaleMiddleware = new SetLocaleMiddleware(defaultLocale ?? "en-us");
var eventDebuggerMiddleware = new EventDebuggerMiddleware();
var autoSaveStateMiddleware = new AutoSaveStateMiddleware(userState, conversationState);

Func<ITurnContext, Exception, Task> onTurnError = async (context, exception) =>
{
await context.SendActivityAsync(context.Activity.CreateReply(RestaurantBookingSharedResponses.ErrorMessage));
await context.SendActivityAsync(new Activity(type: ActivityTypes.Trace, text: $"Skill Error: {exception.Message} | {exception.StackTrace}"));
telemetryClient.TrackExceptionEx(exception, context.Activity);
};

// Catches any errors that occur during a conversation turn and logs them to AppInsights.
options.OnTurnError = async (context, exception) =>
// Add the http adapter with middlewares
services.AddTransient<IBotFrameworkHttpAdapter>(sp =>
{
var botFrameworkHttpAdapter = new BotFrameworkHttpAdapter(credentialProvider)
{
await context.SendActivityAsync(context.Activity.CreateReply(RestaurantBookingSharedResponses.ErrorMessage));
await context.SendActivityAsync(new Activity(type: ActivityTypes.Trace, text: $"Skill Error: {exception.Message} | {exception.StackTrace}"));
telemetryClient.TrackExceptionEx(exception, context.Activity);
OnTurnError = onTurnError
};

// Transcript Middleware (saves conversation history in a standard format)
var storageService = botConfig.Services.FirstOrDefault(s => s.Type == ServiceTypes.BlobStorage) ?? throw new Exception("Please configure your Azure Storage service in your .bot file.");
var blobStorage = storageService as BlobStorageService;
var transcriptStore = new AzureBlobTranscriptStore(blobStorage.ConnectionString, blobStorage.Container);
var transcriptMiddleware = new TranscriptLoggerMiddleware(transcriptStore);
options.Middleware.Add(transcriptMiddleware);
botFrameworkHttpAdapter.Use(appInsightsLogger);
botFrameworkHttpAdapter.Use(transcriptMiddleware);
botFrameworkHttpAdapter.Use(typingMiddleware);
botFrameworkHttpAdapter.Use(setLocaleMiddleware);
botFrameworkHttpAdapter.Use(eventDebuggerMiddleware);
botFrameworkHttpAdapter.Use(autoSaveStateMiddleware);

return botFrameworkHttpAdapter;
});

// Add the SkillAdapter with middlewares
services.AddTransient(sp =>
{
var skillAdapter = new SkillAdapter(credentialProvider)
{
OnTurnError = onTurnError
};

// Typing Middleware (automatically shows typing when the bot is responding/working)
var typingMiddleware = new ShowTypingMiddleware();
options.Middleware.Add(typingMiddleware);
skillAdapter.Use(appInsightsLogger);
skillAdapter.Use(transcriptMiddleware);
skillAdapter.Use(typingMiddleware);
skillAdapter.Use(setLocaleMiddleware);
skillAdapter.Use(eventDebuggerMiddleware);
skillAdapter.Use(autoSaveStateMiddleware);

options.Middleware.Add(new AutoSaveStateMiddleware(userState, conversationState));
return skillAdapter;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.5.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.3" />
<PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.ContentModerator" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.Language" Version="1.0.1-preview" />
<PackageReference Include="Microsoft.Bot.Builder" Version="4.3.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.Skills;

namespace AutomotiveSkill.Controllers
{
[ApiController]
public class BotController : SkillController
{
public BotController(IAdapterIntegration adapter, ISkillAdapter skillAdapter, IBot bot)
: base(adapter, skillAdapter, bot)
public BotController(IBotFrameworkHttpAdapter botFrameworkHttpAdapter, SkillAdapter skillAdapter, IBot bot)
: base(botFrameworkHttpAdapter, skillAdapter, bot)
{
}
}
Expand Down
Loading