如何在 IDialog 上下文中使用 ThumbnailCard

How to use the ThumbnailCard in IDialog Context

您好,我正在使用 Microsoft botframework 项目开发一个机器人,因为我正在使用 IDialog 界面。因为我使用 ThumbnailCard 来显示卡片。在这里,当我将一些数据附加到我的卡片上并且数据附加正确时,但在 PostAsync 方法中它没有提供回复。

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        ThumbnailCard plCard = null;
        IMessageActivity replyToConversation =await argument;
        replyToConversation.Type = "message";
        replyToConversation.Attachments = new List<Attachment>();
        replyToConversation.Text = "welcome to book my show";
        Dictionary<string, string> cardContentList = new Dictionary<string, string>();
        cardContentList.Add("Jason Bourne", "URL");
        cardContentList.Add("The Land", "URL");
        cardContentList.Add("Yoga Hosers", "URL");
        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>();
            if (cardContent.Key == "Jason Bourne")
            {
                CardAction plButton1 = new CardAction()
                {
                     Value = $"",
                    Type = "openUrl",
                    Title = "Book Now"
                };

                CardAction plButton2 = new CardAction()
                {
                    Value = "tel:1-800-800-5705",
                    Type = "call",
                    Title = "Show timings"
                };
                cardButtons.Add(plButton1);
                cardButtons.Add(plButton2);

                plCard = new ThumbnailCard()
                {
                    Title = $"Jason Bourne",
                    Subtitle = " ",
                    Images = cardImages,
                    Buttons = cardButtons,

                };
                Attachment plAttachment = plCard.ToAttachment();
                replyToConversation.Attachments.Add(plAttachment);
            }
            else if (cardContent.Key == "The Land")
            {
                CardAction plButton1 = new CardAction()
                {
                     Value = $"",
                    Type = "openUrl",
                    Title = "Book Now"
                };
                CardAction plButton2 = new CardAction()
                {
                    Value = "tel:1-800-800-5705",
                    Type = "call",
                    Title = "Show Timings"
                };
                cardButtons.Add(plButton1);
                cardButtons.Add(plButton2);

                plCard = new ThumbnailCard()
                {
                    Title = $"The Land",
                    Subtitle = "",
                    Images = cardImages,
                    Buttons = cardButtons,

                };
                Attachment plAttachment = plCard.ToAttachment();
                replyToConversation.Attachments.Add(plAttachment);
            }
            else if (cardContent.Key == "Yoga Hosers")
            {

                CardAction plButton1 = new CardAction()
                {
                     Value = $"",
                    Type = "openUrl",
                    Title = "Book Now"
                };
                CardAction plButton2 = new CardAction()
                {
                    Value = "tel:1-800-800-5705",
                    Type = "call",
                    Title = "Show timings"
                };
                cardButtons.Add(plButton1);
                cardButtons.Add(plButton2);

                plCard = new ThumbnailCard()
                {
                    Title = $"Yoga Hosers",
                    Subtitle = "",
                    Images = cardImages,
                    Buttons = cardButtons,
                };
                Attachment plAttachment = plCard.ToAttachment();
                replyToConversation.Attachments.Add(plAttachment);
            }
        }
        replyToConversation.AttachmentLayout = AttachmentLayoutTypes.List;
        await context.PostAsync(replyToConversation);
    }       

当我 运行 机器人显示以下错误

我们可以在 IDialog 上下文 中使用卡片作为附件吗?

问题出在 IMessageActivity 上,您正试图在 context.PostAsync 中发送 IMessageActivity。这就是它失败的原因。

进行以下更改以使其工作

像下面这样更改方法签名

private async Task messageReceived(IDialogContext context, IAwaitable<object> argument)

并将IMessageActivity replyToConversation =await argument;修改为喜欢下面

var message = await argument as Activity;           
        Activity replyToConversation = message.CreateReply("Welcome." + "(Hi)");
        replyToConversation.Recipient = message.From;

现在应该可以了,如果还有问题请在这里评论。

-基肖尔