Exemplo n.º 1
0
        public async Task SearchRecipe(IDialogContext context, LuisResult result)
        {
            try
            {
                var searchModel = CreateSearchModel(result);

                ListViewModel <ShortRecipeViewModel> recipes = null;
                using (var client = new GatewayClient())
                {
                    recipes = await client.GetRecipes(searchModel);
                }

                IMessageActivity reply = context.MakeMessage();

                reply.Recipient = context.Activity.From;
                reply.Type      = ActivityTypes.Message;

                if (recipes.Entries.Any())
                {
                    await context.PostAsync(Resources.Message_RecipesFound);

                    reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                    foreach (var shortRecipeViewModel in recipes.Entries)
                    {
                        var attachment = shortRecipeViewModel.ToAttachment(String.Format(RecipeStartStringFormat, shortRecipeViewModel.Id));
                        reply.Attachments.Add(attachment);
                    }

                    //recipes.Entries.Select(x => x.ToAttachment(String.Format(RecipeStartStringFormat, x.Id))).ForEach(x => reply.Attachments.Add(x));
                    await context.PostAsync(reply);
                }
                else
                {
                    await context.PostAsync(Resources.Message_Error_NoRecipesFound);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                await context.PostAsync(Resources.Message_Error_SearchRecipeError);
            }


            context.Wait(MessageReceived);
        }