如何使用Microsoft Bot Framework循环制作旋转木马

时间:2019-03-18 16:55:06

标签: c# json frameworks botframework bots

尝试使用从API返回的 JSON 创建轮播。我正在尝试返回一个带有多个图像的轮播,您可以滚动查看所有图像。我很确定这是我所缺少的小东西,任何帮助将不胜感激。

                    RootObject result = JsonConvert.DeserializeObject<RootObject>(apiResponse);

                    var card = new HeroCard { };

                    List<CardImage> cardImages = new List<CardImage>();
                    var reply = context.Activity.CreateReply();
                    Attachment plAttachment;

                    for (int i = 0; i < 5; i++)
                    {
                        cardImages.Add(new CardImage(url: "https://image.tmdb.org/t/p/original" + result.results[i].poster_path));                           
                    }

                    for (int x = 0; x < 5; x++)
                    {
                        HeroCard plCard = new HeroCard()
                        {
                            Text = "Test",
                            Title = "Movies",
                            Images = cardImages,

                        };
                        plAttachment = plCard.ToAttachment();
                        reply.Attachments.Add(plAttachment);
                    }
                    reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                    await context.SendActivityAsync(reply);
                }
            }       
        }

        catch (Exception ex)
        {

        }
}

目前,其返回的多个轮播带有相同的图片: Bot Reply

1 个答案:

答案 0 :(得分:0)

您已经想出了此解决方案。它是根据您的问题进行编辑的,大概是因为应该将其发布为答案。因此,如果您想将此问题标记为已回答,可以在此处进行修改(以提高效率)

for (int i = 0; i < result.results.Count(); i++)
{
    var data = result.results[i];
    var heroCard = new HeroCard
    {
        Title = data.title,
        Subtitle = "Rating: " + data.popularity,
        Text = data.overview,
        Images = new List<CardImage> { new CardImage("https://image.tmdb.org/t/p/original" + data.poster_path) },
        Buttons = new List<CardAction> { new CardAction(ActionTypes.OpenUrl, "Book", value: "https://www.odeoncinemas.ie/cinema-tickets") },
    };
    // Add the attachment to our reply.
    reply.Attachments.Add(heroCard.ToAttachment());
}
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
await context.SendActivityAsync(reply);