-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddNumberIntent.cs
89 lines (80 loc) · 3.34 KB
/
AddNumberIntent.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace sample.Areas
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Zyin.IntentBot.Bot;
using Zyin.IntentBot.Intent;
using Zyin.IntentBot.Prompt;
using sample.Bot;
/// <summary>
/// Intent context for adding two numbers
/// </summary>
public class AddNumberIntentContext : IntentContext
{
/// <summary>
/// First prompt, number, must be between 1 and 10.
/// Note we need to use nullable here.
/// </summary>
[PromptProperty("What's the first value (between 1 and 10)?", "Value should be between 1 and 10", promptProvider:typeof(SampleNumberPromptProvider), order: 0)]
public int? First { get; set; }
/// <summary>
/// Second prompt, number as well, Int32 no other limitation
/// Note we need to use nullable here.
/// </summary>
[PromptProperty("What's the second value (any int32)?", order: 1)]
public int? Second { get; set; }
/// <summary>
/// Second prompt, number as well, Int32 no other limitation
/// Note we need to use nullable here.
/// </summary>
[PromptProperty("Memorize result?", order: 2)]
public bool? MemorizeResult { get; set; }
/// <summary>
/// Gets the intent name
/// </summary>
public override string IntentName => SampleIntentService.Intent_Sum;
}
/// <summary>
/// Intent handler for add number
/// </summary>
public class AddNumberIntentHandler : IntentHandler<AddNumberIntentContext>
{
/// <summary>
/// User state accessor
/// </summary>
private UserStateManager<SampleUserInfo> userInfoManager;
/// <summary>
/// Initialize a new instance of the AddNumberIntentHandler
/// </summary>
/// <param name="userStateAccessors"></param>
public AddNumberIntentHandler(UserStateManager<SampleUserInfo> userInfoManager)
{
this.userInfoManager = userInfoManager;
}
/// <summary>
/// Process the add number intent (after user input has been collected)
/// </summary>
/// <param name="turnContext"></param>
/// <param name="intentContext"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected override async Task ProcessIntentInternalAsync(ITurnContext turnContext, AddNumberIntentContext intentContext, CancellationToken cancellationToken)
{
// Perform the sum task
var x = intentContext.First.Value;
var y = intentContext.Second.Value;
var result = x + y;
if (intentContext.MemorizeResult.Value)
{
// Save result to user state since user wants to memorize it
var userInfo = await this.userInfoManager.GetAsync(turnContext, cancellationToken);
userInfo.SavedSumResult = result;
await this.userInfoManager.SetAsync(turnContext, userInfo, cancellationToken);
}
// Output result
var message = $"{x} + {y} = {result}";
await turnContext.SendActivityAsync(MessageFactory.Text(message), cancellationToken);
}
}
}