public async Task ShowWishesAsync(IDialogContext context, IAwaitable <IMessageActivity> activity)
        {
            List <string> wishes = StateHelper.GetWishlistItems(context);

            //Retrive wishes information
            var to_retrieve = wishes;

            //options
            List <ButtonType> buttons = new List <ButtonType>();

            //fetch only a limited number of wishes
            if (wishes.Count() > Constants.N_ITEMS_CARROUSSEL)
            {
                to_retrieve = wishes.Skip(this.skip)
                              .Take(Constants.N_ITEMS_CARROUSSEL)
                              .ToList();
            }

            var products = new List <Product>();

            foreach (string i in to_retrieve)
            {
                products.Add(ProductController.getProduct(i));
            }

            //Prepare answer

            var reply = context.MakeMessage();
            var text  = "";

            var button_text = "";

            // No products on wishlsit
            if (products.Count == 0)
            {
                text = Interactions.getWishList(Interactions.State.FAIL, 0);
                await context.PostAsync(text);
            }
            // Has Products
            else
            {
                text = Interactions.getWishList(Interactions.State.SUCCESS, skip / Constants.N_ITEMS_CARROUSSEL + 1);
                await Interactions.SendMessage(context, text, 0, 2500);

                //display products
                reply = context.MakeMessage();
                reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                List <Attachment> cards = new List <Attachment>();

                //limit
                for (var i = 0; i < products.Count && i < Constants.N_ITEMS_CARROUSSEL; i++)
                {
                    cards.Add(ProductCard.GetProductCard(products[i], ProductCard.CardType.WISHLIST).ToAttachment());
                }

                reply.Attachments = cards;
                await context.PostAsync(reply);

                //Check if pagination is needed and display wishes
                if (wishes.Count() > this.skip + Constants.N_ITEMS_CARROUSSEL)
                {
                    buttons.Add(ButtonType.PAGINATION);
                    skip        += skip + Constants.N_ITEMS_CARROUSSEL;
                    button_text += "Não consegui exibir todos os seus produtos favoritos. Se desejar ver mais clique no botão abaixo.";
                }
            }

            //add option add more products
            buttons.Add(ButtonType.ADD_PRODUCT);
            button_text += "\nSe desejar adicionar mais produtos aos seus favoritos faça uma pesquisa no nosso catálogo. Para isso, clique no botão abaixo.";

            await Interactions.SendMessage(context, button_text, 2000, 2000);

            //show options
            reply = context.MakeMessage();
            reply.Attachments.Add(getCardButtonsAttachment(buttons, DialogType.WISHLIST));
            await context.PostAsync(reply);

            context.Wait(this.InputHandler);
        }
Beispiel #2
0
        public async Task FilterAsync(IDialogContext context, IAwaitable <IMessageActivity> activity)
        {
            List <Filter> filters = StateHelper.GetFilters(context);

            // search products based on the last fetch id (inclusive)
            // search will fetch +1 product to know if pagination is needed
            List <Product> products = ProductController.getProductsFilter(
                FilterLogic.GetJoinedFilter(filters),
                Constants.N_ITEMS_CARROUSSEL + 1,
                last_fetch_id);

            if (products.Count > 1)
            {
                last_fetch_id = products[products.Count - 2].Id;
            }

            var reply = context.MakeMessage();
            var text  = "";

            if (products.Count > 0)
            {
                text = Interactions.getFilter(Interactions.State.SUCCESS, page) + "  \n";
            }
            else
            {
                text = Interactions.getFilter(Interactions.State.FAIL, page) + "  \n";
            }

            //display current filters
            for (int i = 0; i < filters.Count; i++)
            {
                text += filters[i].FilterName + filters[i].Operator + filters[i].Value;
                if (i != filters.Count - 1)
                {
                    text += ", ";
                }
            }

            await Interactions.SendMessage(context, text, 2000, 3000);

            bool done = false;
            List <ButtonType> buttons = new List <ButtonType>();

            string button_text = "";

            //show products
            if (products.Count > 0)
            {
                //display products
                reply = context.MakeMessage();
                reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                List <Attachment> cards = new List <Attachment>();

                //limit
                for (var i = 0; i < products.Count && i < Constants.N_ITEMS_CARROUSSEL; i++)
                {
                    cards.Add(ProductCard.GetProductCard(products[i], ProductCard.CardType.SEARCH).ToAttachment());
                }

                reply.Attachments = cards;
                await context.PostAsync(reply);

                //Check if pagination is needed
                if (products.Count > Constants.N_ITEMS_CARROUSSEL)
                {
                    button_text = "Não consegui trazer todos os produtos do catálogo com esses requisitos. Se quiser ver mais produtos clique no botão abaixo.";
                    buttons.Add(ButtonType.PAGINATION);
                }
            }

            button_text += "\nPara alterar os parâmetros da pesquisa carregue no respetivo botão.";
            await Interactions.SendMessage(context, button_text, 2000, 2000);

            buttons.Add(ButtonType.FILTER_AGAIN);

            //show options
            reply = context.MakeMessage();
            reply.Attachments.Add(getCardButtonsAttachment(buttons, DialogType.FILTER));
            await context.PostAsync(reply);

            context.Wait(this.InputHandler);
        }
Beispiel #3
0
        public static void ShowProductComparison(IDialogContext context, List <Product> productsToCompare)
        {
            Dictionary <ComparatorLogic.Parts, List <int> > comparisonResults = GetBestProduct(productsToCompare); //TODO VERIFICAR RESULTS


            //size of products to show on result(top 3 if >3)

            /* var resultSize = 0;
             * if (productsToCompare.Count > 3)
             *   resultSize = 3;
             * else */var resultSize = productsToCompare.Count;

            var reply = context.MakeMessage();

            //Sends a reply for each specification compared and shows the products(best ones first)
            foreach (KeyValuePair <ComparatorLogic.Parts, List <int> > entry in comparisonResults)
            {
                reply      = context.MakeMessage();
                reply.Text = "### " + Interactions.getComparator(entry.Key.ToString()) + "  \n";

                for (int i = 0; i < resultSize && i < 7; i++)
                {
                    reply.Text += i + 1 + ". " + productsToCompare[entry.Value[i]].Brand + " " + productsToCompare[entry.Value[i]].Model + ";  \n";
                }
                //Send individual classification
                context.PostAsync(reply);
            }



            List <double> overallBest = new List <double>(new double[productsToCompare.Count]);
            var           bestInPart  = new Product();
            var           product     = new Product();

            //calculate score for each product
            foreach (KeyValuePair <ComparatorLogic.Parts, List <int> > entry in comparisonResults)
            {
                for (int i = 0; i < productsToCompare.Count; i++)
                {
                    //RAM
                    if (entry.Key == ComparatorLogic.Parts.RAM)
                    {
                        product = productsToCompare[entry.Value[i]];
                        if (i == 0)
                        {
                            bestInPart = productsToCompare[entry.Value[i]];
                        }

                        overallBest[entry.Value[i]] += (double)(product.RAM * 0.15 / bestInPart.RAM);
                    }
                    //PRICE
                    else if (entry.Key == ComparatorLogic.Parts.Price)
                    {
                        product = productsToCompare[entry.Value[i]];
                        if (i == 0)
                        {
                            bestInPart = productsToCompare[entry.Value[i]];
                        }
                        overallBest[entry.Value[i]] += (double)(1 - (Math.Abs(bestInPart.Price - product.Price) * 0.3 / bestInPart.Price));
                    }
                    //GPU
                    else if (entry.Key == ComparatorLogic.Parts.GPU)
                    {
                        overallBest[entry.Value[i]] += (double)((productsToCompare.Count - i) * 0.2);
                    }
                    //CPU
                    else if (entry.Key == ComparatorLogic.Parts.CPU)
                    {
                        overallBest[entry.Value[i]] += (double)((productsToCompare.Count - i) * 0.25);
                    }
                    //SCREEN
                    else if (entry.Key == ComparatorLogic.Parts.Screen)
                    {
                        overallBest[entry.Value[i]] += (double)((productsToCompare.Count - i) * 0.1);
                    }
                }
            }

            List <Attachment>        cards  = new List <Attachment>();
            Dictionary <int, double> result = new Dictionary <int, double>();

            for (int i = 0; i < overallBest.Count; i++)
            {
                result.Add(i, overallBest[i]);
            }

            //sort by descending score
            IEnumerable <KeyValuePair <int, double> > sortedResult = result.OrderByDescending(i => i.Key);

            int keyBestProduct       = 0;
            int keySecondBestProduct = 0;

            //add products to cards
            foreach (KeyValuePair <int, double> kvp in sortedResult)
            {
                if (keyBestProduct == 0)
                {
                    keyBestProduct = kvp.Key;
                }
                else
                {
                    if (keySecondBestProduct == 0)
                    {
                        keySecondBestProduct = kvp.Key;
                    }
                }

                cards.Add(ProductCard.GetProductCard(productsToCompare[kvp.Key], ProductCard.CardType.COMPARATOR).ToAttachment());
            }

            string response = "";

            if (keyBestProduct != 0)
            {
                string bestProductModel = productsToCompare[keyBestProduct].Brand + " " + productsToCompare[keyBestProduct].Model;

                response += "O melhor computador pelo preço é o " + bestProductModel + ".";

                if (keySecondBestProduct != 0)
                {
                    string secondBestProductModel = productsToCompare[keySecondBestProduct].Brand + " " + productsToCompare[keySecondBestProduct].Model;
                    response += "A alternativa é o " + secondBestProductModel + ".";
                }
            }

            context.PostAsync(response);

            reply                  = context.MakeMessage();
            reply.Attachments      = cards;
            reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            context.PostAsync(reply);
            cards.Clear();
        }
Beispiel #4
0
        public async Task InitAsync(IDialogContext context, IAwaitable <IMessageActivity> activity)
        {
            List <ButtonType> buttons = new List <ButtonType>();

            // fetch products
            var itemsToCompare = StateHelper.GetComparatorItems(context);

            foreach (string o in itemsToCompare)
            {
                products.Add(ProductController.getProduct(o));
            }

            var reply = context.MakeMessage();

            string intro_msg  = "Bem-vindo ao comparador. Aqui posso dar-lhe sugestões sobre quais os melhores produtos que deseja comparar.";
            string button_msg = "";

            if (products.Count > 0)
            {
                intro_msg = "\nVou buscar os produtos em que demonstrou interesse em avaliar. Espere só um momento por favor.";
                await Interactions.SendMessage(context, intro_msg, 0, 0);

                //display products
                reply = context.MakeMessage();
                reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                List <Attachment> cards = new List <Attachment>();

                //limit
                for (var i = 0; i < products.Count && i < Constants.N_ITEMS_CARROUSSEL; i++)
                {
                    cards.Add(ProductCard.GetProductCard(products[i], ProductCard.CardType.COMPARATOR).ToAttachment());
                }

                reply.Attachments = cards;

                await Interactions.SendMessage(context, "Aqui estão os produtos:", 4000, 2000);

                await context.PostAsync(reply);

                if (products.Count <= ComparatorLogic.MAX_PRODUCTS_ON_COMPARATOR)
                {
                    buttons.Add(ButtonType.ADD_PRODUCT);
                }

                buttons.Add(ButtonType.COMPARE);

                button_msg = "Se quiser posso fazer uma avaliação dos produtos, clique no botão abaixo para que eu iniciar a comparação.";
            }
            else
            {
                intro_msg = "\nDe momentos ainda não adicionou nenhum produto para ser avaliado.";
                await Interactions.SendMessage(context, intro_msg, 0, 3000);

                buttons.Add(ButtonType.ADD_PRODUCT);
            }

            button_msg += "\nSe tiver interesse em adicionar produtos para serem avaliados, faça uma pesquisa no nosso catálogo. Para isto, clique no botão abaixo para preencher um formulário de pesquisa.";
            await Interactions.SendMessage(context, button_msg, 3000, 2000);

            //show options
            reply = context.MakeMessage();
            reply.Attachments.Add(getCardButtonsAttachment(buttons, DialogType.COMPARE));
            await context.PostAsync(reply);

            context.Wait(InputHandler);
        }
Beispiel #5
0
        private async Task ShowRecommendations(IDialogContext context, IAwaitable <object> result)
        {
            await Interactions.SendMessage(context, "Tenho tantos produtos que sei que poderiam ser do seu interesse. Espere só um momento, vou analisar o nosso catálogo.", 0, 4000);

            List <Product> products = new List <Product>();
            List <Filter>  popular  = RecommendationsLogic.GetPopularFilters(StateHelper.GetFiltersCount(context));

            while (true)
            {
                FilterDefinition <Product> joinedFilters = FilterLogic.GetJoinedFilter(popular);

                //fetch +1 product to see if pagination is needed
                products = ProductController.getProductsFilter(
                    joinedFilters,
                    Constants.N_ITEMS_CARROUSSEL + 1,
                    this.lastFetchId);

                //filters didn't retrieved any products at the first try
                if (products.Count == 0 && lastFetchId == null)
                {
                    popular.RemoveAt(popular.Count - 1);
                }
                else
                {
                    break;
                }
            }

            List <string> currentWishlist = StateHelper.GetWishlistItems(context);

            foreach (string str in currentWishlist)
            {
                ObjectId       obj = new ObjectId(str);
                List <Product> l   = Logic.RecommendationsLogic.GetSimilarProducts(obj);
                products.InsertRange(0, l);
            }

            if (products.Count > Constants.N_ITEMS_CARROUSSEL)
            {
                lastFetchId = products[products.Count - 2].Id;
            }

            var reply = context.MakeMessage();

            reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            List <Attachment> cards = new List <Attachment>();

            for (int i = 0; i < products.Count && i < Constants.N_ITEMS_CARROUSSEL; i++)
            {
                cards.Add(ProductCard.GetProductCard(products[i], ProductCard.CardType.RECOMMENDATION).ToAttachment());
            }

            await Interactions.SendMessage(context, "Estas são as minhas recomendações:", 0, 2000);

            reply.Attachments = cards;
            await context.PostAsync(reply);

            //Check if pagination is needed
            if (products.Count <= Constants.N_ITEMS_CARROUSSEL)
            {
                context.Done(new CODE(DIALOG_CODE.DONE));
            }
            else
            {
                await Interactions.SendMessage(context, "Ainda tenho mais recomendações para si. Se for do seu interesse não hesite, carregue no botão abaixo.", 2000, 2000);

                reply = context.MakeMessage();

                reply.Attachments.Add(
                    getCardButtonsAttachment(new List <ButtonType> {
                    ButtonType.PAGINATION
                }, DialogType.RECOMMENDATION));
                await context.PostAsync(reply);


                context.Wait(this.InputHandler);
            }
        }