-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManageLightDialog.cs
More file actions
109 lines (84 loc) · 3.59 KB
/
ManageLightDialog.cs
File metadata and controls
109 lines (84 loc) · 3.59 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
namespace LightManagerBot
{
[Serializable]
[LuisModel("Replace with your ModelId", "Replace with your subscriptionKey")]
public class ManageLightDialog : LuisDialog<object>
{
[LuisIntent("")]
public async Task noIntent(IDialogContext context, LuisResult result)
{
string text = "Sorry, didn't understand. Try again";
await context.PostAsync(text);
context.Wait(MessageReceived);
}
[LuisIntent("OnLight")]
public async Task Onlight(IDialogContext context, LuisResult result)
{
string location;
string color;
if (TryFindType(result, out location, out color))
{
string message = $"Searching for {location} in {color}....";
context.ConversationData.SetValue<QueryItem>("userQuery", new QueryItem() { location = location, color = color });
await context.PostAsync(message);
context.Wait(MessageReceived);
}
else if (location == null && color == null)
{
string message = $"I think you want to switch on, please try again.\n\n" +
"Try a phrase like \"Switch on light in the kitchen with red color\"";
await context.PostAsync(message);
context.Wait(MessageReceived);
}
else if (location == null)
{
string message = $"Ok, you want to light on with {color} color - But what location?";
context.ConversationData.SetValue("color", color);
await context.PostAsync(message);
context.Wait(GetOtherField);
}
else
{
string message = $"Ok, you want to light on the light in the {location} - but what color ?";
context.ConversationData.SetValue("location", location);
await context.PostAsync(message);
context.Wait(GetOtherField);
}
}
public async Task GetOtherField(IDialogContext context, IAwaitable<Message> argument)
{
var incomingMessage = await argument;
string location = null;
string color = null;
if (context.ConversationData.TryGetValue("location", out location))
color = incomingMessage.Text;
else if (context.ConversationData.TryGetValue("color", out color))
location = incomingMessage.Text;
string message = $"Switch on light in the {location} with {color} color ....";
context.ConversationData.SetValue<QueryItem>("userQuery", new QueryItem() { location = location, color = color });
await context.PostAsync(message);
context.Wait(MessageReceived);
}
private bool TryFindType(LuisResult result, out string location, out string color)
{
location = null;
color = null;
EntityRecommendation LocationEntity, ColorEntity;
if (result.TryFindEntity("Location", out LocationEntity))
location = LocationEntity.Entity;
if (result.TryFindEntity("Color", out ColorEntity))
color = ColorEntity.Entity;
if (location != null && color != null)
return true;
return false;
}
}
}