From ec8169869ca87d4647600c349a6e2e0df294fa70 Mon Sep 17 00:00:00 2001 From: Markus Meyer Date: Wed, 4 Mar 2020 11:40:30 +0100 Subject: [PATCH] #105 Clean Up --- PlanB.Butler.Bot/Bots/TeamsBot.cs | 38 ++++++++++++--- PlanB.Butler.Bot/Dialogs/DeleteOrderDialog.cs | 12 ----- PlanB.Butler.Bot/Dialogs/MainDialog.cs | 15 +++--- PlanB.Butler.Bot/Dialogs/OrderDialog.cs | 37 +++++++++++++- PlanB.Butler.Bot/Dialogs/OverviewDialog.cs | 48 ++++++++----------- 5 files changed, 98 insertions(+), 52 deletions(-) diff --git a/PlanB.Butler.Bot/Bots/TeamsBot.cs b/PlanB.Butler.Bot/Bots/TeamsBot.cs index 65751c8..53946c7 100644 --- a/PlanB.Butler.Bot/Bots/TeamsBot.cs +++ b/PlanB.Butler.Bot/Bots/TeamsBot.cs @@ -15,18 +15,25 @@ namespace PlanB.Butler.Bot { /// - /// This bot is derived (view DialogBot) from the TeamsACtivityHandler class currently included as part of this sample. + /// This bot is derived (view DialogBot) from the TeamsACtivityHandler class currently included as part of this sample. /// - /// + /// Bot. /// - public class TeamsBot : DialogBot where T : Dialog + public class TeamsBot : DialogBot + where T : Dialog { /// /// TeamsBotsWelcomeMessage. /// - private static string teamBotsWelcomeMessage = string.Empty; - + + /// + /// Initializes a new instance of the class. + /// + /// State of the conversation. + /// State of the user. + /// The dialog. + /// The logger. public TeamsBot(ConversationState conversationState, UserState userState, T dialog, ILogger> logger) : base(conversationState, userState, dialog, logger) { @@ -34,12 +41,31 @@ public TeamsBot(ConversationState conversationState, UserState userState, T dial teamBotsWelcomeMessage = rm.GetString("TeamBots_WelcomeMessage"); } + /// + /// Override this in a derived class to provide logic for when members other than the bot + /// join the conversation, such as your bot's welcome logic. + /// + /// A list of all the members added to the conversation, as + /// described by the conversation update activity. + /// A strongly-typed context object for this turn. + /// A cancellation token that can be used by other objects + /// or threads to receive notice of cancellation. + /// + /// When the + /// method receives a conversation update activity that indicates one or more users other than the bot + /// are joining the conversation, it calls this method. + /// + /// protected override async Task OnMembersAddedAsync(IList membersAdded, ITurnContext turnContext, CancellationToken cancellationToken) { - await turnContext.SendActivityAsync(teamBotsWelcomeMessage, cancellationToken: cancellationToken); } + /// + /// Call signin verify state asynchronous. + /// + /// The turn context. + /// The cancellation token. protected override async Task OnSigninVerifyStateAsync(ITurnContext turnContext, CancellationToken cancellationToken) { this.Logger.LogInformation("Running dialog with signin/verifystate from an Invoke Activity."); diff --git a/PlanB.Butler.Bot/Dialogs/DeleteOrderDialog.cs b/PlanB.Butler.Bot/Dialogs/DeleteOrderDialog.cs index b88cf39..603fa4a 100644 --- a/PlanB.Butler.Bot/Dialogs/DeleteOrderDialog.cs +++ b/PlanB.Butler.Bot/Dialogs/DeleteOrderDialog.cs @@ -279,18 +279,6 @@ private async Task DeleteOrderStep(WaterfallStepContext stepCo } } - public Order GetOrder(Order order) - { - OrderBlob orderBlob = new OrderBlob(); - int weeknumber = (DateTime.Now.DayOfYear / 7) + 1; - orderBlob = JsonConvert.DeserializeObject(BotMethods.GetDocument("orders", "orders_" + weeknumber + "_" + DateTime.Now.Year + ".json", this.botConfig.Value.StorageAccountUrl, this.botConfig.Value.StorageAccountKey)); - - var bufferOrder = orderBlob.OrderList.FindAll(x => x.Name == order.Name); - - var temp = bufferOrder.FindAll(x => x.CompanyStatus == order.CompanyStatus); - var orderValue = temp[temp.Count - 1]; - return orderValue; - } public void DeleteOrder(Order order, string serviceBusConnectionString) { string date = order.Date.ToString("yyyy-MM-dd"); diff --git a/PlanB.Butler.Bot/Dialogs/MainDialog.cs b/PlanB.Butler.Bot/Dialogs/MainDialog.cs index bacc88d..302d89a 100644 --- a/PlanB.Butler.Bot/Dialogs/MainDialog.cs +++ b/PlanB.Butler.Bot/Dialogs/MainDialog.cs @@ -2,19 +2,13 @@ // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using AdaptiveCards; using Microsoft.Bot.Builder; using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Choices; -using Microsoft.Bot.Schema; using Microsoft.Extensions.Options; -using Newtonsoft.Json.Linq; -using PlanB.Butler.Bot; namespace PlanB.Butler.Bot.Dialogs { @@ -41,6 +35,7 @@ public MainDialog(IBotTelemetryClient telemetryClient, IOptions confi // Set the telemetry client for this and all child dialogs. this.TelemetryClient = telemetryClient; this.clientFactory = httpClientFactory; + this.TelemetryClient?.TrackTrace($"{nameof(MainDialog)} started.", Severity.Information, new Dictionary()); // This array defines how the Waterfall will execute. var waterfallSteps = new WaterfallStep[] @@ -53,8 +48,16 @@ public MainDialog(IBotTelemetryClient telemetryClient, IOptions confi // The initial child Dialog to run. this.InitialDialogId = nameof(WaterfallDialog); + this.TelemetryClient?.TrackTrace($"{nameof(MainDialog)} finished.", Severity.Information, new Dictionary()); + this.TelemetryClient?.Flush(); } + /// + /// Initials the step asynchronous. + /// + /// The step context. + /// The cancellation token. + /// DialogTurnResult. private async Task InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { return await stepContext.BeginDialogAsync(nameof(OverviewDialog), null, cancellationToken); diff --git a/PlanB.Butler.Bot/Dialogs/OrderDialog.cs b/PlanB.Butler.Bot/Dialogs/OrderDialog.cs index 9ce5e7f..f0df451 100644 --- a/PlanB.Butler.Bot/Dialogs/OrderDialog.cs +++ b/PlanB.Butler.Bot/Dialogs/OrderDialog.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -10,6 +11,7 @@ using Microsoft.Bot.Builder.Dialogs.Choices; using Microsoft.Extensions.Options; using Newtonsoft.Json; +using PlanB.Butler.Bot.Services; namespace PlanB.Butler.Bot.Dialogs { @@ -26,10 +28,24 @@ public class OrderDialog : ComponentDialog /// private readonly IOptions botConfig; - public OrderDialog(IOptions config, IBotTelemetryClient telemetryClient) + /// + /// The client factory. + /// + private readonly IHttpClientFactory clientFactory; + + /// + /// Initializes a new instance of the class. + /// + /// The configuration. + /// The telemetry client. + /// The HTTP client factory. + public OrderDialog(IOptions config, IBotTelemetryClient telemetryClient, IHttpClientFactory httpClientFactory) : base(nameof(OrderDialog)) { this.TelemetryClient = telemetryClient; + this.botConfig = config; + this.clientFactory = httpClientFactory; + // This array defines how the Waterfall will execute. var waterfallSteps = new WaterfallStep[] { @@ -54,6 +70,25 @@ public OrderDialog(IOptions config, IBotTelemetryClient telemetryClie private async Task TimeDayStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { + IMealService mealService = new MealService(this.clientFactory.CreateClient(), this.botConfig.Value); + var meals = await mealService.GetMeals(string.Empty, string.Empty); + var mealEnumerator = meals.GetEnumerator(); + PlanDay day = new PlanDay(); + while (mealEnumerator.MoveNext()) + { + if (string.IsNullOrEmpty(day.Restaurant1)) + { + day.Restaurant1 = mealEnumerator.Current.Restaurant; + } + + if (string.IsNullOrEmpty(day.Restaurant2) && day.Restaurant1 != mealEnumerator.Current.Restaurant) + { + day.Restaurant2 = mealEnumerator.Current.Restaurant; + } + } + + + // Get the Plan try { diff --git a/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs b/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs index 0dcd507..c3a0d58 100644 --- a/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs +++ b/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Reflection; using System.Resources; @@ -93,15 +94,15 @@ public OverviewDialog(IOptions config, IBotTelemetryClient telemetryC otherDayDialogOrder = rm.GetString("OtherDayDialog_Order"); choices = new string[] { overviewDialogOrderFood, overviewDialogOtherDay, overviewDialogDeleteOrder, overviewDialogShowDepts, overviewDialogDaysOrder }; - OrderDialog orderDialog = new OrderDialog(config, telemetryClient); - NextOrder nextorderDialog = new NextOrder(config, telemetryClient); - PlanDialog planDialog = new PlanDialog(config, telemetryClient); - CreditDialog creditDialog = new CreditDialog(config, telemetryClient); - OrderForOtherDayDialog orderForAnotherDay = new OrderForOtherDayDialog(config, telemetryClient); - DeleteOrderDialog deleteOrderDialog = new DeleteOrderDialog(config, telemetryClient, this.clientFactory); + OrderDialog orderDialog = new OrderDialog(this.botConfig, telemetryClient, this.clientFactory); + NextOrder nextorderDialog = new NextOrder(this.botConfig, telemetryClient); + PlanDialog planDialog = new PlanDialog(this.botConfig, telemetryClient); + CreditDialog creditDialog = new CreditDialog(this.botConfig, telemetryClient); + OrderForOtherDayDialog orderForAnotherDay = new OrderForOtherDayDialog(this.botConfig, telemetryClient); + DeleteOrderDialog deleteOrderDialog = new DeleteOrderDialog(this.botConfig, telemetryClient, this.clientFactory); List dialogsList = new List(); - DailyCreditDialog dailyCreditDialog = new DailyCreditDialog(config, telemetryClient); - ExcellDialog excellDialog = new ExcellDialog(config, telemetryClient); + DailyCreditDialog dailyCreditDialog = new DailyCreditDialog(this.botConfig, telemetryClient); + ExcellDialog excellDialog = new ExcellDialog(this.botConfig, telemetryClient); // dialogsList.Add(orderDialog); dialogsList.Add(nextorderDialog); @@ -123,21 +124,20 @@ public OverviewDialog(IOptions config, IBotTelemetryClient telemetryC // Add named dialogs to the DialogSet. These names are saved in the dialog state. this.AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps)); - this.AddDialog(new OrderDialog(config, telemetryClient)); - this.AddDialog(new CreditDialog(config, telemetryClient)); - this.AddDialog(new PlanDialog(config, telemetryClient)); - this.AddDialog(new OrderForOtherDayDialog(config, telemetryClient)); - this.AddDialog(new DeleteOrderDialog(config, telemetryClient, this.clientFactory)); - this.AddDialog(new NextOrder(config, telemetryClient)); - this.AddDialog(new DailyCreditDialog(config, telemetryClient)); - this.AddDialog(new ExcellDialog(config, telemetryClient)); + this.AddDialog(new OrderDialog(this.botConfig, telemetryClient, this.clientFactory)); + this.AddDialog(new CreditDialog(this.botConfig, telemetryClient)); + this.AddDialog(new PlanDialog(this.botConfig, telemetryClient)); + this.AddDialog(new OrderForOtherDayDialog(this.botConfig, telemetryClient)); + this.AddDialog(new DeleteOrderDialog(this.botConfig, telemetryClient, this.clientFactory)); + this.AddDialog(new NextOrder(this.botConfig, telemetryClient)); + this.AddDialog(new DailyCreditDialog(this.botConfig, telemetryClient)); + this.AddDialog(new ExcellDialog(this.botConfig, telemetryClient)); this.AddDialog(new TextPrompt(nameof(TextPrompt))); this.AddDialog(new ChoicePrompt(nameof(ChoicePrompt))); this.AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt))); // The initial child Dialog to run. this.InitialDialogId = nameof(WaterfallDialog); - } /// @@ -145,7 +145,7 @@ public OverviewDialog(IOptions config, IBotTelemetryClient telemetryC /// /// The step context. /// The cancellation token. - /// + /// DialogTurnResult. private async Task InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { await stepContext.Context.SendActivityAsync(MessageFactory.Text(overviewDialogHelp), cancellationToken); @@ -227,18 +227,12 @@ private async Task InitialStepAsync(WaterfallStepContext stepC // Reply to the activity we received with an activity. var reply = MessageFactory.Attachment(attachments); - List choiceList = new List(); - for (int i = 0; i < choices.Length; i++) - { - choiceList.Add(choices[i]); - } - return await stepContext.PromptAsync( nameof(ChoicePrompt), new PromptOptions { Prompt = MessageFactory.Text(overviewDialogWhatNow), - Choices = ChoiceFactory.ToChoices(choiceList), + Choices = ChoiceFactory.ToChoices(choices.ToList()), Style = ListStyle.HeroCard, }, cancellationToken); } @@ -248,7 +242,7 @@ private async Task InitialStepAsync(WaterfallStepContext stepC /// /// The step context. /// The cancellation token. - /// + /// DialogTurnResult. private async Task ForwardStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { stepContext.Values["mainChoise"] = ((FoundChoice)stepContext.Result).Value; @@ -279,7 +273,7 @@ private async Task ForwardStepAsync(WaterfallStepContext stepC /// /// The step context. /// The cancellation token. - /// + /// DialogTurnResult. private async Task FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { return await stepContext.EndDialogAsync(null, cancellationToken);