Пример #1
0
        /// <summary>
        /// To parse the incoming json from QnA Maker in to a reply
        /// </summary>
        /// <param name="customResponse"></param>
        /// <param name="responseJson"></param>
        /// <param name="telemetryClient"></param>
        /// <returns></returns>
        public static Activity getAnswer(Activity customResponse, string responseJson, IBotTelemetryClient telemetryClient)
        {
            Answers   answersHolder = null;
            CardMaker _cardMaker    = new CardMaker(telemetryClient);

            try
            {
                answersHolder = JsonConvert.DeserializeObject <Answers>(responseJson.Trim());
            }
            catch (Exception e)
            {
                telemetryClient.TrackException(e, TelemetryMetaData.BindExceptionEvent(TelemetryKey.JsonDeserializationError, "QnA Answer Json to Answer Model"));
            }

            switch (answersHolder.Type)
            {
            case "Multi_Response":
                customResponse = MessageFactory.Text(answersHolder.Content[RandomSelector.Rand(answersHolder.Content.Count())]);
                break;

            case "Image_Response":
                try
                {
                    var response = customResponse.CreateReply();
                    response.Attachments = new List <Attachment>()
                    {
                        _cardMaker.getCardAttachment(_cardMaker.getImage_ResponseCardJson(answersHolder))
                    };
                    return(response);
                }
                catch (Exception e)
                {
                    telemetryClient.TrackException(e, TelemetryMetaData.BindExceptionEvent(TelemetryKey.CardRenderError, "Image_Response card error", customResponse.Conversation.Id, responseJson));
                }
                break;

            case "Card_Response":
                try
                {
                    var response = customResponse.CreateReply();
                    response.Attachments = new List <Attachment>()
                    {
                        _cardMaker.getCardAttachment(_cardMaker.getCard_ResponseCardJson(answersHolder))
                    };
                    return(response);
                }
                catch (Exception e)
                {
                    telemetryClient.TrackException(e, TelemetryMetaData.BindExceptionEvent(TelemetryKey.CardRenderError, "Card_Response card error", customResponse.Conversation.Id, responseJson));
                }
                break;

            default:
                customResponse = MessageFactory.Text(answersHolder.Content.First());
                break;
            }
            return(customResponse);
        }
Пример #2
0
 /// <summary>
 /// Calls the blob to download the json and send
 /// </summary>
 /// <param name="answerholder"></param>
 /// <returns></returns>
 public String getCard_ResponseCardJson(Answers answerholder)
 {
     try
     {
         string parsedURL = answerholder.Content[RandomSelector.Rand(answerholder.Content.Count())];
         using (WebClient webClient = new WebClient())
         {
             string adaptiveCardJson = webClient.DownloadString(parsedURL);
             return(adaptiveCardJson.Trim());
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Пример #3
0
        /// <summary>
        /// For parsing Image data into a card
        /// </summary>
        /// <param name="answerholder"></param>
        /// <returns></returns>
        public string getImage_ResponseCardJson(Answers answerholder)
        {
            JObject templeteJson = null;

            try
            {
                templeteJson = JObject.Parse(File.ReadAllText(Path.Combine(new string[] { ".", @"wwwroot/Sticker_TextCard.json" })));
                //Editing the templete
                templeteJson["body"][0]["items"][0]["url"]  = answerholder.Media[RandomSelector.Rand(answerholder.Media.Count())] ??= "";
                templeteJson["body"][0]["items"][1]["text"] = answerholder.Content[RandomSelector.Rand(answerholder.Content.Count())] ??= "";
            }
            catch (Exception e)
            {
                throw e;
            }
            return(templeteJson.ToString());
        }