forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SearchSelectRefinerDialog.cs
53 lines (46 loc) · 2 KB
/
SearchSelectRefinerDialog.cs
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
namespace Search.Dialogs
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Internals.Fibers;
using Search.Models;
[Serializable]
public class SearchSelectRefinerDialog : IDialog<string>
{
protected readonly SearchQueryBuilder QueryBuilder;
protected readonly IEnumerable<string> Refiners;
protected readonly PromptStyler PromptStyler;
public SearchSelectRefinerDialog(IEnumerable<string> refiners, SearchQueryBuilder queryBuilder = null, PromptStyler promptStyler = null)
{
SetField.NotNull(out this.Refiners, nameof(refiners), refiners);
this.QueryBuilder = queryBuilder ?? new SearchQueryBuilder();
this.PromptStyler = promptStyler;
}
public async Task StartAsync(IDialogContext context)
{
IEnumerable<string> unusedRefiners = this.Refiners;
if (this.QueryBuilder != null)
{
unusedRefiners = unusedRefiners.Except(this.QueryBuilder.Refinements.Keys, StringComparer.OrdinalIgnoreCase);
}
if (unusedRefiners.Any())
{
var promptOptions = new CancelablePromptOptions<string>("What do you want to refine by?", cancelPrompt: "Type 'cancel' if you changed your mind.", options: unusedRefiners.ToList(), promptStyler: this.PromptStyler);
CancelablePromptChoice<string>.Choice(context, this.ReturnSelection, promptOptions);
}
else
{
await context.PostAsync("Oops! You used all the available refiners and you cannot refine the results anymore.");
context.Done<string>(null);
}
}
protected virtual async Task ReturnSelection(IDialogContext context, IAwaitable<string> input)
{
var selection = await input;
context.Done(selection);
}
}
}