private async Task FeedbackReceived(IDialogContext context, IAwaitable <Message> result)
        {
            var message = await result;

            TextAnalyticsMessage textAnalyticsMessage = new TextAnalyticsMessage();

            textAnalyticsMessage.Documents.Add(new TextAnalyticsDocument()
            {
                Id   = Guid.NewGuid().ToString(),
                Text = message.Text
            });

            Dictionary <TextAnalyticsResultType, TextAnalyticsResult> textAnalyticsResult = await TextAnalyticsClient.SendRequest(BaseUrl, AccountKey, NumLanguages, textAnalyticsMessage);

            Message replyMessage = message.CreateReplyMessage($"{FormatResultMessage(textAnalyticsResult)}");

            replyMessage.SetBotPerUserInConversationData("sentimentScore", textAnalyticsResult[TextAnalyticsResultType.Sentiment].Documents.FirstOrDefault().Score);
            await context.PostAsync(replyMessage);

            context.Wait(MessageReceived);
        }
Beispiel #2
0
        public static async Task <Dictionary <TextAnalyticsResultType, TextAnalyticsResult> > SendRequest(string baseUrl, string accountKey, int noOfLanguages, TextAnalyticsMessage documents)
        {
            Dictionary <TextAnalyticsResultType, TextAnalyticsResult> result = new Dictionary <TextAnalyticsResultType, TextAnalyticsResult>();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", accountKey);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(documents));

                var uri      = "text/analytics/v2.0/keyPhrases";
                var response = await CallEndpoint(client, uri, byteData);

                result.Add(TextAnalyticsResultType.KeyPhrases, JsonConvert.DeserializeObject <TextAnalyticsResult>(response));

                var queryString = HttpUtility.ParseQueryString(string.Empty);
                queryString["numberOfLanguagesToDetect"] = noOfLanguages.ToString(CultureInfo.InvariantCulture);
                uri      = "text/analytics/v2.0/languages?" + queryString;
                response = await CallEndpoint(client, uri, byteData);

                result.Add(TextAnalyticsResultType.Languages, JsonConvert.DeserializeObject <TextAnalyticsResult>(response));

                uri      = "text/analytics/v2.0/sentiment";
                response = await CallEndpoint(client, uri, byteData);

                result.Add(TextAnalyticsResultType.Sentiment, JsonConvert.DeserializeObject <TextAnalyticsResult>(response));

                return(result);
            }
        }