-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathChoiceFlow_Dialog_Sample.cs
More file actions
96 lines (79 loc) · 3.49 KB
/
ChoiceFlow_Dialog_Sample.cs
File metadata and controls
96 lines (79 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Bot.Builder.Community.Dialogs.ChoiceFlow;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
namespace ChoiceFlow_Dialog_Sample
{
public class EmptyBot : ActivityHandler
{
private ConversationState _conversationState;
private DialogSet Dialogs { get; set; }
public EmptyBot(ILoggerFactory loggerFactory, ConversationState conversationState)
{
_conversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));
Dialogs = new DialogSet(_conversationState.CreateProperty<DialogState>(nameof(ChoiceFlow_Dialog_Sample)));
var pathToChoiceFlowJson = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "choiceFlow.json");
Dialogs.Add(new ChoiceFlowDialog(pathToChoiceFlowJson));
Dialogs.Add(new WaterfallDialog("MainDialog", new WaterfallStep[]
{
async (dc, cancellationToken) =>
{
return await dc.BeginDialogAsync(ChoiceFlowDialog.DefaultDialogId);
},
async (dc, cancellationToken) =>
{
if (dc.Result is ChoiceFlowItem returnedItem)
{
await dc.Context.SendActivityAsync($"The choice flow has finished. The user picked {returnedItem.Name}");
}
return await dc.EndDialogAsync();
}
}));
}
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
await base.OnTurnAsync(turnContext, cancellationToken);
// Save any state changes that might have occured during the turn.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var dc = await Dialogs.CreateContextAsync(turnContext);
var dialogResult = await dc.ContinueDialogAsync();
if (!dc.Context.Responded)
{
switch (dialogResult.Status)
{
case DialogTurnStatus.Empty:
await dc.BeginDialogAsync("MainDialog");
break;
case DialogTurnStatus.Waiting:
break;
case DialogTurnStatus.Complete:
await dc.EndDialogAsync();
break;
default:
await dc.CancelAllDialogsAsync();
break;
}
}
}
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Hello world!"), cancellationToken);
}
}
}
}
}