Skip to content

Commit

Permalink
#52 implement adaptive card "summary" in NextOrder- Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchneider99 committed Mar 12, 2020
1 parent 0f70194 commit 1b363db
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
{
"type": "TextBlock",
"text": "Summary",
"spacing": "Large",
"size": "Large",
"weight": "Bolder"
"spacing": "large",
"size": "large",
"weight": "bolder"
},
{
"type": "ColumnSet",
Expand Down Expand Up @@ -52,7 +52,7 @@
{
"type": "TextBlock",
"separator": true,
"text": "..."
"value": "..."
},
{
"type": "TextBlock",
Expand Down
18 changes: 0 additions & 18 deletions PlanB.Butler.Bot/PlanB.Butler.Bot/Bots/TeamsBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ protected override async Task OnMembersAddedAsync(IList<ChannelAccount> 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>("DialogState"), cancellationToken);

}

/// <summary>
Expand All @@ -86,17 +80,5 @@ protected override async Task OnSigninVerifyStateAsync(ITurnContext<IInvokeActiv
// Run the Dialog with the new Invoke Activity.
await this.Dialog.RunAsync(turnContext, this.ConversationState.CreateProperty<DialogState>(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),
};
}
}
}
2 changes: 0 additions & 2 deletions PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/CreditDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public class CreditDialog : ComponentDialog
private static string creditDialogYes = string.Empty;
private static string creditDialogNo = string.Empty;



/// <summary>
/// The bot configuration.
/// </summary>
Expand Down
72 changes: 42 additions & 30 deletions PlanB.Butler.Bot/PlanB.Butler.Bot/Dialogs/NextOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading;
using System.Threading.Tasks;

using AdaptiveCards;
using BotLibraryV2;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
Expand Down Expand Up @@ -116,13 +117,14 @@ public NextOrder(IOptions<BotConfig> 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,
};

Expand Down Expand Up @@ -197,7 +199,6 @@ private async Task<DialogTurnResult> CompanyStepAsync(WaterfallStepContext stepC
// }
//}


return await stepContext.PromptAsync(
nameof(ChoicePrompt),
new PromptOptions
Expand All @@ -209,17 +210,37 @@ private async Task<DialogTurnResult> CompanyStepAsync(WaterfallStepContext stepC
}
}

private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
// Test Adaptive Card
private async Task<DialogTurnResult> 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<Attachment>() { cardAttachment };
await stepContext.Context.SendActivityAsync(message, cancellationToken);

var opts = new PromptOptions
{
};

return await stepContext.PromptAsync(nameof(TextPrompt), opts);
}

private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{

try
{
stepContext.Values["companyStatus"] = ((FoundChoice)stepContext.Result).Value;
companyStatus = (string)stepContext.Values["companyStatus"];
if (companyStatus.ToLower().ToString() == "kunde" || companyStatus == "extern")
{
if (companyName == " ")
if (companyName == nextOrderDialogCostumer)
{
return await stepContext.PromptAsync(
nameof(TextPrompt),
Expand All @@ -239,7 +260,9 @@ private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext s
cancellationToken);
}
else
{
return await stepContext.NextAsync(null, cancellationToken);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -282,7 +305,6 @@ private async Task<DialogTurnResult> RestaurantStepAsync(WaterfallStepContext st
}
else
{

return await stepContext.PromptAsync(
nameof(ChoicePrompt),
new PromptOptions
Expand Down Expand Up @@ -378,6 +400,7 @@ private static async Task<DialogTurnResult> 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
Expand Down Expand Up @@ -508,22 +531,6 @@ private async Task<DialogTurnResult> 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<Attachment>() { CreateAdaptiveCardAttachment() };
await turnContext.SendActivityAsync(response);
}
}
}
else
{
Expand Down Expand Up @@ -679,17 +686,22 @@ internal static IList<Choice> 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),
};
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private async Task<DialogTurnResult> 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);
Expand Down
8 changes: 2 additions & 6 deletions PlanB.Butler.Bot/PlanB.Butler.Bot/PlanB.Butler.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<ItemGroup>
<Content Remove="AdaptiveCard.json" />
<Content Remove="appsettings.Development.json" />
<Content Remove="stylecop.json" />
</ItemGroup>

Expand All @@ -36,6 +37,7 @@

<ItemGroup>
<None Include="AdaptiveCard.json" />
<None Include="appsettings.Development.json" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -69,12 +71,6 @@
</Compile>
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Dictionary\Dialogs.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
Expand Down
2 changes: 1 addition & 1 deletion PlanB.Butler.Bot/PlanB.Butler.Bot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)

app.UseMvc();

// app.UseHttpsRedirection();
//app.UseHttpsRedirection();
}
}
}
12 changes: 6 additions & 6 deletions PlanB.Butler.Bot/PlanB.Butler.Bot/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

0 comments on commit 1b363db

Please sign in to comment.