diff --git a/ANAConversationPlatform/ANAConversationPlatform.csproj b/ANAConversationPlatform/ANAConversationPlatform.csproj index c0eea9d..2ffe558 100644 --- a/ANAConversationPlatform/ANAConversationPlatform.csproj +++ b/ANAConversationPlatform/ANAConversationPlatform.csproj @@ -7,13 +7,13 @@ + + + - - - - + diff --git a/ANAConversationPlatform/Controllers/ConversationController.cs b/ANAConversationPlatform/Controllers/ConversationController.cs index 6334098..60f6232 100644 --- a/ANAConversationPlatform/Controllers/ConversationController.cs +++ b/ANAConversationPlatform/Controllers/ConversationController.cs @@ -7,6 +7,7 @@ using ANAConversationPlatform.Models; using ANAConversationPlatform.Models.Sections; using Microsoft.Extensions.Logging; +using static ANAConversationPlatform.Helpers.Constants; namespace ANAConversationPlatform.Controllers { @@ -23,16 +24,23 @@ public ConversationController(ILogger log) [HttpGet] public ActionResult Chat() { - var chatNodes = MongoHelper.RetrieveRecordsFromChatNode(); - Debug.WriteLine(chatNodes.ToJson()); - if (chatNodes == null || chatNodes.Count == 0) - return Ok(new object[] { }); + try + { + var chatNodes = MongoHelper.RetrieveRecordsFromChatNode(); + if (chatNodes == null || chatNodes.Count == 0) + return Ok(new object[] { }); - return Json(chatNodes, new JsonSerializerSettings() + return Json(chatNodes, new JsonSerializerSettings() + { + NullValueHandling = NullValueHandling.Ignore, + Converters = new List { new CustomStringEnumConverter() } + }); + } + catch (System.Exception ex) { - NullValueHandling = NullValueHandling.Ignore, - Converters = new List { new CustomStringEnumConverter() } - }); + _log.LogError(new EventId((int)LoggerEventId.CHAT_ACTION_ERROR), ex, ex.Message); + return StatusCode(500, new { Message = ex.Message }); + } } [HttpGet] @@ -45,75 +53,83 @@ public ActionResult RefreshContent() [HttpGet] public ActionResult HybridChat() { - var chatNodes = MongoHelper.RetrieveRecordsFromChatNode(); + try + { + var chatNodes = MongoHelper.RetrieveRecordsFromChatNode(); - chatNodes.AddRange(new[] { - new ChatNode("INIT_CHAT_NODE") - { - ApiMethod = "GET", - ApiUrl = Url.Action("CreateUserSessionForHChat", "AgentChat", new { }, Request.Scheme), - Emotion = EmotionEnum.Cool, - Name = "Send Chat Text Node", - NodeType = NodeTypeEnum.ApiCall, - NextNodeId = "SEND_CHAT_HISTORY_TO_SERVER", - RequiredVariables = new[] { "DEVICE_ID", "PERSON_NAME" }, - }, - new ChatNode("SEND_CHAT_HISTORY_TO_SERVER") - { - ApiMethod = "POST", - ApiUrl = Url.Action("SubmitHistory", "AgentChat", new { }, Request.Scheme), - Emotion = EmotionEnum.Cool, - Name = "Send Chat History To Server", - NodeType = NodeTypeEnum.ApiCall, - NextNodeId = "GET_CHAT_TEXT_NODE", - RequiredVariables = new[] { "CHAT_USER_ID", "CHAT_USER_TOKEN", "DEVICE_ID", "AGENT", "HISTORY" }, - }, - new ChatNode("GET_CHAT_TEXT_NODE") + chatNodes.AddRange(new[] { - Buttons = new List + + + + + + + + + + + + diff --git a/ANAConversationSimulator/ViewModels/MainPageViewModel.cs b/ANAConversationSimulator/ViewModels/MainPageViewModel.cs index 13d0ea0..05487d1 100644 --- a/ANAConversationSimulator/ViewModels/MainPageViewModel.cs +++ b/ANAConversationSimulator/ViewModels/MainPageViewModel.cs @@ -83,7 +83,11 @@ public async void StartChatting() private DispatcherTimer buttonTimeoutTimer; public async void ProcessNode(JToken node, JToken section = null) { - if (node == null) return; + if (node == null) + { + Utils.ShowDialog("Node not found!"); + return; + } ClearButtonTimer(); //Replaceing verbs @@ -99,32 +103,41 @@ public async void ProcessNode(JToken node, JToken section = null) try { var paramDict = new Dictionary(); - foreach (var reqParam in parsedNode.RequiredVariables) - { - if (reqParam == "HISTORY") //Custom Variable - paramDict[reqParam] = ChatThread.Where(x => x.SectionType != SectionTypeEnum.Typing).ToArray(); - else - paramDict[reqParam] = ButtonActionHelper.GetSavedValue(reqParam); - } + if (parsedNode.RequiredVariables != null) + foreach (var reqParam in parsedNode.RequiredVariables) + { + if (reqParam == "HISTORY") //Custom Variable + paramDict[reqParam] = ChatThread.Where(x => x.SectionType != SectionTypeEnum.Typing).ToArray(); + else + paramDict[reqParam] = ButtonActionHelper.GetSavedValue(reqParam); + } var nextNodeId = parsedNode.NextNodeId; //Default switch (parsedNode.ApiMethod.ToUpper()) { case "GET": { var query = string.Join("&", paramDict.Select(x => $"{x.Key}={Uri.EscapeDataString(x.Value + "")}")); - var api = string.IsNullOrWhiteSpace(query) ? parsedNode.ApiUrl : parsedNode.ApiUrl + "?" + query; + var api = string.IsNullOrWhiteSpace(query) ? parsedNode.ApiUrl : parsedNode.ApiUrl + (parsedNode.ApiUrl?.Contains("?") == true ? "&" : "?") + query; + + var resp = await APIHelper.HitAsync(api); - var resp = await APIHelper.HitAsync>(api); - if (resp.ContainsKey("NextNodeId")) + if (!string.IsNullOrWhiteSpace(resp["NextNodeId"] + "")) nextNodeId = resp["NextNodeId"] + ""; - ButtonActionHelper.HandleSaveMultiple(resp); + + ButtonActionHelper.HandleSaveMultiple(resp.ToObject>()); + var apiNextNodeId = ExtractNextNodeIdFromAPIResp(parsedNode, resp); + if (!string.IsNullOrWhiteSpace(apiNextNodeId)) + nextNodeId = apiNextNodeId; } break; case "POST": { - var resp = await APIHelper.HitPostAsync, Dictionary>(parsedNode.ApiUrl, paramDict); - if (resp.ContainsKey("NextNodeId")) + var resp = await APIHelper.HitPostAsync, JObject>(parsedNode.ApiUrl, paramDict); + if (!string.IsNullOrWhiteSpace(resp["NextNodeId"] + "")) nextNodeId = resp["NextNodeId"] + ""; + var apiNextNodeId = ExtractNextNodeIdFromAPIResp(parsedNode, resp); + if (!string.IsNullOrWhiteSpace(apiNextNodeId)) + nextNodeId = apiNextNodeId; } break; default: @@ -184,9 +197,19 @@ public async void ProcessNode(JToken node, JToken section = null) case SectionTypeEnum.EmbeddedHtml: parsedSection = currentSectionSource.ToObject(); break; + case SectionTypeEnum.Carousel: + parsedSection = currentSectionSource.ToObject(); + (parsedSection as CarouselSection).Items + .SelectMany(x => x.Buttons) + .Where(X => X != null).ToList() + .ForEach(x => + { + x.VariableName = parsedNode.VariableName; + x.NodeId = parsedNode.Id; + }); + break; case SectionTypeEnum.Link: case SectionTypeEnum.Graph: - case SectionTypeEnum.Carousel: Utils.ShowDialog($"{secType} Coming soon!"); break; default: @@ -210,14 +233,17 @@ public async void ProcessNode(JToken node, JToken section = null) { await Task.Run(async () => { - try + await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { - await APIHelper.TrackEvent(Utils.GetViewEvent(parsedNode.Id, Utils.DeviceId)); - } - catch (Exception ex) - { - await Utils.ShowDialogAsync(ex.ToString()); - } + try + { + await APIHelper.TrackEvent(Utils.GetViewEvent(parsedNode.Id, Utils.DeviceId)); + } + catch (Exception ex) + { + await Utils.ShowDialogAsync(ex.ToString()); + } + }); }); } AddIncommingSection(parsedSection); @@ -352,7 +378,15 @@ public void NavigateToNode(string nextNodeId) if (!string.IsNullOrWhiteSpace(nextNodeId)) ProcessNode(GetNodeById(nextNodeId)); } - + private string ExtractNextNodeIdFromAPIResp(ChatNode node, JObject resp) + { + if (node.Buttons == null) + return null; + return node.Buttons.FirstOrDefault(btn => + !string.IsNullOrWhiteSpace(btn.APIResponseMatchKey) && //resp[btn.APIResponseMatchKey] != null && + resp.SelectToken(btn.APIResponseMatchKey) + "" == btn.APIResponseMatchValue + "") + ?.NextNodeId; + } #region Default public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary suspensionState) { diff --git a/ANAConversationStudio/ANAConversationStudio.csproj b/ANAConversationStudio/ANAConversationStudio.csproj index 6bc0424..8633b50 100644 --- a/ANAConversationStudio/ANAConversationStudio.csproj +++ b/ANAConversationStudio/ANAConversationStudio.csproj @@ -131,13 +131,19 @@ MSBuild:Compile Designer + + + + + ChatElementCollectionEditorWindow.xaml + DBConnectionManager.xaml @@ -166,10 +172,27 @@ + + EnterPassword.xaml + + + SetPassword.xaml + + + SettingsWindow.xaml + + + Designer + MSBuild:Compile + Designer MSBuild:Compile + + Designer + MSBuild:Compile + MSBuild:Compile Designer @@ -193,6 +216,14 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + @@ -224,6 +255,9 @@ Settings.Designer.cs + + PreserveNewest + @@ -233,6 +267,11 @@ + + + PreserveNewest + +