Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in Command.cs #46

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions IntermediatorBotSample/Bot/IntermediatorBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -33,7 +34,7 @@ public async Task OnTurn(ITurnContext context)

Activity replyActivity = context.Activity.CreateReply();
replyActivity.Attachments = new List<Attachment>() { heroCard.ToAttachment() };
await context.SendActivity(replyActivity);
await context.SendActivityAsync(replyActivity);
}
}
}
5 changes: 4 additions & 1 deletion IntermediatorBotSample/CommandHandling/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
2 changes: 1 addition & 1 deletion IntermediatorBotSample/CommandHandling/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ await _connectionRequestHandler.AcceptOrRejectRequestAsync(

if (replyActivity != null)
{
await context.SendActivity(replyActivity);
await context.SendActivityAsync(replyActivity);
}

return wasHandled;
Expand Down
25 changes: 18 additions & 7 deletions IntermediatorBotSample/IntermediatorBotSample.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BotMessageRouting" Version="2.0.1-alpha" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.6" />
<PackageReference Include="Microsoft.Bot.Builder.Core" Version="4.0.1-preview" />
<PackageReference Include="Microsoft.Bot.Builder.Core.Extensions" Version="4.0.1-preview" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.0.1-preview" />
<PackageReference Include="BotMessageRouting" Version="2.0.5-alpha" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Bot.Builder" Version="4.2.2" />


<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.2.2" />
<PackageReference Include="Microsoft.Bot.Configuration" Version="4.2.2" />
<PackageReference Include="Microsoft.Bot.Connector" Version="4.2.2" />



<PackageReference Include="Microsoft.Bot.Schema" Version="4.2.2" />
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.2.0" />

</ItemGroup>

<ItemGroup>
Expand Down
44 changes: 44 additions & 0 deletions IntermediatorBotSample/Middleware/CatchExceptionMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Bot.Builder.Core.Extensions
{
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">
/// The type of the exception that you want to catch. This can be 'Exception' to
/// catch all or a specific type of exception
/// </typeparam>
public class CatchExceptionMiddleware<T> : IMiddleware where T : Exception
{
private readonly Func<ITurnContext, T, Task> _handler;

public CatchExceptionMiddleware(Func<ITurnContext, T, Task> 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);
}
}

}
}
9 changes: 7 additions & 2 deletions IntermediatorBotSample/Middleware/HandoffMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -148,6 +149,10 @@ public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next)
// Handle the result, if necessary
await MessageRouterResultHandler.HandleResultAsync(messageRouterResult);
}
else
{
await next(ct).ConfigureAwait(false);
}
}

/// <summary>
Expand Down
16 changes: 6 additions & 10 deletions IntermediatorBotSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,8 +52,8 @@ public void ConfigureServices(IServiceCollection services)
// an "Ooops" message is sent.
options.Middleware.Add(new CatchExceptionMiddleware<Exception>(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
Expand Down Expand Up @@ -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();
}
}
}