Skip to content

Commit

Permalink
Merge pull request #1294 from Microsoft/johtaylo/core-bot-feedback
Browse files Browse the repository at this point in the history
incorporate feedback
  • Loading branch information
johnataylor authored Mar 6, 2019
2 parents 8cd5dc2 + 3e520d7 commit 4e9964b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 41 deletions.
8 changes: 4 additions & 4 deletions samples/csharp_dotnetcore/13.basic-bot/Bots/DialogBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace Microsoft.BotBuilderSamples
// and the requirement is that all BotState objects are saved at the end of a turn.
public class DialogBot<T> : ActivityHandler where T : Dialog
{
private BotState _conversationState;
private BotState _userState;
private Dialog _dialog;
private ILogger _logger;
private readonly Dialog _dialog;
protected readonly BotState _conversationState;
protected readonly BotState _userState;
protected readonly ILogger _logger;

public DialogBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Microsoft.BotBuilderSamples
[ApiController]
public class BotController : ControllerBase
{
private IBotFrameworkHttpAdapter _adapter;
private IBot _bot;
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly IBot _bot;

public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
Expand Down
12 changes: 9 additions & 3 deletions samples/csharp_dotnetcore/13.basic-bot/Dialogs/MainDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Recognizers.Text.DataTypes.TimexExpression;

namespace Microsoft.BotBuilderSamples
{
public class MainDialog : ComponentDialog
{
private IConfiguration _configuration;
protected readonly IConfiguration _configuration;
protected readonly ILogger _logger;

public MainDialog(IConfiguration configuration)
public MainDialog(IConfiguration configuration, ILogger<MainDialog> logger)
: base(nameof(MainDialog))
{
_configuration = configuration;
_logger = logger;

AddDialog(new TextPrompt(nameof(TextPrompt)));
AddDialog(new BookingDialog());
Expand All @@ -42,7 +45,10 @@ private async Task<DialogTurnResult> IntroStepAsync(WaterfallStepContext stepCon
private async Task<DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Call LUIS and gather any potential booking details.
var bookingDetails = await LuisHelper.ExecuteLuisQuery(_configuration, stepContext.Context, cancellationToken);
var bookingDetails = await LuisHelper.ExecuteLuisQuery(_configuration, _logger, stepContext.Context, cancellationToken);

// In this sample we only have a single Intent we are concerned with. However, typically a scneario
// will have multiple different Intents each corresponding to starting a different child Dialog.

// Run the BookingDialog giving it whatever details we have from the LUIS call, it will fill out the remainder.
return await stepContext.BeginDialogAsync(nameof(BookingDialog), bookingDetails, cancellationToken);
Expand Down
62 changes: 35 additions & 27 deletions samples/csharp_dotnetcore/13.basic-bot/LuisHelper.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.AI.Luis;
using Microsoft.Bot.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Microsoft.BotBuilderSamples
{
public static class LuisHelper
{
public static async Task<BookingDetails> ExecuteLuisQuery(IConfiguration configuration, ITurnContext turnContext, CancellationToken cancellationToken)
public static async Task<BookingDetails> ExecuteLuisQuery(IConfiguration configuration, ILogger logger, ITurnContext turnContext, CancellationToken cancellationToken)
{
// Create the LUIS client from configuration.
var luisService = new LuisService
{
AppId = configuration["BotServices:Luis-Booking-AppId"],
AuthoringKey = configuration["BotServices:Luis-Booking-AuthoringKey"],
Region = configuration["BotServices:Luis-Booking-Region"],
};

var recognizer = new LuisRecognizer(new LuisApplication(
luisService.AppId,
luisService.AuthoringKey,
luisService.GetEndpoint()));

// The actual call to LUIS
var recognizerResult = await recognizer.RecognizeAsync(turnContext, cancellationToken);

// Now process the result from LUIS.
var bookingDetails = new BookingDetails();

var (intent, score) = recognizerResult.GetTopScoringIntent();
if (intent == "Book_flight")
try
{
// We need to get the result from the LUIS JSON which at every level returns an array.
bookingDetails.Destination = recognizerResult.Entities["To"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();
bookingDetails.Origin = recognizerResult.Entities["From"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();

// This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
// TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year.
bookingDetails.TravelDate = recognizerResult.Entities["datetime"]?.FirstOrDefault()?["timex"]?.FirstOrDefault()?.ToString().Split('T')[0];
// Create the LUIS client from configuration.
var luisService = new LuisService
{
AppId = configuration["BotServices:Luis-Booking-AppId"],
AuthoringKey = configuration["BotServices:Luis-Booking-AuthoringKey"],
Region = configuration["BotServices:Luis-Booking-Region"],
};

var recognizer = new LuisRecognizer(new LuisApplication(
luisService.AppId,
luisService.AuthoringKey,
luisService.GetEndpoint()));

// The actual call to LUIS
var recognizerResult = await recognizer.RecognizeAsync(turnContext, cancellationToken);

var (intent, score) = recognizerResult.GetTopScoringIntent();
if (intent == "Book_flight")
{
// We need to get the result from the LUIS JSON which at every level returns an array.
bookingDetails.Destination = recognizerResult.Entities["To"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();
bookingDetails.Origin = recognizerResult.Entities["From"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();

// This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
// TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year.
bookingDetails.TravelDate = recognizerResult.Entities["datetime"]?.FirstOrDefault()?["timex"]?.FirstOrDefault()?.ToString().Split('T')[0];
}
}
catch (Exception e)
{
logger.LogWarning($"LUIS Exception: {e.Message} Check your LUIS configuration.");
}

return bookingDetails;
Expand Down
6 changes: 1 addition & 5 deletions samples/csharp_dotnetcore/13.basic-bot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.BotBuilderSamples
{
public class Startup
{
public Startup(IConfiguration configuration)
public Startup()
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
Expand Down

0 comments on commit 4e9964b

Please sign in to comment.