【问题标题】:Add Card attachment to message using Microsoft Bot Framework使用 Microsoft Bot Framework 将卡片附件添加到消息中
【发布时间】:2017-04-22 21:38:04
【问题描述】:

问题:

  • 将卡片附加到响应对话框,以下代码主要取自机器人示例,但在我提取使用的显示逻辑的关键部分中,响应对话框中没有显示卡片。

我在 LUIS Intent 任务中执行附件时遇到问题。

目标

  • 让用户提出 LUIS 无法识别的问题,然后在代码跳转到负责处理无法识别的问题的 LUIS 意图任务时使用帮助卡进行响应。我是否可以考虑其他一些仍然使用卡片的帮助窗口结构?

代码

我的卡片应该从哪里显示

[LuisIntent("None")]    
public async Task NoneHandler(IDialogContext context, LuisResult result) {
        string worriedFace = "\U0001F61F";
        string smilingFace = "\U0001F642";

        await context.PostAsync("I'm sorry, I didn't get that " + worriedFace + '.');
        await context.PostAsync("Here are some things I know how to talk about!" + smilingFace);

        var message = context.MakeMessage();

        var attachment = new CardDialog().ReceiptCard();
        message.Attachments.Add(attachment);

        await context.PostAsync(message);
    }

我创建的要显示的视图对象的卡片类。

namespace LUISBankingBot.Views
{
    using System.Collections.Generic;
    using Microsoft.Bot.Connector;
    using Microsoft.Bot.Builder.Dialogs;
    using System;
    using System.Threading.Tasks;

    public class CardDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            throw new NotImplementedException();
        }

        public Attachment ReceiptCard()
        {
            var receiptCard = new ReceiptCard
            {
                Title = "John Doe",
                Facts = new List<Fact> { new Fact("Order Number", "1234"), new Fact("Payment Method", "VISA 5555-****") },
                Items = new List<ReceiptItem>
                {
                    new ReceiptItem("Data Transfer", price: "$ 38.45", quantity: "368", image: new CardImage(url: "https://github.com/amido/azure-vector-icons/raw/master/renders/traffic-manager.png")),
                    new ReceiptItem("App Service", price: "$ 45.00", quantity: "720", image: new CardImage(url: "https://github.com/amido/azure-vector-icons/raw/master/renders/cloud-service.png")),
                },
                Tax = "$ 7.50",
                Total = "$ 90.95",
                Buttons = new List<CardAction>
                {
                    new CardAction(
                        ActionTypes.OpenUrl,
                        "More information",
                        "https://account.windowsazure.com/content/6.10.1.38-.8225.160809-1618/aux-pre/images/offer-icon-freetrial.png",
                        "https://azure.microsoft.com/en-us/pricing/")
                }
            };

            return receiptCard.ToAttachment();
        }        
    }
}

【问题讨论】:

  • 还有……你遇到了什么问题?
  • 我面临的问题是将卡片附加到响应对话框,以下代码主要取自机器人示例,但不会在响应对话框中显示卡片。
  • 你使用哪个频道?
  • 通过渠道您是指 Microsoft.Bot.Connector 吗?
  • 否;你在模拟器中尝试这个吗?在Facebook? Skype?只发生在收据卡上,或者例如英雄卡也没有显示?

标签: c# bots botframework azure-language-understanding


【解决方案1】:

几件事。首先,当您尝试添加附件时,您可能会遇到空引用异常,因为附件数组尚未初始化。

message.Attachments = new List<Attachment>();

另外,您不需要创建 CardDialog。这是一个有效的示例:

    [LuisIntent("None")]
    public async Task NoneHandler(IDialogContext context, LuisResult result)
    {
        string worriedFace = "\U0001F61F";
        string smilingFace = "\U0001F642";

        await context.PostAsync("I'm sorry, I didn't get that " + worriedFace + '.');
        await context.PostAsync("Here are some things I know how to talk about!" + smilingFace);

        var message = context.MakeMessage();

        var receiptCard = new ReceiptCard
        {
            Title = "John Doe",
            Facts = new List<Fact> { new Fact("Order Number", "1234"), new Fact("Payment Method", "VISA 5555-****") },
            Items = new List<ReceiptItem>
            {
                new ReceiptItem("Data Transfer", price: "$ 38.45", quantity: "368", image: new CardImage(url: "https://github.com/amido/azure-vector-icons/raw/master/renders/traffic-manager.png")),
                new ReceiptItem("App Service", price: "$ 45.00", quantity: "720", image: new CardImage(url: "https://github.com/amido/azure-vector-icons/raw/master/renders/cloud-service.png")),
            },
            Tax = "$ 7.50",
            Total = "$ 90.95",
            Buttons = new List<CardAction>
            {
                new CardAction(
                    ActionTypes.OpenUrl,
                    "More information",
                    "https://github.com/amido/azure-vector-icons/raw/master/renders/traffic-manager.png",
                    "https://azure.microsoft.com/en-us/pricing/")
            }
        };

        message.Attachments = new List<Attachment>();
        message.Attachments.Add(receiptCard.ToAttachment());

        await context.PostAsync(message);
    }

【讨论】:

  • 像魅力一样工作?感谢您的反馈。现在我可以继续我的项目了!
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 2016-08-30
  • 2017-10-28
  • 2017-04-10
  • 1970-01-01
相关资源
最近更新 更多