/// <summary>
        /// Get Answer Card.
        /// </summary>
        /// <param name="result">Result to be displayed.</param>
        /// <param name="displayPreciseAnswerOnly">Choice whether to display Precise Answer Only or source text along with Precise Answer.</param>
        /// <returns>Message activity for Query Result.</returns>
        public static IMessageActivity GetQnADefaultResponse(QueryResult result, bool displayPreciseAnswerOnly)
        {
            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            var chatActivity = Activity.CreateMessageActivity();

            chatActivity.Text = result.Answer;

            var buttonList = new List <CardAction>();

            if (result?.Context?.Prompts != null &&
                result.Context.Prompts.Any())
            {
                // Add all prompts
                foreach (var prompt in result.Context.Prompts)
                {
                    buttonList.Add(
                        new CardAction()
                    {
                        Value = prompt.DisplayText,
                        Type  = "imBack",
                        Title = prompt.DisplayText,
                    });
                }
            }

            string heroCardText = string.Empty;

            if (!string.IsNullOrWhiteSpace(result?.AnswerSpan?.Text))
            {
                chatActivity.Text = result.AnswerSpan.Text;

                // When the configuration is set to display precise answer only
                if (!displayPreciseAnswerOnly)
                {
                    heroCardText = result.Answer;
                }
            }

            if (buttonList.Any() || !string.IsNullOrWhiteSpace(heroCardText))
            {
                var plCard = new HeroCard();

                if (buttonList.Any())
                {
                    plCard.Buttons = buttonList;
                }

                if (!string.IsNullOrWhiteSpace(heroCardText))
                {
                    plCard.Text = heroCardText;
                }

                // Create the attachment.
                var attachment = plCard.ToAttachment();

                chatActivity.Attachments.Add(attachment);
            }

            return(chatActivity);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get QnAAnswer from the query result.
        /// </summary>
        /// <param name="response">query result response.</param>
        /// <returns>QnAMaker answer.</returns>
        public static QnAMakerAnswer GetQnAAnswerFromResponse(Microsoft.Bot.Builder.AI.QnA.QueryResult response)
        {
            if (response == null)
            {
                return(null);
            }

            var promptOptionDictionary = new Dictionary <int, PromptOption>();
            var selectedResponse       = response;
            var qnaAnswer = new QnAMakerAnswer();

            qnaAnswer.Text = selectedResponse.Answer ?? null;
            foreach (var metadata in selectedResponse.Metadata)
            {
                if (metadata.Name.Contains(Constants.MetadataName.OptionRequery))
                {
                    var index = metadata.Name.Substring(13);
                    if (int.TryParse(index, out var result))
                    {
                        if (promptOptionDictionary.ContainsKey(result))
                        {
                            promptOptionDictionary[result].Requery = metadata.Value;
                        }
                        else
                        {
                            var optionPrompt = new PromptOption();
                            optionPrompt.Requery = metadata.Value;
                            promptOptionDictionary.Add(result, optionPrompt);
                        }
                    }
                }
                else if (metadata.Name.Contains(Constants.MetadataName.Option))
                {
                    var index = metadata.Name.Substring(6);
                    if (int.TryParse(index, out var result))
                    {
                        if (promptOptionDictionary.ContainsKey(result))
                        {
                            promptOptionDictionary[result].Option = metadata.Value;
                        }
                        else
                        {
                            var optionPrompt = new PromptOption();
                            optionPrompt.Option = metadata.Value;
                            promptOptionDictionary.Add(result, optionPrompt);
                        }
                    }
                }
                else if (metadata.Name.Contains(Constants.MetadataName.QnAId))
                {
                    var index = metadata.Name.Substring(5);
                    if (int.TryParse(index, out var result))
                    {
                        int.TryParse(metadata.Value, out var id);
                        if (promptOptionDictionary.ContainsKey(result))
                        {
                            promptOptionDictionary[result].QnAId = id;
                        }
                        else
                        {
                            var optionPrompt = new PromptOption();
                            optionPrompt.QnAId = id;
                            promptOptionDictionary.Add(result, optionPrompt);
                        }
                    }
                }
                else if (metadata.Name == Constants.MetadataName.Requery)
                {
                    qnaAnswer.Requery = metadata.Value ?? null;
                }
                else if (metadata.Name.Contains(Constants.MetadataName.Name))
                {
                    qnaAnswer.Name = metadata.Value ?? null;
                }
                else if (metadata.Name.Contains(Constants.MetadataName.Parent))
                {
                    qnaAnswer.Parent = metadata.Value ?? null;
                }
                else if (metadata.Name.Contains(Constants.MetadataName.Isroot))
                {
                    qnaAnswer.IsRoot = metadata.Value ?? null;
                }
                else if (metadata.Name.Contains(Constants.MetadataName.Flowtype))
                {
                    qnaAnswer.Flowtype = metadata.Value ?? null;
                }
            }

            foreach (var promptOption in promptOptionDictionary)
            {
                qnaAnswer.Options.Add(promptOption.Value);
            }

            qnaAnswer.IsChitChat = IsResponseChitChat(response);
            return(qnaAnswer);
        }
 /// <summary>
 /// Get Card for MultiTurn scenario. (Can be deprecated from 4.10.0 release of sdk).
 /// </summary>
 /// <param name="result">Result to be displayed.</param>
 /// <param name="cardNoMatchText">No match text.</param>
 /// <returns>IMessageActivity.</returns>
 public static IMessageActivity GetQnAPromptsCard(QueryResult result, string cardNoMatchText)
 {
     return(GetQnADefaultResponse(result, true));
 }