From 1b363db14bd9d97fe43c9587fe422c9fa6f83f49 Mon Sep 17 00:00:00 2001 From: Simon Schneider Date: Thu, 12 Mar 2020 09:34:35 +0100 Subject: [PATCH] #52 implement adaptive card "summary" in NextOrder- Dialog --- .../{cards => AdaptiveCards}/Summary.json | 8 +-- .../PlanB.Butler.Bot/Bots/TeamsBot.cs | 18 ----- .../PlanB.Butler.Bot/Dialogs/CreditDialog.cs | 2 - .../PlanB.Butler.Bot/Dialogs/NextOrder.cs | 72 +++++++++++-------- .../Dialogs/OverviewDialog.cs | 2 +- .../PlanB.Butler.Bot/PlanB.Butler.Bot.csproj | 8 +-- PlanB.Butler.Bot/PlanB.Butler.Bot/Startup.cs | 2 +- .../appsettings.Development.json | 12 ++-- 8 files changed, 56 insertions(+), 68 deletions(-) rename PlanB.Butler.Bot/PlanB.Butler.Bot/{cards => AdaptiveCards}/Summary.json (94%) diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/cards/Summary.json b/PlanB.Butler.Bot/PlanB.Butler.Bot/AdaptiveCards/Summary.json similarity index 94% rename from PlanB.Butler.Bot/PlanB.Butler.Bot/cards/Summary.json rename to PlanB.Butler.Bot/PlanB.Butler.Bot/AdaptiveCards/Summary.json index cfeca74..44bcd35 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/cards/Summary.json +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/AdaptiveCards/Summary.json @@ -7,9 +7,9 @@ { "type": "TextBlock", "text": "Summary", - "spacing": "Large", - "size": "Large", - "weight": "Bolder" + "spacing": "large", + "size": "large", + "weight": "bolder" }, { "type": "ColumnSet", @@ -52,7 +52,7 @@ { "type": "TextBlock", "separator": true, - "text": "..." + "value": "..." }, { "type": "TextBlock", diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/Bots/TeamsBot.cs b/PlanB.Butler.Bot/PlanB.Butler.Bot/Bots/TeamsBot.cs index 8c76204..4137061 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/Bots/TeamsBot.cs +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/Bots/TeamsBot.cs @@ -63,12 +63,6 @@ protected override async Task OnMembersAddedAsync(IList membersA { await turnContext.SendActivityAsync(teamBotsWelcomeMessage, cancellationToken: cancellationToken); - // AdaptiveCard Test - var welcomeCard = this.CreateAdaptiveCardAttachment(); - var response = MessageFactory.Attachment(welcomeCard, ssml: "Welcome"); - await turnContext.SendActivityAsync(response, cancellationToken); - await this.Dialog.RunAsync(turnContext, this.ConversationState.CreateProperty("DialogState"), cancellationToken); - } /// @@ -86,17 +80,5 @@ protected override async Task OnSigninVerifyStateAsync(ITurnContext(nameof(DialogState)), cancellationToken); } - private Attachment CreateAdaptiveCardAttachment() - { - var path = "PlanB.Butler.Bot.cards.Summary.json"; - using var stream = GetType().Assembly.GetManifestResourceStream(path); - using var reader = new StreamReader(stream); - var adaptiveCard = reader.ReadToEnd(); - return new Attachment() - { - ContentType = "application/vnd.microsoft.card.adaptive", - Content = JsonConvert.DeserializeObject(adaptiveCard), - }; - } } } diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/CreditDialog.cs b/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/CreditDialog.cs index 79d89d0..3062832 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/CreditDialog.cs +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/CreditDialog.cs @@ -38,8 +38,6 @@ public class CreditDialog : ComponentDialog private static string creditDialogYes = string.Empty; private static string creditDialogNo = string.Empty; - - /// /// The bot configuration. /// diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/NextOrder.cs b/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/NextOrder.cs index 6af6549..d2b76e1 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/NextOrder.cs +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/NextOrder.cs @@ -12,6 +12,7 @@ using System.Threading; using System.Threading.Tasks; +using AdaptiveCards; using BotLibraryV2; using Microsoft.Bot.Builder; using Microsoft.Bot.Builder.Dialogs; @@ -116,13 +117,14 @@ public NextOrder(IOptions config, IBotTelemetryClient telemetryClient // This array defines how the Waterfall will execute. var waterfallSteps = new WaterfallStep[] { - this.CompanyStepAsync, + CompanyStepAsync, NameStepAsync, RestaurantStepAsync, QuantityStepAsync, FoodStepAsync, MealQuantityStepAsync, PriceStepAsync, + DisplayCardAsync, this.SummaryStepAsync, }; @@ -197,7 +199,6 @@ private async Task CompanyStepAsync(WaterfallStepContext stepC // } //} - return await stepContext.PromptAsync( nameof(ChoicePrompt), new PromptOptions @@ -209,9 +210,29 @@ private async Task CompanyStepAsync(WaterfallStepContext stepC } } - private static async Task NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) + // Test Adaptive Card + private async Task DisplayCardAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { + var path = Path.Combine(".", "AdaptiveCards", "Summary.json"); + var summaryCard = File.ReadAllText(path); + var cardAttachment = new Attachment() + { + ContentType = "application/vnd.microsoft.card.adaptive", + Content = JsonConvert.DeserializeObject(summaryCard), + }; + var message = MessageFactory.Text(string.Empty); + message.Attachments = new List() { cardAttachment }; + await stepContext.Context.SendActivityAsync(message, cancellationToken); + var opts = new PromptOptions + { + }; + + return await stepContext.PromptAsync(nameof(TextPrompt), opts); + } + + private static async Task NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) + { try { @@ -219,7 +240,7 @@ private static async Task NameStepAsync(WaterfallStepContext s companyStatus = (string)stepContext.Values["companyStatus"]; if (companyStatus.ToLower().ToString() == "kunde" || companyStatus == "extern") { - if (companyName == " ") + if (companyName == nextOrderDialogCostumer) { return await stepContext.PromptAsync( nameof(TextPrompt), @@ -239,7 +260,9 @@ private static async Task NameStepAsync(WaterfallStepContext s cancellationToken); } else + { return await stepContext.NextAsync(null, cancellationToken); + } } catch (Exception ex) { @@ -282,7 +305,6 @@ private async Task RestaurantStepAsync(WaterfallStepContext st } else { - return await stepContext.PromptAsync( nameof(ChoicePrompt), new PromptOptions @@ -378,6 +400,7 @@ private static async Task FoodStepAsync(WaterfallStepContext s if (stepContext.Values["restaurant"].ToString().ToLower() == plan.Planday[indexer].Restaurant1.ToLower()) { stepContext.Values["rest1"] = "yes"; + return await stepContext.PromptAsync( nameof(ChoicePrompt), new PromptOptions @@ -508,22 +531,6 @@ private async Task SummaryStepAsync(WaterfallStepContext stepC { await stepContext.Context.SendActivityAsync(MessageFactory.Text(nextOrderDialogSaveOrder), cancellationToken); - // Adaptive Card - async Task OnTurn(ITurnContext turnContext) - { - if (turnContext.Activity.Value != null) - { - await turnContext.SendActivityAsync(turnContext.Activity.Value.ToString()); - - } - - if (turnContext.Activity.Type == ActivityTypes.Message) - { - var response = turnContext.Activity.CreateReply(); - response.Attachments = new List() { CreateAdaptiveCardAttachment() }; - await turnContext.SendActivityAsync(response); - } - } } else { @@ -679,17 +686,22 @@ internal static IList GetChoice(string identifier, Plan plan) return ChoiceFactory.ToChoices(choice); } - private Attachment CreateAdaptiveCardAttachment() + + public Attachment CreateAdaptiveCardAttachment() { - var path = "PlanB.Butler.Bot.cards.Summary.json"; - using var stream = GetType().Assembly.GetManifestResourceStream(path); - using var reader = new StreamReader(stream); - var adaptiveCard = reader.ReadToEnd(); - return new Attachment() + var path = "PlanB.Butler.Bot.AdaptiveCards.Summary.json"; + using (var stream = GetType().Assembly.GetManifestResourceStream(path)) { - ContentType = "application/vnd.microsoft.card.adaptive", - Content = JsonConvert.DeserializeObject(adaptiveCard), - }; + using (var reader = new StreamReader(stream)) + { + var adaptiveCard = reader.ReadToEnd(); + return new Attachment() + { + ContentType = "application/vnd.microsoft.card.adaptive", + Content = JsonConvert.DeserializeObject(adaptiveCard), + }; + } + } } } } \ No newline at end of file diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs b/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs index d510815..bc18413 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/OverviewDialog.cs @@ -223,7 +223,7 @@ private async Task InitialStepAsync(WaterfallStepContext stepC } } - await stepContext.Context.SendActivityAsync(MessageFactory.Text("")); + await stepContext.Context.SendActivityAsync(MessageFactory.Text(string.Empty)); // Reply to the activity we received with an activity. var reply = MessageFactory.Attachment(attachments); diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/PlanB.Butler.Bot.csproj b/PlanB.Butler.Bot/PlanB.Butler.Bot/PlanB.Butler.Bot.csproj index d0db3c7..f7ad694 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/PlanB.Butler.Bot.csproj +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/PlanB.Butler.Bot.csproj @@ -22,6 +22,7 @@ + @@ -36,6 +37,7 @@ + @@ -69,12 +71,6 @@ - - - Always - - - PublicResXFileCodeGenerator diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/Startup.cs b/PlanB.Butler.Bot/PlanB.Butler.Bot/Startup.cs index 4c87033..823f7dd 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/Startup.cs +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/Startup.cs @@ -150,7 +150,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseMvc(); - // app.UseHttpsRedirection(); + //app.UseHttpsRedirection(); } } } diff --git a/PlanB.Butler.Bot/PlanB.Butler.Bot/appsettings.Development.json b/PlanB.Butler.Bot/PlanB.Butler.Bot/appsettings.Development.json index b49abfc..f999bc2 100644 --- a/PlanB.Butler.Bot/PlanB.Butler.Bot/appsettings.Development.json +++ b/PlanB.Butler.Bot/PlanB.Butler.Bot/appsettings.Development.json @@ -1,9 +1,9 @@ { - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" } } +} \ No newline at end of file