Skip to content

Commit

Permalink
PlanBGmbH#105 Clean Up
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusMeyer13 committed Mar 4, 2020
1 parent b8c2122 commit ec81698
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 52 deletions.
38 changes: 32 additions & 6 deletions PlanB.Butler.Bot/Bots/TeamsBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,57 @@
namespace PlanB.Butler.Bot
{
/// <summary>
/// This bot is derived (view DialogBot<T>) 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.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">Bot.</typeparam>
/// <seealso cref="PlanB.Butler.Bot.DialogBot{T}" />
public class TeamsBot<T> : DialogBot<T> where T : Dialog
public class TeamsBot<T> : DialogBot<T>
where T : Dialog
{
/// <summary>
/// TeamsBotsWelcomeMessage.
/// </summary>

private static string teamBotsWelcomeMessage = string.Empty;


/// <summary>
/// Initializes a new instance of the <see cref="TeamsBot{T}"/> class.
/// </summary>
/// <param name="conversationState">State of the conversation.</param>
/// <param name="userState">State of the user.</param>
/// <param name="dialog">The dialog.</param>
/// <param name="logger">The logger.</param>
public TeamsBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger)
: base(conversationState, userState, dialog, logger)
{
ResourceManager rm = new ResourceManager("PlanB.Butler.Bot.Dictionary.Dialogs", Assembly.GetExecutingAssembly());
teamBotsWelcomeMessage = rm.GetString("TeamBots_WelcomeMessage");
}

/// <summary>
/// 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.
/// </summary>
/// <param name="membersAdded">A list of all the members added to the conversation, as
/// described by the conversation update activity.</param>
/// <param name="turnContext">A strongly-typed context object for this turn.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <remarks>
/// When the <see cref="M:Microsoft.Bot.Builder.ActivityHandler.OnConversationUpdateActivityAsync(Microsoft.Bot.Builder.ITurnContext{Microsoft.Bot.Schema.IConversationUpdateActivity},System.Threading.CancellationToken)" />
/// method receives a conversation update activity that indicates one or more users other than the bot
/// are joining the conversation, it calls this method.
/// </remarks>
/// <seealso cref="M:Microsoft.Bot.Builder.ActivityHandler.OnConversationUpdateActivityAsync(Microsoft.Bot.Builder.ITurnContext{Microsoft.Bot.Schema.IConversationUpdateActivity},System.Threading.CancellationToken)" />
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{

await turnContext.SendActivityAsync(teamBotsWelcomeMessage, cancellationToken: cancellationToken);
}

/// <summary>
/// Call signin verify state asynchronous.
/// </summary>
/// <param name="turnContext">The turn context.</param>
/// <param name="cancellationToken">The cancellation token.</param>
protected override async Task OnSigninVerifyStateAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken)
{
this.Logger.LogInformation("Running dialog with signin/verifystate from an Invoke Activity.");
Expand Down
12 changes: 0 additions & 12 deletions PlanB.Butler.Bot/Dialogs/DeleteOrderDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,6 @@ private async Task<DialogTurnResult> DeleteOrderStep(WaterfallStepContext stepCo
}
}

public Order GetOrder(Order order)
{
OrderBlob orderBlob = new OrderBlob();
int weeknumber = (DateTime.Now.DayOfYear / 7) + 1;
orderBlob = JsonConvert.DeserializeObject<OrderBlob>(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");
Expand Down
15 changes: 9 additions & 6 deletions PlanB.Butler.Bot/Dialogs/MainDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -41,6 +35,7 @@ public MainDialog(IBotTelemetryClient telemetryClient, IOptions<BotConfig> 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<string, string>());

// This array defines how the Waterfall will execute.
var waterfallSteps = new WaterfallStep[]
Expand All @@ -53,8 +48,16 @@ public MainDialog(IBotTelemetryClient telemetryClient, IOptions<BotConfig> confi

// The initial child Dialog to run.
this.InitialDialogId = nameof(WaterfallDialog);
this.TelemetryClient?.TrackTrace($"{nameof(MainDialog)} finished.", Severity.Information, new Dictionary<string, string>());
this.TelemetryClient?.Flush();
}

/// <summary>
/// Initials the step asynchronous.
/// </summary>
/// <param name="stepContext">The step context.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>DialogTurnResult.</returns>
private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.BeginDialogAsync(nameof(OverviewDialog), null, cancellationToken);
Expand Down
37 changes: 36 additions & 1 deletion PlanB.Butler.Bot/Dialogs/OrderDialog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -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
{
Expand All @@ -26,10 +28,24 @@ public class OrderDialog : ComponentDialog
/// </summary>
private readonly IOptions<BotConfig> botConfig;

public OrderDialog(IOptions<BotConfig> config, IBotTelemetryClient telemetryClient)
/// <summary>
/// The client factory.
/// </summary>
private readonly IHttpClientFactory clientFactory;

/// <summary>
/// Initializes a new instance of the <see cref="OrderDialog"/> class.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="telemetryClient">The telemetry client.</param>
/// <param name="httpClientFactory">The HTTP client factory.</param>
public OrderDialog(IOptions<BotConfig> 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[]
{
Expand All @@ -54,6 +70,25 @@ public OrderDialog(IOptions<BotConfig> config, IBotTelemetryClient telemetryClie

private async Task<DialogTurnResult> 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
{
Expand Down
48 changes: 21 additions & 27 deletions PlanB.Butler.Bot/Dialogs/OverviewDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Resources;
Expand Down Expand Up @@ -93,15 +94,15 @@ public OverviewDialog(IOptions<BotConfig> 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<ComponentDialog> dialogsList = new List<ComponentDialog>();
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);
Expand All @@ -123,29 +124,28 @@ public OverviewDialog(IOptions<BotConfig> 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);

}

/// <summary>
/// Initials the step asynchronous.
/// </summary>
/// <param name="stepContext">The step context.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
/// <returns>DialogTurnResult.</returns>
private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
await stepContext.Context.SendActivityAsync(MessageFactory.Text(overviewDialogHelp), cancellationToken);
Expand Down Expand Up @@ -227,18 +227,12 @@ private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepC

// Reply to the activity we received with an activity.
var reply = MessageFactory.Attachment(attachments);
List<string> choiceList = new List<string>();
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);
}
Expand All @@ -248,7 +242,7 @@ private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepC
/// </summary>
/// <param name="stepContext">The step context.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
/// <returns>DialogTurnResult.</returns>
private async Task<DialogTurnResult> ForwardStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["mainChoise"] = ((FoundChoice)stepContext.Result).Value;
Expand Down Expand Up @@ -279,7 +273,7 @@ private async Task<DialogTurnResult> ForwardStepAsync(WaterfallStepContext stepC
/// </summary>
/// <param name="stepContext">The step context.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
/// <returns>DialogTurnResult.</returns>
private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.EndDialogAsync(null, cancellationToken);
Expand Down

0 comments on commit ec81698

Please sign in to comment.