-
Notifications
You must be signed in to change notification settings - Fork 63
/
SearchScorable.cs
53 lines (45 loc) · 1.8 KB
/
SearchScorable.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 HelpDeskBot.Dialogs
{
using System;
using System.Threading;
using System.Threading.Tasks;
using HelpDeskBot.Services;
using HelpDeskBot.Util;
using Microsoft.Bot.Builder.Scorables.Internals;
using Microsoft.Bot.Connector;
public class SearchScorable : ScorableBase<IActivity, string, double>
{
private const string TRIGGER = "search about ";
private readonly AzureSearchService searchService = new AzureSearchService();
protected override Task DoneAsync(IActivity item, string state, CancellationToken token)
{
return Task.CompletedTask;
}
protected override double GetScore(IActivity item, string state)
{
return 1.0;
}
protected override bool HasScore(IActivity item, string state)
{
return !string.IsNullOrWhiteSpace(state);
}
protected async override Task PostAsync(IActivity item, string state, CancellationToken token)
{
var searchResult = await this.searchService.Search(state);
var replyActivity = ((Activity)item).CreateReply();
await CardUtil.ShowSearchResults(replyActivity, searchResult, $"I'm sorry, I did not understand '{state}'.\nType 'help' to know more about me :)");
}
protected async override Task<string> PrepareAsync(IActivity item, CancellationToken token)
{
var message = item.AsMessageActivity();
if (message != null && !string.IsNullOrWhiteSpace(message.Text))
{
if (message.Text.Trim().StartsWith(TRIGGER, StringComparison.InvariantCultureIgnoreCase))
{
return message.Text.Substring(TRIGGER.Length);
}
}
return null;
}
}
}