Skip to content

Commit f441e80

Browse files
committed
Ported the ChoiceFlow Dialog sample to MVC #185
1 parent ecf5c51 commit f441e80

17 files changed

+655
-260
lines changed

Bot.Builder.Community.Samples.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Google Adapter Sample", "sa
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Form Flow Sample", "samples\Form Flow Sample\Form Flow Sample.csproj", "{B982923B-163B-49FD-960B-0DF5BB6E6562}"
1515
EndProject
16-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChoiceFlow Dialog Sample", "samples\ChoiceFlow Dialog Sample\ChoiceFlow Dialog Sample.csproj", "{7EA0CA57-3D7E-4561-B457-13A1B57879BD}"
17-
EndProject
1816
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cortana Assistant Alexa Sample", "samples\Cortana Assistant Alexa Sample\Cortana Assistant Alexa Sample.csproj", "{2FD06287-25A1-47A6-BB85-2E508E543E66}"
1917
EndProject
2018
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Twitter Adapter Sample", "samples\Twitter Adapter Sample\Twitter Adapter Sample.csproj", "{036E2C3A-6894-4237-99B7-591D16326602}"
@@ -23,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Luis Dialog Sample", "sampl
2321
EndProject
2422
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentiment Middleware Sample", "samples\Sentiment Middleware Sample\Sentiment Middleware Sample.csproj", "{55F863F1-9EC8-420C-9099-BD8378D72F18}"
2523
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChoiceFlow Dialog Sample", "samples\ChoiceFlow Dialog Sample\ChoiceFlow Dialog Sample.csproj", "{D1C3490D-477A-4D79-A41C-4D15CA9DC183}"
25+
EndProject
2626
Global
2727
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2828
Debug|Any CPU = Debug|Any CPU
@@ -49,10 +49,6 @@ Global
4949
{B982923B-163B-49FD-960B-0DF5BB6E6562}.Debug|Any CPU.Build.0 = Debug|Any CPU
5050
{B982923B-163B-49FD-960B-0DF5BB6E6562}.Release|Any CPU.ActiveCfg = Release|Any CPU
5151
{B982923B-163B-49FD-960B-0DF5BB6E6562}.Release|Any CPU.Build.0 = Release|Any CPU
52-
{7EA0CA57-3D7E-4561-B457-13A1B57879BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
53-
{7EA0CA57-3D7E-4561-B457-13A1B57879BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
54-
{7EA0CA57-3D7E-4561-B457-13A1B57879BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
55-
{7EA0CA57-3D7E-4561-B457-13A1B57879BD}.Release|Any CPU.Build.0 = Release|Any CPU
5652
{2FD06287-25A1-47A6-BB85-2E508E543E66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5753
{2FD06287-25A1-47A6-BB85-2E508E543E66}.Debug|Any CPU.Build.0 = Debug|Any CPU
5854
{2FD06287-25A1-47A6-BB85-2E508E543E66}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -69,6 +65,10 @@ Global
6965
{55F863F1-9EC8-420C-9099-BD8378D72F18}.Debug|Any CPU.Build.0 = Debug|Any CPU
7066
{55F863F1-9EC8-420C-9099-BD8378D72F18}.Release|Any CPU.ActiveCfg = Release|Any CPU
7167
{55F863F1-9EC8-420C-9099-BD8378D72F18}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{D1C3490D-477A-4D79-A41C-4D15CA9DC183}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{D1C3490D-477A-4D79-A41C-4D15CA9DC183}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{D1C3490D-477A-4D79-A41C-4D15CA9DC183}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{D1C3490D-477A-4D79-A41C-4D15CA9DC183}.Release|Any CPU.Build.0 = Release|Any CPU
7272
EndGlobalSection
7373
GlobalSection(SolutionProperties) = preSolution
7474
HideSolutionNode = FALSE
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Generated with Bot Builder V4 SDK Template for Visual Studio CoreBot v4.6.2
2+
3+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
4+
using Microsoft.Bot.Builder.TraceExtensions;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace ChoiceFlow_Dialog_Sample
9+
{
10+
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
11+
{
12+
public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger)
13+
: base(configuration, logger)
14+
{
15+
OnTurnError = async (turnContext, exception) =>
16+
{
17+
// Log any leaked exception from the application.
18+
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
19+
20+
// Send a message to the user
21+
await turnContext.SendActivityAsync("The bot encounted an error or bug.");
22+
await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");
23+
24+
// Send a trace activity, which will be displayed in the Bot Framework Emulator
25+
await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
26+
};
27+
}
28+
}
29+
}

samples/ChoiceFlow Dialog Sample/BotConfiguration.bot

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
5-
<CodeAnalysisRuleSet></CodeAnalysisRuleSet>
6-
<RootNamespace>ChoiceFlowDialogSample</RootNamespace>
7-
<Version>4.3.0</Version>
8-
<FileVersion>4.3.0</FileVersion>
9-
<AssemblyVersion>4.3.0</AssemblyVersion>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<LangVersion>latest</LangVersion>
106
</PropertyGroup>
117

12-
13-
<ItemGroup>
14-
<Content Include="BotConfiguration.bot">
15-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16-
</Content>
17-
</ItemGroup>
18-
19-
208
<ItemGroup>
21-
<PackageReference Include="Bot.Builder.Community.Dialogs.ChoiceFlow" Version="4.3.10" />
22-
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.3" />
23-
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
24-
<PackageReference Include="AsyncUsageAnalyzers" Version="1.0.0-alpha003" PrivateAssets="all" />
25-
<PackageReference Include="Microsoft.Bot.Builder" Version="4.3.2" />
26-
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.3.2" />
27-
<PackageReference Include="Microsoft.Bot.Configuration" Version="4.3.2" />
28-
<PackageReference Include="Microsoft.Bot.Connector" Version="4.3.2" />
29-
<PackageReference Include="Microsoft.Bot.Schema" Version="4.3.2" />
30-
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.1.1" />
31-
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta008" PrivateAssets="all" />
9+
<PackageReference Include="Bot.Builder.Community.Dialogs.ChoiceFlow" Version="4.6.3" />
10+
<PackageReference Include="Microsoft.AspNetCore.App" />
11+
<PackageReference Include="Microsoft.Bot.Builder.Dialogs" Version="4.7.0" />
12+
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.6.2" />
3213
</ItemGroup>
3314

34-
35-
<ItemGroup>
36-
<Content Update="choiceFlow.json">
15+
<ItemGroup>
16+
<Content Update="appsettings.json">
3717
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3818
</Content>
3919
</ItemGroup>
4020

41-
4221
</Project>

samples/ChoiceFlow Dialog Sample/ChoiceFlowDialogSampleBot.cs

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Reflection;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Bot.Builder.Community.Dialogs.ChoiceFlow;
8+
using Microsoft.Bot.Builder;
9+
using Microsoft.Bot.Builder.Dialogs;
10+
using Microsoft.Bot.Schema;
11+
using Microsoft.Extensions.Logging;
12+
13+
namespace ChoiceFlow_Dialog_Sample
14+
{
15+
public class EmptyBot : ActivityHandler
16+
{
17+
private ConversationState _conversationState;
18+
private DialogSet Dialogs { get; set; }
19+
20+
public EmptyBot(ILoggerFactory loggerFactory, ConversationState conversationState)
21+
{
22+
23+
_conversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));
24+
25+
Dialogs = new DialogSet(_conversationState.CreateProperty<DialogState>(nameof(ChoiceFlow_Dialog_Sample)));
26+
27+
var pathToChoiceFlowJson = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "choiceFlow.json");
28+
29+
Dialogs.Add(new ChoiceFlowDialog(pathToChoiceFlowJson));
30+
Dialogs.Add(new WaterfallDialog("MainDialog", new WaterfallStep[]
31+
{
32+
async (dc, cancellationToken) =>
33+
{
34+
return await dc.BeginDialogAsync(ChoiceFlowDialog.DefaultDialogId);
35+
},
36+
async (dc, cancellationToken) =>
37+
{
38+
if (dc.Result is ChoiceFlowItem returnedItem)
39+
{
40+
await dc.Context.SendActivityAsync($"The choice flow has finished. The user picked {returnedItem.Name}");
41+
}
42+
43+
return await dc.EndDialogAsync();
44+
}
45+
}));
46+
}
47+
48+
49+
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
50+
{
51+
await base.OnTurnAsync(turnContext, cancellationToken);
52+
53+
// Save any state changes that might have occured during the turn.
54+
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
55+
}
56+
57+
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
58+
{
59+
60+
var dc = await Dialogs.CreateContextAsync(turnContext);
61+
var dialogResult = await dc.ContinueDialogAsync();
62+
63+
if (!dc.Context.Responded)
64+
{
65+
switch (dialogResult.Status)
66+
{
67+
case DialogTurnStatus.Empty:
68+
await dc.BeginDialogAsync("MainDialog");
69+
break;
70+
71+
case DialogTurnStatus.Waiting:
72+
break;
73+
74+
case DialogTurnStatus.Complete:
75+
await dc.EndDialogAsync();
76+
break;
77+
78+
default:
79+
await dc.CancelAllDialogsAsync();
80+
break;
81+
}
82+
}
83+
}
84+
85+
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
86+
{
87+
foreach (var member in membersAdded)
88+
{
89+
if (member.Id != turnContext.Activity.Recipient.Id)
90+
{
91+
await turnContext.SendActivityAsync(MessageFactory.Text($"Hello world!"), cancellationToken);
92+
}
93+
}
94+
}
95+
}
96+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Generated with Bot Builder V4 SDK Template for Visual Studio EmptyBot v4.6.2
2+
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Bot.Builder;
6+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
7+
8+
namespace ChoiceFlow_Dialog_Sample.Controllers
9+
{
10+
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
11+
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
12+
// achieved by specifying a more specific type for the bot constructor argument.
13+
[Route("api/messages")]
14+
[ApiController]
15+
public class BotController : ControllerBase
16+
{
17+
private readonly IBotFrameworkHttpAdapter Adapter;
18+
private readonly IBot Bot;
19+
20+
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
21+
{
22+
Adapter = adapter;
23+
Bot = bot;
24+
}
25+
26+
[HttpPost, HttpGet]
27+
public async Task PostAsync()
28+
{
29+
// Delegate the processing of the HTTP POST to the adapter.
30+
// The adapter will invoke the bot.
31+
await Adapter.ProcessAsync(Request, Response, Bot);
32+
}
33+
}
34+
}

samples/ChoiceFlow Dialog Sample/DeploymentScripts/MsbotClone/bot.recipe

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)