-
Notifications
You must be signed in to change notification settings - Fork 0
/
RootLuisDialog.cs
359 lines (281 loc) · 14.2 KB
/
RootLuisDialog.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
namespace LuisBot.Dialogs
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Connector;
using System.Net.Http;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using LuisBot.FormFlow;
using LuisBot.CommonTypes;
using static LuisBot.CommonTypes.CommonTypes;
using LuisBot.Models;
using System.Text;
[LuisModel("77fee18b-8bbe-4e7b-b054-d863143ab616", "31aa162117554361b119f1a76e403f85")]
[Serializable]
public class RootLuisDialog : LuisDialog<object>
{
[LuisIntent("")]
[LuisIntent("None")]
public async Task None(IDialogContext context, LuisResult result)
{
string message = $"סליחה, לא ממש הבנתי למה התכוונת ב'{result.Query}'. אפשר לרשום עזרה כדי לקבל מידע.";
await context.PostAsync(message);
context.Wait(this.MessageReceived);
}
[LuisIntent("ApproveCourseEnrollment")]
public async Task ApproveCourseEnrollmentIntent(IDialogContext context, LuisResult result)
{
if (result.Entities.Where(i => i.Type == ("EnrollmentRequestID")).FirstOrDefault() == null)
{
await context.PostAsync("חסרים פרטים באישור בקשת ההשתלמות");
context.Wait(this.MessageReceived);
}
else
{
string message = $"ביקשת לאשר בקשת השתלמות, אנא המתן...";
//gather the data.
ActionItem infoToSend = new ActionItem()
{
ActionToTake = ActionToTake.Enquire,//"ApproveCourseEnrollment",
MethodName = "GetCourseDetailsByID",
ParametersList = new List<LuisBot.CommonTypes.ActionParameters>
{
new LuisBot.CommonTypes.ActionParameters { ParameterName = "EnrollmentRequestID", ParameterType = "Int", ParameterValue = result.Entities.Where(i => i.Type == ("EnrollmentRequestID")).FirstOrDefault().Entity }
},
SystemName = "CoursesEnrollmentSystem",
WsUrl = "https://somekindOfWebAddress/blabla.asmx" //TODO: Replace with url
};
//call web service with this info.
//Get back an answer and handle it.
bool answerFromWsIsNull = true;
bool answerFromWsIsName = true;
if (answerFromWsIsNull)
{
await context.PostAsync("לא נמצאה בקשת השתלמות עם המספר הזה");
ActionItem infoToSendToGetListOfFewCourses = new ActionItem()
{
ActionToTake = ActionToTake.Enquire,//"ApproveCourseEnrollment",
MethodName = "GetCourseDetails",
ParametersList = new List<LuisBot.CommonTypes.ActionParameters>
{
new LuisBot.CommonTypes.ActionParameters { ParameterName = "NumberOfCourses", ParameterType = "Int", ParameterValue = "4"}
},
SystemName = "CoursesEnrollmentSystem",
WsUrl = "https://somekindOfWebAddress/blabla.asmx" //TODO: Replace with url
};
List<string> coursesNames = GetFewCourses();
StringBuilder sb = new StringBuilder();
foreach (var item in coursesNames)
{
sb.Append($"{item},");
}
await context.PostAsync($"תוכל לטפל באחד הקורסים הבאים: {sb.ToString()}");
//format the answer and present the user with choice:
}
else
{
//Set the EnrollmentRequestID
context.ConversationData.SetValue(ContextConstants.CN_EnrollmentRequestID, infoToSend.ParametersList[0].ParameterValue);
// context.Wait(AwaitingConfirmation);
PromptDialog.Text(context, ResumeAfterPrompt, "'האם אתה מעוניין לאשר את בקשת ההשתלמות בשם 'שם כלשהו שחזר מהשירות'? הקלד 'אשר' או 'דחה או 'ביטול פעולה' ' ");
//PromptDialog.Choice()
}
}
// context.Wait(this.MessageReceived);
}
private List<string> GetFewCourses( )
{
List<string> coursesNames = new List<string>();
// Filling the hotels results manually just for demo purposes
for (int i = 1; i <= 5; i++)
{
coursesNames.Add($"קורס בשם כלשהו {i}");
}
return coursesNames;
}
private async Task ResumeAfterPrompt(IDialogContext context, IAwaitable<string> result)
{
//try
// {
var userReply = await result;
//while (userReply != "אשר" || userReply != "דחה" || userReply != "ביטול פעולה")
// while (userReply != "אשר" || userReply != "דחה" || userReply != "ביטול פעולה")
if (userReply != "Which" && userReply != "Reject" && userReply != "Undo")
{
PromptDialog.Text(context, ResumeAfterPrompt, "'האם אתה מעוניין לאשר את בקשת ההשתלמות בשם 'שם כלשהו שחזר מהשירות'? הקלד 'אשר' או 'דחה או 'ביטול פעולה' ' ");
}
switch (userReply)
{
case "Which":
{
//send approval to the web service
ActionItem infoToSend = new ActionItem()
{
// ActionItemMask = ActionItemMask.None,
ActionToTake = ActionToTake.Approve,//"ApproveCourseEnrollment",
MethodName = "ApproveCourseEnrollment",
ParametersList = new List<LuisBot.CommonTypes.ActionParameters>
{
new LuisBot.CommonTypes.ActionParameters { ParameterName = "EnrollmentRequestID", ParameterType = "Int",
ParameterValue = context.ConversationData.GetValue<string>(ContextConstants.CN_EnrollmentRequestID)
}
},
SystemName = "CoursesEnrollmentSystem",
WsUrl = "https://somekindOfWebAddress/blabla.asmx" //TODO: Replace with url
};
//send to the web service
await context.PostAsync($"פעולת האישור נשלחה ותטופל.");
}
break;
case "Reject":
//send approval to the web service
ActionItem rejectToSend = new ActionItem()
{
// ActionItemMask = ActionItemMask.None,
ActionToTake = ActionToTake.Reject,//"ApproveCourseEnrollment",
MethodName = "RejectCourseEnrollment",
ParametersList = new List<LuisBot.CommonTypes.ActionParameters>
{
new LuisBot.CommonTypes.ActionParameters { ParameterName = "EnrollmentRequestID", ParameterType = "Int",
ParameterValue = context.ConversationData.GetValue<string>(ContextConstants.CN_EnrollmentRequestID)
}
},
SystemName = "CoursesEnrollmentSystem",
WsUrl = "https://somekindOfWebAddress/blabla.asmx" //TODO: Replace with url
};
await context.PostAsync($"פעולת הדחייה נשלחה ותטופל.");
break;
case "Undo":
await context.PostAsync($"הבקשה בוטלה.");
break;
default:
break;
}
}
private async Task AfterCourseDialogIsDone(IDialogContext context, IAwaitable<object> result)
{
await context.PostAsync("האם תרצה לבצע משהו נוסף?");
// await context.PostAsync(message + "asdasd");
context.Wait(this.MessageReceived);
}
[LuisIntent("Reject")]
public async Task RejectCourseEnrollmentIntent(IDialogContext context, LuisResult result)
{
string message = $"";
await context.PostAsync(message);
context.Wait(this.MessageReceived);
}
[LuisIntent("OpenFaultTicket")]
public async Task OpenFaultTicket(IDialogContext context, LuisResult result)
{
string message = $"";
await context.PostAsync(message);
context.Wait(this.MessageReceived);
}
[LuisIntent("GreetingIntent")]
public async Task GreetingIntent(IDialogContext context, LuisResult result)
{
await context.PostAsync(GetGreeting());
}
[LuisIntent("Weather")]
public async Task Weather(IDialogContext context, LuisResult result)
{
if (result.Entities.Where(i => i.Type == ("city")).FirstOrDefault() == null)
{
await context.PostAsync("איפה?");
context.Wait(this.MessageReceived);
}
else
using (var client = new HttpClient())
{
var escapedLocation = Regex.Replace(result.Entities.Where(i => i.Type == ("city")).FirstOrDefault().Entity.ToString(), @"\W+", "_");
dynamic response = JObject.Parse(await client.GetStringAsync($"http://api.wunderground.com/api/1910fe7aa3da5f4f/conditions/q/" + escapedLocation + ".json"));
dynamic observation = response.current_observation;
dynamic results = response.response.results;
if (observation != null)
{
string displayLocation = observation.display_location?.full;
decimal tempC = observation.temp_c;
string weather = observation.weather;
await context.PostAsync($"It is {weather} and {tempC} degrees in {displayLocation}.");
}
else if (results != null)
{
await context.PostAsync($"There is more than one '{result.Entities.Where(i => i.Type == ("city")).FirstOrDefault().ToString()}'. Can you be more specific?");
}
}
}
private string GetGreeting()
{
var greetings = new List<string> { "שלום", "מה שלומך?", "היי", "מה אפשר לעזור?", " לשירותך, מה אפשר לעזור Bot-IT" };
if (DateTime.Now.Hour < 12)
{
greetings.Add("בוקר טוב, מה אפשר לעזור?");
}
if (DateTime.Now.Hour > 11 && DateTime.Now.Hour < 12)
{
greetings.Add("בוקר טוב, בדרך לשולץ? מה אפשר לעזור?");
}
if (DateTime.Now.Hour > 13 && DateTime.Now.Hour < 14)
{
greetings.Add("קשה אחרי חדר האוכל הא? מה אוכל להועיל?");
}
if (DateTime.Now.Hour < 12)
{
greetings.Add("בוקר טוב!");
}
if (DateTime.Now.Hour > 15 && DateTime.Now.Hour < 17)
{
greetings.Add("אחר צהריים נעימים, מה אוכל לעזור?");
}
if (DateTime.Now.Hour > 17 && DateTime.Now.Hour < 20)
{
greetings.Add("ערב נעים, צריך משהו?");
}
if (DateTime.Now.Hour > 20 && DateTime.Now.Hour < 23)
{
greetings.Add("לילה טוב, מה אפשר לעזור?");
}
Random randomizer = new Random();
int index = randomizer.Next(greetings.Count);
string greeting = greetings[index];
return greeting;
}
private async Task<string> GetCurrentWeather(string location)
{
using (var client = new HttpClient())
{
var escapedLocation = Regex.Replace(location, @"\W+", "_");
dynamic response = JObject.Parse(await client.GetStringAsync($"http://api.wunderground.com/api/<key>/conditions/q/{escapedLocation}.json"));
dynamic observation = response.current_observation;
dynamic results = response.response.results;
if (observation != null)
{
string displayLocation = observation.display_location?.full;
decimal tempC = observation.temp_c;
string weather = observation.weather;
return $"It is {weather} and {tempC} degrees in {displayLocation}.";
}
else if (results != null)
{
return $"There is more than one '{location}'. Can you be more specific?";
}
return null;
}
}
[LuisIntent("Help")]
public async Task Help(IDialogContext context, LuisResult result)
{
await context.PostAsync("אפשר למשל לאשר השתלמות, או לפתוח תקלה ב2000. בקרוב עוד אופציות:)");
context.Wait(this.MessageReceived);
}
}
}