0

嗨,我正在使用 Microsoftbotframework项目开发一个机器人,因为我正在使用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 上下文中使用卡片作为附件吗?

4

1 回答 1

1

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

进行以下更改以使其正常工作

更改方法签名,如下所示

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;

现在它应该可以工作了,如果您仍然有问题,请在此处发表评论。

-基肖尔

于 2016-07-29T06:14:26.573 回答