Example #1
0
        /// <summary>
        /// Post to IBM watson with Authorization and JSON formatted utterances to process
        /// </summary>
        static async Task HTTP_POST(WatsonMessage message)
        {
            if (message.Empty)
            {
                return;
            }

            //Create Json Readable String with user input:
            string result      = String.Empty;
            string jsonContent = String.Empty;

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    // credentials
                    var Auth      = WatsonUsername + ":" + WatsonPassword;
                    var byteArray = Encoding.ASCII.GetBytes(Auth);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

                    // JSON content
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // Make Post call and await response
                    jsonContent = message.ToJSON();
                    using (var response = await client.PostAsJsonAsync(WatsonGatewayUrl, JObject.Parse(jsonContent)))
                    {
                        HttpContent content = response.Content;
                        result = await content.ReadAsStringAsync() ?? " ";

                        UtteranceToneList watsonResponse = JsonConvert.DeserializeObject <UtteranceToneList>(result);

                        // publish results to DB
                        message.PublishWatsonResponse(watsonResponse);
                        _actionsAnalyzed += message.ActionCount;
                    }
                }
            }
            catch (Exception ex)
            {
                WatsonEventLog.WriteEntry("********************: Error during watson analysis:", ex);
                if (!String.IsNullOrEmpty(jsonContent))
                {
                    WatsonEventLog.WriteEntry(jsonContent);
                }
                if (!String.IsNullOrEmpty(result))
                {
                    WatsonEventLog.WriteEntry(result);
                }
                Console.WriteLine(ex.ToString());
            }
        }
Example #2
0
        /// <summary> Write the message results back to the database </summary>
        public void PublishWatsonResponse(UtteranceToneList watsonResponse)
        {
            foreach (UtteranceResponse response in watsonResponse.utterances_tone)
            {
                MessageUtterance messageUtterance = _messageUtterances[response.utterance_id];
                Utterance        sent             = messageUtterance._utterance;
                if (string.CompareOrdinal(sent.text, response.utterance_text) != 0)
                {
                    Debugger.Break();
                }

                ActionToAnalyze action = messageUtterance._action;
                action.AddSentiment(response);
            }

            // update the action with the accumulated results
            foreach (ActionToAnalyze action in _actions)
            {
                PublishActionSentiment(action);
            }
        }