forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchRefineDialog.cs
73 lines (61 loc) · 2.71 KB
/
SearchRefineDialog.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
using Search.Services;
[Serializable]
public class SearchRefineDialog : IDialog<string>
{
protected readonly string Refiner;
protected readonly SearchQueryBuilder QueryBuilder;
protected readonly PromptStyler PromptStyler;
protected readonly string Prompt;
protected readonly ISearchClient SearchClient;
public SearchRefineDialog(ISearchClient searchClient, string refiner, SearchQueryBuilder queryBuilder = null, PromptStyler promptStyler = null, string prompt = null)
{
SetField.NotNull(out this.SearchClient, nameof(searchClient), searchClient);
SetField.NotNull(out this.Refiner, nameof(refiner), refiner);
this.QueryBuilder = queryBuilder ?? new SearchQueryBuilder();
this.PromptStyler = promptStyler;
this.Prompt = prompt ?? $"Here's what I found for {this.Refiner}.";
}
public async Task StartAsync(IDialogContext context)
{
var result = await this.SearchClient.SearchAsync(this.QueryBuilder, this.Refiner);
IEnumerable<string> options = result.Facets[this.Refiner].Select(f => this.FormatRefinerOption((string)f.Value, f.Count));
var promptOptions = new CancelablePromptOptions<string>(this.Prompt, cancelPrompt: "Type 'cancel' if you don't want to select any of these.", options: options.ToList(), promptStyler: this.PromptStyler);
CancelablePromptChoice<string>.Choice(context, this.ApplyRefiner, promptOptions);
}
public async Task ApplyRefiner(IDialogContext context, IAwaitable<string> input)
{
string selection = await input;
if (selection == null)
{
context.Done<string>(null);
}
else
{
string value = this.ParseRefinerValue(selection);
if (this.QueryBuilder != null)
{
await context.PostAsync($"Filtering by {this.Refiner}: {value}");
this.QueryBuilder.Refinements.Add(this.Refiner, new string[] { value });
}
context.Done(value);
}
}
protected virtual string FormatRefinerOption(string value, long count)
{
return $"{value} ({count})";
}
protected virtual string ParseRefinerValue(string value)
{
return value.Substring(0, value.LastIndexOf('(') - 1);
}
}
}