From 7016df647293e29dbc0a1742d7bf23fd154585e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Thu, 29 Nov 2018 13:58:18 +0100 Subject: [PATCH 1/9] bumped to netcoreapp2.1, and upgraded to latest versions. demands some reference to newer version of BotMessageRouting. see other project... --- .../Bot/IntermediatorBot.cs | 5 ++- .../CommandHandling/CommandHandler.cs | 2 +- .../IntermediatorBotSample.csproj | 25 ++++++++--- .../Middleware/CatchExceptionMiddleware.cs | 44 +++++++++++++++++++ .../Middleware/HandoffMiddleware.cs | 5 ++- IntermediatorBotSample/Startup.cs | 16 +++---- 6 files changed, 75 insertions(+), 22 deletions(-) create mode 100644 IntermediatorBotSample/Middleware/CatchExceptionMiddleware.cs diff --git a/IntermediatorBotSample/Bot/IntermediatorBot.cs b/IntermediatorBotSample/Bot/IntermediatorBot.cs index 2ce893d..189b599 100644 --- a/IntermediatorBotSample/Bot/IntermediatorBot.cs +++ b/IntermediatorBotSample/Bot/IntermediatorBot.cs @@ -3,6 +3,7 @@ using Microsoft.Bot.Builder; using Microsoft.Bot.Schema; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace IntermediatorBotSample.Bot @@ -11,7 +12,7 @@ public class IntermediatorBot : IBot { private const string SampleUrl = "https://github.com/tompaana/intermediator-bot-sample"; - public async Task OnTurn(ITurnContext context) + public async Task OnTurnAsync(ITurnContext context, CancellationToken ct) { Command showOptionsCommand = new Command(Commands.ShowOptions); @@ -33,7 +34,7 @@ public async Task OnTurn(ITurnContext context) Activity replyActivity = context.Activity.CreateReply(); replyActivity.Attachments = new List() { heroCard.ToAttachment() }; - await context.SendActivity(replyActivity); + await context.SendActivityAsync(replyActivity); } } } diff --git a/IntermediatorBotSample/CommandHandling/CommandHandler.cs b/IntermediatorBotSample/CommandHandling/CommandHandler.cs index b67f433..51cd1d5 100644 --- a/IntermediatorBotSample/CommandHandling/CommandHandler.cs +++ b/IntermediatorBotSample/CommandHandling/CommandHandler.cs @@ -266,7 +266,7 @@ await _connectionRequestHandler.AcceptOrRejectRequestAsync( if (replyActivity != null) { - await context.SendActivity(replyActivity); + await context.SendActivityAsync(replyActivity); } return wasHandled; diff --git a/IntermediatorBotSample/IntermediatorBotSample.csproj b/IntermediatorBotSample/IntermediatorBotSample.csproj index 3c91270..e42e647 100644 --- a/IntermediatorBotSample/IntermediatorBotSample.csproj +++ b/IntermediatorBotSample/IntermediatorBotSample.csproj @@ -1,15 +1,26 @@ - + - netcoreapp2.0 + netcoreapp2.1 - - - - - + + + + + + + + + + + + + + + + diff --git a/IntermediatorBotSample/Middleware/CatchExceptionMiddleware.cs b/IntermediatorBotSample/Middleware/CatchExceptionMiddleware.cs new file mode 100644 index 0000000..99b57ff --- /dev/null +++ b/IntermediatorBotSample/Middleware/CatchExceptionMiddleware.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.Bot.Builder.Core.Extensions +{ + /// + /// This piece of middleware can be added to allow you to handle exceptions when they are thrown + /// within your bot's code or middleware further down the pipeline. Using this handler you might + /// send an appropriate message to the user to let them know that something has gone wrong. + /// You can specify the type of exception the middleware should catch and this middleware can be added + /// multiple times to allow you to handle different exception types in different ways. + /// + /// + /// The type of the exception that you want to catch. This can be 'Exception' to + /// catch all or a specific type of exception + /// + public class CatchExceptionMiddleware : IMiddleware where T : Exception + { + private readonly Func _handler; + + public CatchExceptionMiddleware(Func callOnException) + { + _handler = callOnException; + } + + public async Task OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken)) + { + try + { + // Continue to route the activity through the pipeline + // any errors further down the pipeline will be caught by + // this try / catch + await next(cancellationToken).ConfigureAwait(false); + } + catch (T ex) + { + // If an error is thrown and the exception is of type T then invoke the handler + await _handler.Invoke(context, ex).ConfigureAwait(false); + } + } + + } +} diff --git a/IntermediatorBotSample/Middleware/HandoffMiddleware.cs b/IntermediatorBotSample/Middleware/HandoffMiddleware.cs index 645116f..e51e6c8 100644 --- a/IntermediatorBotSample/Middleware/HandoffMiddleware.cs +++ b/IntermediatorBotSample/Middleware/HandoffMiddleware.cs @@ -6,6 +6,7 @@ using Microsoft.Bot.Schema; using Microsoft.Extensions.Configuration; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Underscore.Bot.MessageRouting; using Underscore.Bot.MessageRouting.DataStore; @@ -89,7 +90,7 @@ public HandoffMiddleware(IConfiguration configuration) MessageLogs = new MessageLogs(connectionString); } - public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) + public async Task OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken ct) { Activity activity = context.Activity; @@ -134,7 +135,7 @@ public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) else { // No action taken - this middleware did not consume the activity so let it propagate - await next().ConfigureAwait(false); + await next(ct).ConfigureAwait(false); } } } diff --git a/IntermediatorBotSample/Startup.cs b/IntermediatorBotSample/Startup.cs index 0179b49..31ce4fe 100644 --- a/IntermediatorBotSample/Startup.cs +++ b/IntermediatorBotSample/Startup.cs @@ -2,8 +2,10 @@ using IntermediatorBotSample.Middleware; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Bot.Builder.BotFramework; +using Microsoft.Bot.Builder; using Microsoft.Bot.Builder.Core.Extensions; +using Microsoft.Bot.Builder.BotFramework; +using Microsoft.Bot.Builder.Integration; using Microsoft.Bot.Builder.Integration.AspNet.Core; using Microsoft.Bot.Builder.TraceExtensions; using Microsoft.Extensions.Configuration; @@ -50,8 +52,8 @@ public void ConfigureServices(IServiceCollection services) // an "Ooops" message is sent. options.Middleware.Add(new CatchExceptionMiddleware(async (context, exception) => { - await context.TraceActivity("Bot Exception", exception); - await context.SendActivity($"Sorry, it looks like something went wrong: {exception.Message}"); + await context.TraceActivityAsync("Bot Exception", exception); + await context.SendActivityAsync($"Sorry, it looks like something went wrong: {exception.Message}"); })); // The Memory Storage used here is for local bot debugging only. When the bot @@ -94,13 +96,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseDefaultFiles() .UseStaticFiles() .UseMvc() // Required Razor pages - .UseBotFramework(bot => - { - // This is how you can define a custom endpoint in case you're unhappy with - // the default "/api/messages": - bot.BasePath = Configuration["BotBasePath"]; - bot.MessagesPath = Configuration["BotMessagesPath"]; - }); + .UseBotFramework(); } } } From 8f56ce18a2aaf7c6712aadea631689e1bf683308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Thu, 6 Dec 2018 09:32:56 +0100 Subject: [PATCH 2/9] updated to netcoreapp2.2, aspnetcore 2.2 --- IntermediatorBotSample/IntermediatorBotSample.csproj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/IntermediatorBotSample/IntermediatorBotSample.csproj b/IntermediatorBotSample/IntermediatorBotSample.csproj index e42e647..df4cf78 100644 --- a/IntermediatorBotSample/IntermediatorBotSample.csproj +++ b/IntermediatorBotSample/IntermediatorBotSample.csproj @@ -1,13 +1,13 @@  - netcoreapp2.1 + netcoreapp2.2 - - - + + + @@ -19,7 +19,7 @@ - + From 247e5f15bc9975730b25b9f007e95603e66214f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Thu, 17 Jan 2019 17:19:26 +0100 Subject: [PATCH 3/9] Bumped updates again --- IntermediatorBotSample/IntermediatorBotSample.csproj | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/IntermediatorBotSample/IntermediatorBotSample.csproj b/IntermediatorBotSample/IntermediatorBotSample.csproj index df4cf78..eecfc2d 100644 --- a/IntermediatorBotSample/IntermediatorBotSample.csproj +++ b/IntermediatorBotSample/IntermediatorBotSample.csproj @@ -7,18 +7,18 @@ - + - + - - - + + + - + From c461b3ccd9e3bd2ffb6838e09a7d3bc0ca8996a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Thu, 17 Jan 2019 17:23:13 +0100 Subject: [PATCH 4/9] Another bump in the update --- IntermediatorBotSample/IntermediatorBotSample.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IntermediatorBotSample/IntermediatorBotSample.csproj b/IntermediatorBotSample/IntermediatorBotSample.csproj index eecfc2d..e58b1b0 100644 --- a/IntermediatorBotSample/IntermediatorBotSample.csproj +++ b/IntermediatorBotSample/IntermediatorBotSample.csproj @@ -5,7 +5,7 @@ - + From a6c4fe2da2074c179ebc37b1f6f6cb2a1893560c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Fri, 18 Jan 2019 12:18:10 +0100 Subject: [PATCH 5/9] removed version and moved from .all -> .app as "recommended" --- IntermediatorBotSample/IntermediatorBotSample.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IntermediatorBotSample/IntermediatorBotSample.csproj b/IntermediatorBotSample/IntermediatorBotSample.csproj index e58b1b0..1741932 100644 --- a/IntermediatorBotSample/IntermediatorBotSample.csproj +++ b/IntermediatorBotSample/IntermediatorBotSample.csproj @@ -7,7 +7,7 @@ - + From 36d72cf4d6615a01c732c0752035dfa1ec76df79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Fri, 18 Jan 2019 12:31:44 +0100 Subject: [PATCH 6/9] removed version and moved from .all -> .app as "recommended" --- IntermediatorBotSample/IntermediatorBotSample.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IntermediatorBotSample/IntermediatorBotSample.csproj b/IntermediatorBotSample/IntermediatorBotSample.csproj index 1741932..1eac088 100644 --- a/IntermediatorBotSample/IntermediatorBotSample.csproj +++ b/IntermediatorBotSample/IntermediatorBotSample.csproj @@ -5,7 +5,7 @@ - + From 169a3eaa42d24b49f287f77ec11c9051e45022f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Sun, 3 Feb 2019 17:22:54 +0100 Subject: [PATCH 7/9] Updated versions --- IntermediatorBotSample/IntermediatorBotSample.csproj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/IntermediatorBotSample/IntermediatorBotSample.csproj b/IntermediatorBotSample/IntermediatorBotSample.csproj index 1eac088..419b9f5 100644 --- a/IntermediatorBotSample/IntermediatorBotSample.csproj +++ b/IntermediatorBotSample/IntermediatorBotSample.csproj @@ -9,16 +9,16 @@ - + - - - + + + - + From 4fcb5ee8e40f4fdd7989994616a51aefa25d7a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Sun, 3 Mar 2019 16:30:39 +0100 Subject: [PATCH 8/9] Enum.Tryparse(..) returns true for all numbers. Need to actually verify each version as defined --- IntermediatorBotSample/CommandHandling/Command.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/IntermediatorBotSample/CommandHandling/Command.cs b/IntermediatorBotSample/CommandHandling/Command.cs index c757c20..17a534d 100644 --- a/IntermediatorBotSample/CommandHandling/Command.cs +++ b/IntermediatorBotSample/CommandHandling/Command.cs @@ -262,7 +262,10 @@ public static Commands StringToCommand(string commandAsString) { if (Enum.TryParse(commandAsString, out Commands result)) { - return result; + if (Enum.IsDefined(typeof(Commands), commandAsString)) + { + return result; + } } foreach (Commands command in Enum.GetValues(typeof(Commands))) From c801e6a5a6f552970443783589716933984b0719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20Ren=C3=A9=20Urholm?= Date: Sun, 3 Mar 2019 20:51:37 +0100 Subject: [PATCH 9/9] In HandoffMiddelware.cs there is a dangling else on the if branch related to only handling ActivityType.Messages. --- IntermediatorBotSample/Middleware/HandoffMiddleware.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/IntermediatorBotSample/Middleware/HandoffMiddleware.cs b/IntermediatorBotSample/Middleware/HandoffMiddleware.cs index e51e6c8..ec10d32 100644 --- a/IntermediatorBotSample/Middleware/HandoffMiddleware.cs +++ b/IntermediatorBotSample/Middleware/HandoffMiddleware.cs @@ -149,6 +149,10 @@ public async Task OnTurnAsync(ITurnContext context, NextDelegate next, Cancellat // Handle the result, if necessary await MessageRouterResultHandler.HandleResultAsync(messageRouterResult); } + else + { + await next(ct).ConfigureAwait(false); + } } ///