示例#1
0
        /*
         * Sort the deserialized cards to a randomized list in the
         * order 'Social -> Image -> Social -> Quote' (if possible)
         */
        private List <BaseCard> SortCards(Cards cards)
        {
            cards.Socials = cards.Socials.OrderBy(a => Guid.NewGuid()).ToList();
            cards.Quotes  = cards.Quotes.OrderBy(a => Guid.NewGuid()).ToList();
            cards.Images  = cards.Images.OrderBy(a => Guid.NewGuid()).ToList();

            List <BaseCard> sortedCards = new List <BaseCard>();

            while (cards.Images.Count + cards.Quotes.Count + cards.Socials.Count > 0)
            {
                if (cards.Socials.Count > 0)
                {
                    SocialCard social = cards.Socials[0];
                    sortedCards.Add(social);
                    cards.Socials.Remove(social);
                }
                if (cards.Images.Count > 0)
                {
                    ImageCard image = cards.Images[0];
                    sortedCards.Add(image);
                    cards.Images.Remove(image);
                }
                if (cards.Socials.Count > 0)
                {
                    SocialCard social = cards.Socials[0];
                    sortedCards.Add(social);
                    cards.Socials.Remove(social);
                }
                if (cards.Quotes.Count > 0)
                {
                    QuoteCard quote = cards.Quotes[0];
                    sortedCards.Add(quote);
                    cards.Quotes.Remove(quote);
                }
            }
            return(sortedCards);
        }
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            try
            {
                bool cardFlag = false;

                //Call to Knowledge Base
                if (turnContext.Activity.TextFormat != null)
                {
                    var userResponse = await QNAMaker.KnowledgeBase(turnContext);

                    await turnContext.SendActivityAsync(userResponse, cancellationToken);

                    return;
                }
                ;


                _userInput = UserInputType(turnContext);
                if (UserResponse())
                {
                    _userResponse = _userInput;
                }
                else
                {
                    _userResponse = JToken.Parse(_userInput);
                }
                dynamic cardType = DetectCardType();


                if (cardType == Card.AdaptiveCard)
                {
                    _currentActiveCard = ConversationalFlow.GetNextCard(_userResponse["button"].ToString()).Result.ToString();
                    if (_currentActiveCard.Contains("Update"))
                    {
                        if (_currentActiveCard == Card.UpdateQuoteCard)
                        {
                            var result = await QuoteCard.GetQuote();

                            if (result.Contains("Error"))
                            {
                                await turnContext.SendActivityAsync(MessageFactory.Text($"{result} {errorHandlingText} "), cancellationToken);

                                return;
                            }
                            ;
                            GetCustomer.Instance.Quote = result;
                            _cardAttachment            = CreateQuoteCardAttachment();
                            cardFlag = true;
                        }
                        else
                        {
                            string customerEmail             = _userResponse["emailAddress"];
                            JToken response                  = (JToken)_userResponse;
                            Option <CustomerDto> customerDto = (response.Count() > 2) ? CustomerInfo.ConvertToObject <CustomerDto>(JsonConvert.SerializeObject(_userResponse)) : await CustomerInfo.GetCustomerAsync(customerEmail);

                            Option <CustomerDto> nextCardDto = await CustomerInfo.GetCustomerAsync(customerEmail);

                            _cardAttachment = nextCardDto.Match(
                                None: () => CreateAdaptiveCardAttachment(Path.Combine(".", "Resources", "LoginWithEmailNotFound.json")),
                                Some: (n) => CreateAdaptiveCardAttachmentDto(n, _currentActiveCard)
                                );
                            //_cardAttachment = CreateAdaptiveCardAttachmentDto(nextCardDto, _currentActiveCard);
                            customerDto.Match(
                                None: () => CreateAdaptiveCardAttachment(Path.Combine(".", "Resources", "LoginWithEmailNotFound.json")),
                                Some: async(c) => await UpdateCustomerAndSaveIfFinal(c)
                                );
                            cardFlag = true;
                        }
                    }
                    else
                    {
                        /*Validate Input and Add User Information*/
                        var result = AddUserInputs(_userResponse);

                        if (result.Contains("Error"))
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Text($"{result} {errorHandlingText}"), cancellationToken);

                            return;
                        }
                        ;

                        if (_currentActiveCard == Card.QuoteCard)
                        {
                            result = await QuoteCard.GetQuote();

                            if (result.Contains("Error"))
                            {
                                await turnContext.SendActivityAsync(MessageFactory.Text($"{result} {errorHandlingText} "), cancellationToken);

                                return;
                            }
                            ;
                            GetCustomer.Instance.Quote = result;
                            _cardAttachment            = CreateQuoteCardAttachment();
                            cardFlag = true;
                        }
                    }
                    if (_currentActiveCard == Card.RentersInsuranceCard)
                    {
                        await QuoteCard.CreatePolicy(); _cardAttachment = CreateRentersInsuranceCardAttachment(); cardFlag = true;
                    }
                    if (!cardFlag)
                    {
                        _cardAttachment = CreateAdaptiveCardAttachment(_currentActiveCard);
                    }
                }
                else
                {
                    _currentActiveCard = ConversationalFlow.GetNextCard(_userResponse.ToString()).Result.ToString();
                    if (_currentActiveCard == Card.EmailConfirmationCard)
                    {
                        if (CreateCustomer.Instance.CustomerId == 0)
                        {
                            var customerInfo = await QuoteCard.CreateCustomerRentersInsurance();

                            var quoteInfo = await QuoteCard.AddCoverage();
                        }
                        _cardAttachment = QuoteCardAttachment(); cardFlag = true;
                    }
                    if (!cardFlag)
                    {
                        _cardAttachment = CreateAdaptiveCardAttachment(_currentActiveCard);
                    }
                }

                await turnContext.SendActivityAsync(MessageFactory.Attachment(_cardAttachment), cancellationToken);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                //ToDO add logger
            }
        }