bot carousel不在messenger工作

时间:2018-04-05 06:55:01

标签: c# carousel botframework facebook-messenger

我使用Hero Cards创建了一个旋转木马。它在bot框架模拟器中工作正常,但在Facebook messenger中它只显示我的第一个回复"选择一个选项"下面的代码?我错过了什么,messenger是否支持旋转木马?为什么缺少图像和按钮?

Activity replyToConversation = activity.CreateReply("Select an option");
replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel;
replyToConversation.Attachments = new List<Attachment>();

Dictionary<string, string> cardContentList = new Dictionary<string, string>();
cardContentList.Add("Shirt", System.Web.HttpContext.Current.Server.MapPath(@"~\imgs\shirt.jpg"));
cardContentList.Add("shoes", System.Web.HttpContext.Current.Server.MapPath(@"~\imgs\shoes.jpg"));

foreach (KeyValuePair<string, string> cardContent in cardContentList)
{
    List<CardImage> cardImages = new List<CardImage>();
    cardImages.Add(new CardImage(url: cardContent.Value));

    List<CardAction> cardButtons = new List<CardAction>();

    CardAction plButton = new CardAction()
    {
        Value = "nike",
        Type = "postBack",
        Title = "shirt"
    };

    cardButtons.Add(plButton);

    HeroCard plCard = new HeroCard()
    {
        Title = "nike",
        Images = cardImages,
        Buttons = cardButtons
    };

    Attachment plAttachment = plCard.ToAttachment();
    replyToConversation.Attachments.Add(plAttachment);
}
await context.PostAsync(replyToConversation);

1 个答案:

答案 0 :(得分:1)

我试图在我的机器人上实现你的情况,它使用网络上托管的图像,但不使用机器人文件夹中的“本地”图像。

使用本地图像我的机器人上有一个例外,而不仅仅是“选择一个选项”。

此测试的代码:

[Serializable]
public class Dialog49665918 : IDialog<object>
{
    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("Type anything to get user question or 'debug' to get debug version");
        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        var replyToConversation = activity.CreateReply("Select an option");
        replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel;
        replyToConversation.Attachments = new List<Attachment>();

        var cardContentList = new Dictionary<string, string>();

        if (!"debug".Equals(activity.Text, StringComparison.InvariantCultureIgnoreCase))
        {
            cardContentList.Add("Shirt", System.Web.HttpContext.Current.Server.MapPath(@"~\imgs\shirt.jpg"));
            cardContentList.Add("shoes", System.Web.HttpContext.Current.Server.MapPath(@"~\imgs\shoes.jpg"));
        }
        else
        {
            cardContentList.Add("Shirt", "https://media.deparis.me/3257-tm_large_default/tshirt-homme-papa-cool-et-tatoue.jpg");
            cardContentList.Add("shoes", "https://assets.adidas.com/images/w_840,h_840,f_auto,q_auto/d4dd2144b22b41bfbbd5a7ff01674bb3_9366/Superstar_Shoes_White_C77153_01_standard.jpg");
        }

        foreach (var cardContent in cardContentList)
        {
            var cardImages = new List<CardImage>
            {
                new CardImage(url: cardContent.Value)
            };

            var plButton = new CardAction()
            {
                Value = "nike",
                Type = "postBack",
                Title = "shirt"
            };

            var cardButtons = new List<CardAction>
            {
                plButton
            };

            var plCard = new HeroCard()
            {
                Title = "nike",
                Images = cardImages,
                Buttons = cardButtons
            };

            var plAttachment = plCard.ToAttachment();
            replyToConversation.Attachments.Add(plAttachment);
        }
        await context.PostAsync(replyToConversation);
    }
}

证明(“调试”案例,带有互联网图像):

Demo