如何在IDialog上下文

时间:2016-07-28 12:08:27

标签: bots botframework botbuilder

您好我正在使用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);
    }       

当我运行机器人时,它显示以下错误 enter image description here

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

1 个答案:

答案 0 :(得分: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;

现在它应该有效,如果你还有问题请在这里发表评论。

-Kishore