英雄卡没有显示在Bot框架的模拟器中

时间:2017-05-11 07:04:17

标签: c# .net bots botframework

我是Bot Framework的新手,我正在使用C#来编写一个简单的机器人,它应该返回一个示例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);
    }

正如你所看到的,我一直在尝试使用上下文和连接器,但卡片没有显示出来。我调试了应用程序,我可以看到输入信息被正确捕获

对此有何想法?

1 个答案:

答案 0 :(得分:2)

我会发布您可能的解决方案。

*如果你的函数在某个IDialog类中,如果你期望某些结果应该是这样的:

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;

希望有所帮助:)