From 0520eddc6ec5417e5cf99bb70ad5c59c8054006d Mon Sep 17 00:00:00 2001 From: haiping008 Date: Fri, 7 Dec 2018 15:21:36 -0600 Subject: [PATCH] Fill response parameters. --- .../ContextStorage/ContextStorageInFile.cs | 12 ++++++++++ .../IContextStorage.cs | 10 +++++++++ BotSharp.Platform.Dialogflow/DialogflowAi.cs | 22 +++++++++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 BotSharp.Core/ContextStorage/ContextStorageInFile.cs create mode 100644 BotSharp.Platform.Abstraction/IContextStorage.cs diff --git a/BotSharp.Core/ContextStorage/ContextStorageInFile.cs b/BotSharp.Core/ContextStorage/ContextStorageInFile.cs new file mode 100644 index 00000000..aa1c25f3 --- /dev/null +++ b/BotSharp.Core/ContextStorage/ContextStorageInFile.cs @@ -0,0 +1,12 @@ +using BotSharp.Platform.Abstraction; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Core.ContextStorage +{ + public class ContextStorageInFile : IContextStorage + { + + } +} diff --git a/BotSharp.Platform.Abstraction/IContextStorage.cs b/BotSharp.Platform.Abstraction/IContextStorage.cs new file mode 100644 index 00000000..9b999f08 --- /dev/null +++ b/BotSharp.Platform.Abstraction/IContextStorage.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Platform.Abstraction +{ + public interface IContextStorage + { + } +} diff --git a/BotSharp.Platform.Dialogflow/DialogflowAi.cs b/BotSharp.Platform.Dialogflow/DialogflowAi.cs index 8fa3e7d2..71c4bf5a 100644 --- a/BotSharp.Platform.Dialogflow/DialogflowAi.cs +++ b/BotSharp.Platform.Dialogflow/DialogflowAi.cs @@ -133,7 +133,9 @@ namespace BotSharp.Platform.Dialogflow }); var matches = Regex.Matches(presetResponse.Messages.Random().Speech, "\".*?\"").Cast(); - var speech = matches.ToList().Random(); + var speech = matches.Count() == 0 ? String.Empty : matches.ToList().Random().Value; + + var contexts = HandleContexts(presetResponse); var aiResponse = new AIResponseResult { @@ -143,17 +145,33 @@ namespace BotSharp.Platform.Dialogflow { IntentName = response.Intent }, + Intent = response.Intent, Fulfillment = new AIResponseFulfillment { Messages = presetResponse.Messages.ToList(), - Speech = speech.Value.Substring(1, speech.Length - 2) + Speech = speech.Length > 1 ? speech.Substring(1, speech.Length - 2) : String.Empty }, Score = response.Score, Source = response.Source, + Contexts = contexts.ToArray(), Parameters = presetResponse.Parameters.Where(x => !String.IsNullOrEmpty(x.Value)).ToDictionary(item => item.Name, item => (object)item.Value) }; return (TResult)(object)aiResponse; } + + private List HandleContexts(IntentResponse response) + { + var newContexts = response.Contexts.Select(x => new AIContext + { + Name = x.Name, + Lifespan = x.Lifespan, + Parameters = response.Parameters.Select(p => new KeyValuePair(p.Name, p.Value)).ToDictionary(d => d.Key, d => d.Value == null ? String.Empty : d.Value) + }).ToList(); + + // persist + + return newContexts; + } } }