forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeController.cs
85 lines (75 loc) · 2.98 KB
/
HomeController.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
namespace LuisActions.Samples.Web.Controllers
{
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Cognitive.LUIS.ActionBinding;
using Models;
using Samples;
public class HomeController : Controller
{
public ActionResult Index()
{
var emptyModel = new QueryViewModel();
return this.View(emptyModel);
}
[HttpPost]
public async Task<ActionResult> Index(QueryViewModel model)
{
if (!model.HasIntent)
{
var luisService = new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings["LUIS_ModelId"], ConfigurationManager.AppSettings["LUIS_SubscriptionKey"]));
var luisResult = await luisService.QueryAsync(model.Query, CancellationToken.None);
var resolver = new LuisActionResolver(typeof(GetTimeInPlaceAction).Assembly);
var action = resolver.ResolveActionFromLuisIntent(luisResult);
// Triggering Contextual Action from scratch is not supported on this Web Sample
if (action != null && !LuisActionResolver.IsContextualAction(action))
{
model.LuisAction = action;
// TODO: this is dangerous. This should be stored somewhere else, not in the client, or at least encrypted
model.LuisActionType = action.GetType().AssemblyQualifiedName;
}
else
{
// no action recnogized
return this.View(new QueryViewModel());
}
}
ModelState.Clear();
var isValid = TryValidateModel(model.LuisAction);
if (isValid)
{
// fulfill
var actionResult = await model.LuisAction.FulfillAsync();
if (actionResult == null)
{
actionResult = "Cannot resolve your query";
}
return this.View("ActionFulfill", actionResult);
}
else
{
// not valid, continue to present form with missing/invalid parameters
return this.View(model);
}
}
public PartialViewResult ScaffoldAction(QueryViewModel query)
{
if (query == null || query.LuisAction == null)
{
return new EmptyPartialViewResult();
}
var modelProperties = query.LuisAction.GetType().GetProperties()
.Where(p => p.SetMethod != null);
var model = new ActionScaffoldViewModel()
{
Fields = modelProperties.Select(o => string.Format("LuisAction." + o.Name)),
LuisAction = query.LuisAction
};
return this.PartialView(model);
}
}
}