【问题标题】:bot carousel not working in messenger机器人轮播在 Messenger 中不起作用
【发布时间】:2018-04-05 22:03:27
【问题描述】:

我使用英雄卡创建了一个轮播。它在 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);

【问题讨论】:

  • 您可以尝试使用托管在网络上(但不在您的机器人上)的图片吗?
  • 是的……这是为什么呢?我不能使用本地文件夹吗?
  • 当我使用互联网网址获取图片时,它在我身边工作
  • 是的,它可以使用 url,但它不应该也可以使用本地文件夹路径吗?

标签: c# carousel botframework facebook-messenger


【解决方案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);
    }
}

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

【讨论】:

  • 那么唯一的解决方案是删除本地文件夹并使用 Web 中托管的图像。谢谢你的澄清
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 2013-12-31
  • 2018-05-10
  • 2014-12-31
  • 1970-01-01
  • 2019-10-12
相关资源
最近更新 更多