Bot 框架模拟器中未显示 Hero Card

Hero Card not showing in Bot framework's emulator

我是 Bot Framework 的新手,我正在使用 C# 编写一个简单的机器人,它应该 return 一个示例 Hero Card 作为回复。问题是 Hero Card 没有出现在 Bot Framework Channel Emulator 中。这是代码:

    public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> arg)
    {
        var referenceMessage = await arg as IMessageActivity;
        var msg = (Activity)context.MakeMessage();
        Activity replyToConversation = msg.CreateReply($"Buscando resultados para {referenceMessage.Text}");
        replyToConversation.Recipient = msg.From;
        replyToConversation.Type = "message";
        replyToConversation.ReplyToId = referenceMessage.Id;
        replyToConversation.AttachmentLayout = "carousel";
        replyToConversation.Attachments = new List<Attachment>();
        List<CardImage> CardImages = new List<CardImage>();
        CardImages.Add(new CardImage()
        {
            Url = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/BMW-Z4_diagonal_front_at_IAA_2005.jpg/243px-BMW-Z4_diagonal_front_at_IAA_2005.jpg"
        });

        CardAction btnWebsite = new CardAction()
        {
            Type = "openUrl",
            Title = "Open",
            Value = "http://bmw.com"
        };

        HeroCard plCard = new HeroCard()
        {
            Title = $"{referenceMessage.Text}",
            Subtitle = $"Resultados de busqueda para {referenceMessage.Text}",
            Images = CardImages,
            Tap = btnWebsite
        };

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

        //var connector = new ConnectorClient(new Uri(msg.ServiceUrl));
        //var reply = connector.Conversations.SendToConversationAsync(replyToConversation);
    }

如您所见,我一直在尝试使用上下文和连接器,但卡片未显示 up.I已调试应用程序,我可以看到输入信息已被正确捕获

对此有什么想法吗?

我会post你可能的解决方案。

*如果您的函数在某些 IDialog class 中,并且如果您期望某些结果应该如下所示:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)

*第二种解决方案(如果我是你,我会使用它)是从当前上下文创建一条消息。所以你的代码应该是:

 public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> arg)
    {

        var replyToConversation= context.MakeMessage();

        replyToConversation.AttachmentLayout = "carousel";
        replyToConversation.Attachments = new List<Attachment>();
        List<CardImage> CardImages = new List<CardImage>();
        CardImages.Add(new CardImage()
        {
            Url = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/BMW-Z4_diagonal_front_at_IAA_2005.jpg/243px-BMW-Z4_diagonal_front_at_IAA_2005.jpg"
        });

        CardAction btnWebsite = new CardAction()
        {
            Type = "openUrl",
            Title = "Open",
            Value = "http://bmw.com"
        };

        HeroCard plCard = new HeroCard()
        {
            Title = $"{referenceMessage.Text}",
            Subtitle = $"Resultados de busqueda para {referenceMessage.Text}",
            Images = CardImages,
            Tap = btnWebsite
        };

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

注:

而不是

replyToConversation.AttachmentLayout = "carousel",

使用

replyToConversation.AttachmentLayout =  AttachmentLayoutTypes.Carousel;

希望对您有所帮助:)