public async Task ReceiveAsync(Message message, CancellationToken cancellationToken)
        {
            Trace.TraceInformation($"From: {message.From} \tContent: {message.Content}");

            var request = new FaqRequest {
                Ask = message.Content.ToString()
            };
            var response = await _faqService.AskThenIAnswer(request);

            if (response.Score >= 0.8)
            {
                await _sender.SendMessageAsync($"{response.Answer}", message.From, cancellationToken);
            }
            else if (response.Score >= 0.5)
            {
                await _sender.SendMessageAsync($"Eu acho que a resposta para o que você precisa é:", message.From, cancellationToken);

                cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(1));
                await _sender.SendMessageAsync($"{response.Answer}", message.From, cancellationToken);
            }
            else
            {
                await _sender.SendMessageAsync($"Infelizmente eu ainda não sei isso! Mas vou me aprimorar, prometo!", message.From, cancellationToken);
            }
        }
Esempio n. 2
0
        protected FaqRequestDetail ToDetauls(FaqRequest request)
        {
            FaqRequestDetail requestDetail = new FaqRequestDetail
            {
                Id         = request.Id,
                Question   = request.Question,
                Status     = request.Status,
                CreatedAt  = request.CreatedAt,
                IsDeleted  = request.IsDeleted,
                AvatarLink = PlatformConfiguration.DefaultAvatarPath
            };

            if (!String.IsNullOrEmpty(request.UserId))
            {
                ApplicationUser user = DataContext.Users.Find(request.UserId);

                if (user != null)
                {
                    requestDetail.CreatedBy = String.Format("{0} {1}", user.LastName, user.FirstName);

                    if (String.IsNullOrWhiteSpace(requestDetail.CreatedBy))
                    {
                        requestDetail.CreatedBy = user.UserName;
                    }
                }
            }

            return(requestDetail);
        }
        public async Task <FaqResponse <string> > AskThenIAnswer(FaqRequest request)
        {
            var cachedResponse = await _cacheService.Get(request.Ask);

            if (cachedResponse != null)
            {
                return(cachedResponse);
            }

            var qnaRequest = new QnaMakerRequest {
                Question = request.Ask
            };
            var requestContent = new StringContent(JsonConvert.SerializeObject(qnaRequest), Encoding.UTF8, "application/json");
            var requestMessage = GetRequestMessage(requestContent);

            var response = await _httpClient.SendAsync(requestMessage);

            var responseContent = await response.Content.ReadAsStringAsync();

            var qnaResponse = JsonConvert.DeserializeObject <QnaMakerResponse>(responseContent);
            var faqResponse = new FaqResponse <string> {
                Answer = HttpUtility.HtmlDecode(qnaResponse.Answer), Score = qnaResponse.Score / 100f
            };

            await _cacheService.AddOrUpdate(request.Ask, faqResponse);

            return(faqResponse);
        }
Esempio n. 4
0
        protected override void OnHandling(RemoveFaqRequestCommand command, CommandResult result)
        {
            FaqRequest request = DataContext.FaqRequests.Find(command.Id);

            if (request != null)
            {
                DataContext.FaqRequests.Remove(request);
            }
        }
        protected override void OnHandling(RecoveryFaqRequestCommand command, CommandResult result)
        {
            FaqRequest request = DataContext.FaqRequests.Find(command.Id);

            if (request != null)
            {
                request.IsDeleted = false;
                DataContext.Entry(request).State = EntityState.Modified;
            }
        }
Esempio n. 6
0
        protected override void OnHandling(OpenFaqRequestCommand command, CommandResult result)
        {
            FaqRequest request = DataContext.FaqRequests.Find(command.Id);

            if (request != null)
            {
                request.Status = FaqRequestStatus.Open;
                DataContext.Entry(request).State = EntityState.Modified;
            }
        }
Esempio n. 7
0
        protected override FindFaqRequestByIdQueryResult OnExecuting(IdCriterion criterion)
        {
            FaqRequest request = DataContext.FaqRequests.Find(criterion.Id);

            FindFaqRequestByIdQueryResult result = new FindFaqRequestByIdQueryResult
            {
                RequestDetail = ToDetauls(request)
            };

            return(result);
        }
Esempio n. 8
0
        protected override void OnHandling(FaqRequestCommand command, CommandResult result)
        {
            FaqRequest request = new FaqRequest
            {
                Question  = command.Question,
                UserId    = UserContext.UserId,
                Status    = FaqRequestStatus.Open,
                CreatedAt = DateTime.Now
            };

            DataContext.FaqRequests.Add(request);
        }
Esempio n. 9
0
        public async Task CallQna_ShouldReturn_Something()
        {
            //Arrange
            var faqRequest = new FaqRequest {
                Ask = "Hi"
            };

            //Act
            var response = await _qnaMakerFaqService.AskThenIAnswer(faqRequest);

            //Assert
            Assert.That(response, Is.Not.Null);
            Assert.That(response.Answer, Is.Not.Null);
            Assert.That(response.Score, Is.Positive);
        }
Esempio n. 10
0
        public async Task CallQna_ShouldReturn_CorrectAnswer(string question, string expectedAnswer, float confidenceThreshold)
        {
            //Arrange
            var faqRequest = new FaqRequest {
                Ask = question
            };
            //Act
            var response = await _qnaMakerFaqService.AskThenIAnswer(faqRequest);

            //Assert
            Assert.That(response, Is.Not.Null);
            Assert.That(response.Answer, Is.Not.Null);
            Assert.That(response.Answer, Is.EqualTo(expectedAnswer));
            Assert.That(response.Score, Is.Positive);
            Assert.That(response.Score, Is.GreaterThan(confidenceThreshold));
        }