Beispiel #1
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 #2
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);
            }
        }