Esempio n. 1
0
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        protected static BotApiMessageRequest BuildBotApiMessage(MessageInsertRequest inputModel, string humanId)
        {
            var resultMessage = new BotApiMessageRequest();

            // Populate message values.
            resultMessage.Type = "message";
            resultMessage.Text = inputModel.Content;
            resultMessage.From = new Models.Requests.From
            {
                Id   = humanId,
                Name = "User"
            };

            return(resultMessage);
        }
Esempio n. 2
0
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        public static async Task <bool> GetBotResponse(MessageInsertRequest inputMessage)
        {
            bool   isSender = (inputMessage.SenderId != null && inputMessage.SenderId != "");
            string humanId  = isSender ? inputMessage.SenderId : inputMessage.ReceiverId;

            // Log incoming human message to DB
            InsertMessage(inputMessage);

            // Make sure this wasn't triggered by a bot statement
            if (isSender)
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", BotAuthKey);

                    // If conversation doesn't already exist, create one.
                    if (ApiConversationId == "")
                    {
                        BotConversationIdResponseDomain _Data = null;
                        var values = new Dictionary <string, string>();
                        values.Add("", "");
                        var content = new FormUrlEncodedContent(values);

                        HttpResponseMessage requestConvo = await client
                                                           .PostAsync("https://directline.botframework.com/v3/directline/conversations/", content);

                        if (requestConvo.IsSuccessStatusCode)
                        {
                            string jsonResponse = await requestConvo.Content.ReadAsStringAsync();

                            _Data             = JsonConvert.DeserializeObject <BotConversationIdResponseDomain>(jsonResponse);
                            ApiConversationId = _Data.ConversationId;
                        }
                    }
                }

                //- Build BotApiMessageRequest
                BotApiMessageRequest message = BuildBotApiMessage(inputMessage, humanId);

                //- Send question to bot. Recieve response.
                using (var client = new HttpClient())
                {
                    string uri = String
                                 .Format("https://directline.botframework.com/v3/directline/conversations/{0}/activities", ApiConversationId);

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", BotAuthKey);

                    //- Send question.
                    string postBody = JsonConvert.SerializeObject(message);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage messageResponse = await client.PostAsync(uri, new StringContent(postBody, Encoding.UTF8, "application/json"));

                    //- If the bot recieved the question, ask for its response.
                    if (messageResponse.IsSuccessStatusCode)
                    {
                        BotApiActivitiesDomain _Activites = null;

                        HttpResponseMessage msg = await client.GetAsync(uri);

                        //- If request successful, log it to the DB and GET OUT!
                        if (msg.IsSuccessStatusCode)
                        {
                            string jsonResponse = await msg.Content.ReadAsStringAsync();

                            _Activites = JsonConvert.DeserializeObject <BotApiActivitiesDomain>(jsonResponse);
                            Activity lastMessage = _Activites.Activities.Last <Activity>();
                            if (lastMessage.Text != null)
                            {
                                var messageToInsert = new MessageInsertRequest
                                {
                                    ReceiverId     = humanId,
                                    Content        = lastMessage.Text,
                                    SenderId       = "",
                                    ConversationId = 0
                                };

                                InsertMessage(messageToInsert);

                                return(true);
                            }
                        }
                    }
                }

                //- If you've made it to this point, something has gone wrong.
                var errorMessageToInsert = new MessageInsertRequest
                {
                    ReceiverId     = humanId,
                    Content        = "Error! Error! Does not compute!",
                    SenderId       = "",
                    ConversationId = 0
                };

                InsertMessage(errorMessageToInsert);
            }
            return(false);
        }