forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TriggerScorable.cs
50 lines (41 loc) · 1.52 KB
/
TriggerScorable.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
namespace TestBot.Scorables
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Scorables.Internals;
using Microsoft.Bot.Connector;
public abstract class TriggerScorable : ScorableBase<IActivity, bool, double>
{
protected readonly IBotToUser BotToUser;
protected readonly IBotData BotData;
public TriggerScorable(IBotToUser botToUser, IBotData botData)
{
SetField.NotNull(out this.BotToUser, nameof(botToUser), botToUser);
SetField.NotNull(out this.BotData, nameof(botData), botData);
}
public abstract string Trigger { get; }
protected override Task DoneAsync(IActivity item, bool state, CancellationToken token)
{
return Task.CompletedTask;
}
protected override double GetScore(IActivity item, bool state)
{
return state ? 1 : 0;
}
protected override bool HasScore(IActivity item, bool state)
{
return state;
}
protected override Task<bool> PrepareAsync(IActivity item, CancellationToken token)
{
var message = item.AsMessageActivity();
if (message == null)
{
return Task.FromResult(false);
}
return Task.FromResult(message.Text.ToLowerInvariant().Contains(this.Trigger.ToLowerInvariant()));
}
}
}