Esempio n. 1
0
        private async Task ShowAmazonProductsAsync(ITurnContext turnContext, CustomSearch customSearch, CancellationToken cancellationToken)
        {
            var    searchIndexs = _shoppingHelper.GetSearchIndexDictionary(customSearch.Age, customSearch.Gender);
            Random random       = new Random();
            int    randomNumber = random.Next(0, searchIndexs.Keys.Count);

            int index  = searchIndexs.ElementAt(randomNumber).Key;
            var items1 = _shopping.SearchItems("", (Enums.SearchIndex)index, 0, true, 0, Convert.ToInt32(customSearch.Price), 1, Enums.SearchSort.Salesrank, Enums.SearchSortOrder.Ascending);

            randomNumber = random.Next(0, searchIndexs.Keys.Count);
            index        = searchIndexs.ElementAt(randomNumber).Key;

            var items2 = _shopping.SearchItems("", (Enums.SearchIndex)index, 0, true, 0, Convert.ToInt32(customSearch.Price), 1, Enums.SearchSort.Salesrank, Enums.SearchSortOrder.Ascending);

            randomNumber = random.Next(0, searchIndexs.Keys.Count);
            index        = searchIndexs.ElementAt(randomNumber).Key;
            var items3 = _shopping.SearchItems("", (Enums.SearchIndex)index, 0, true, 0, Convert.ToInt32(customSearch.Price), 1, Enums.SearchSort.Salesrank, Enums.SearchSortOrder.Ascending);


            List <Item> items = new List <Item>();

            items.AddRange(items1.Items.Where(x => x.Price < customSearch.Price).OrderBy(x => random.Next()).Take(5));
            items.AddRange(items2.Items.Where(x => x.Price < customSearch.Price).OrderBy(x => random.Next()).Take(5));
            items.AddRange(items3.Items.Where(x => x.Price < customSearch.Price).OrderBy(x => random.Next()).Take(5));



            List <Attachment> attachments = new List <Attachment>();

            foreach (var item in items)
            {
                attachments.Add(
                    new HeroCard(
                        title: item.Title,
                        subtitle: $"Prezzo € {item.Price}",
                        images: new CardImage[] { new CardImage(url: item.ImageUrl, alt: item.Title) },
                        buttons: new CardAction[]
                {
                    new CardAction(title: "Compra", type: ActionTypes.MessageBack, value: item.ItemId)
                })
                    .ToAttachment()
                    );
            }
            var activity = MessageFactory.Carousel(attachments);

            // Send the activity as a reply to the user.
            await turnContext.SendActivityAsync(activity);
        }
Esempio n. 2
0
        private async Task <DialogTurnResult> CustomSearchResultAsync(
            WaterfallStepContext stepContext,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var price = (decimal)stepContext.Result;

            stepContext.Values["price"] = price;

            // Return the collected information to the parent context.
            CustomSearch customSearch = new CustomSearch
            {
                Price  = price,
                Age    = (int)stepContext.Values["age"],
                Gender = (stepContext.Values["gender"].ToString().ToLower() == "donna") ? Enums.Gender.female: Enums.Gender.male
            };

            return(await stepContext.EndDialogAsync(customSearch, cancellationToken));
        }
Esempio n. 3
0
        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // Get the current reservation info from state.
                CustomSearch customSearch = await _accessors.CustomSearchAccessor.GetAsync(
                    turnContext, () => null, cancellationToken);

                // Generate a dialog context for our dialog set.
                DialogContext dc = await _dialogSet.CreateContextAsync(turnContext, cancellationToken);

                if (turnContext.Activity.Text == "Start")
                {
                    await dc.CancelAllDialogsAsync(cancellationToken);

                    customSearch = null;
                    await _accessors.CustomSearchAccessor.SetAsync(
                        turnContext,
                        customSearch,
                        cancellationToken);

                    await dc.CancelAllDialogsAsync(cancellationToken);
                }

                if (dc.ActiveDialog is null)
                {
                    if (customSearch is null)
                    {
                        // If not, start the dialog.
                        await dc.BeginDialogAsync(CustomSearchDialog, null, cancellationToken);
                    }
                    else
                    {
                        // Otherwise, send a status message.
                        customSearch = null;
                        await _accessors.CustomSearchAccessor.SetAsync(
                            turnContext,
                            customSearch,
                            cancellationToken);

                        await dc.CancelAllDialogsAsync(cancellationToken);
                        await ShowStartAsync(turnContext, cancellationToken);
                    }
                }
                else
                {
                    // Continue the dialog.
                    DialogTurnResult dialogTurnResult = await dc.ContinueDialogAsync(cancellationToken);

                    // If the dialog completed this turn, record the reservation info.
                    if (dialogTurnResult.Status is DialogTurnStatus.Complete)
                    {
                        customSearch = (CustomSearch)dialogTurnResult.Result;
                        await _accessors.CustomSearchAccessor.SetAsync(
                            turnContext,
                            customSearch,
                            cancellationToken);

                        await ShowAmazonProductsAsync(turnContext, customSearch, cancellationToken);
                        await ShowStartAsync(turnContext, cancellationToken);
                    }
                }

                // Save the updated dialog state into the conversation state.
                await _accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
            }
            else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
            {
                if (turnContext.Activity.MembersAdded != null)
                {
                    await SendWelcomeMessageAsync(turnContext, cancellationToken);
                }
            }
            //else
            //{
            //    await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected");
            //}
        }