/// <summary> /// make a message from a message template key and data in the context /// </summary> /// <param name="message_key"></param> /// <param name="context"></param> /// <returns></returns> private static string MakeMessageFromKey(string message_key, TalkContext context) { var msg_template = context.CurrentStep.MessageTemplates[message_key]; msg_template = Substitute(msg_template, context.CollectedData); msg_template = Substitute(msg_template, context.Properties); return(msg_template); }
internal static (bool, string) ProcessResponse(TalkContext context, string customerMessage, IServiceProvider serviceProvider) { List <string> botResponse = new List <string>(); // decode their response var responseTokenList = GetNextResponse(context, customerMessage, serviceProvider); // collect all data from tokens var matches = Contains(responseTokenList, context.CurrentStep.DataToCollect); // report any failures var fail_matches = matches.Where(x => x.Property.Result == CollectProperty.CollectionResult.Fail).ToList(); if (fail_matches != null && fail_matches.Count > 0) { return(true, $"Escalated because of {fail_matches.Count} rejection tokens"); } // find warnings var warn_matches = matches.Where(x => x.Property.Result == CollectProperty.CollectionResult.Warning).ToList(); if (warn_matches != null && warn_matches.Count > 0) { foreach (var warning in warn_matches) { // remove any collected data that matches this property name if (!string.IsNullOrEmpty(warning.Property.PropertyName)) { matches.RemoveAll(x => x.Property.Result == CollectProperty.CollectionResult.Collect && x.Property.PropertyName == warning.Property.PropertyName); } botResponse.Add(MakeMessageFromKey(warning.Property.CapturedTemplate, context)); } } var collect_matches = matches.Where(x => x.Property.Result == CollectProperty.CollectionResult.Collect).ToList(); var reqrd_matches = context.CurrentStep.DataToCollect.Where(x => x.Result == CollectProperty.CollectionResult.Collect).ToList(); bool got_everything = (reqrd_matches.Count == context.CollectedData.Count); if (collect_matches != null && collect_matches.Count > 0) { if (!string.IsNullOrEmpty(context.CurrentStep.CapturedPrompt)) { var msg = MakeMessageFromKey(context.CurrentStep.CapturedPrompt, context); botResponse.Add(msg); } foreach (var collect in collect_matches) { var key = collect.Property.PropertyName; if (context.CollectedData.ContainsKey(key)) { context.CollectedData.Remove(key); } context.CollectedData.Add(collect.Property.PropertyName, collect.MatchingTokens.First().Text); // show message detailing what I got tis time around if (!string.IsNullOrEmpty(context.CurrentStep.CapturedPrompt)) { var msg = MakeMessageFromKey(collect.Property.CapturedTemplate, context); botResponse.Add(msg); } } } // got it all?? if (got_everything) { botResponse.Add(MakeMessageFromKey(context.CurrentStep.CompletePrompt, context)); return(true, string.Join("\n", botResponse)); } // find missing data var missing = new List <CollectProperty>(); foreach (var required in context.CurrentStep.DataToCollect.Where(x => x.Optional == false && x.Result == CollectProperty.CollectionResult.Collect)) { if (!context.CollectedData.ContainsKey(required.PropertyName)) { missing.Add(required); } } if (missing.Count > 1) { botResponse.Add(MakeMessageFromKey(context.CurrentStep.InCompleteManyPrompt, context)); for (int i = 1; i <= missing.Count; i++) { botResponse.Add($"{i}) " + MakeMessageFromKey(missing[i - 1].PromptTemplate, context)); } } if (missing.Count == 1) { var s1 = MakeMessageFromKey(context.CurrentStep.InCompleteSinglePrompt, context); var s2 = MakeMessageFromKey(missing[0].PromptTemplate, context); botResponse.Add(s1 + s2); } return(false, string.Join("\n", botResponse)); }
private static List <Token> GetNextResponse(TalkContext context, string customerMessage, IServiceProvider serviceProvider) { var flattenedTokens = Parser.ParseText(context.Properties, customerMessage, serviceProvider); return(flattenedTokens.MostLikely(context.CurrentStep.DataToCollect)); }