如何在`Dialog`中使用卡片

时间:2016-11-05 22:58:57

标签: c# bots botframework

我正在尝试使用流畅的API来创建一个简单的流程。但我没有使用纯文本,而是想使用丰富的可视化组件。这是一个例子。

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        var yn = Chain
            .PostToChain()
            .Select(m => CreateYesNoPrompt(activity)) //This is a dialog which should provide some buttons to the user
            .PostToUser()
            .WaitToBot()
            .Select(x => x.Text)
            .Switch
            (
                Chain.Case
                (
                    s => s == "S",
                    new ContextualSelector<string, IDialog<string>>((context, item) => Chain.Return("Yes"))
                ),
                Chain.Default<string, IDialog<string>>((context, text) => Chain.Return("No"))
            )
            .Unwrap()
            .PostToUser();

        await Conversation.SendAsync(activity, () => yn);

        return Request.CreateResponse(HttpStatusCode.OK);
    }


    private static Activity CreateYesNoPrompt(Activity activity)
    {
        var reply = activity.CreateReply();

        var ybutton = new CardAction(type: "postBack", title: "Yes", value: "S");
        var nbutton = new CardAction(type: "postBack", title: "No", value: "N");

        var buttons = new List<CardAction>() { ybutton, nbutton };

        var card = new HeroCard("Would you like to start an order?", "Subtitle", buttons: buttons);

        reply.Attachments = new List<Attachment> { card.ToAttachment() };

        return reply;
    }

机器人正在输出Microsoft.Bot.Connector.Activity,而不是预期的输出,这是ToString()对象的Activity返回。

如何在对话框中使用卡片?

1 个答案:

答案 0 :(得分:1)

这会在传递DialogContext时显示一张卡片:

    private static Activity ShowButtons(IDialogContext context, string strText)
{
    // Create a reply Activity
    Activity replyToConversation = (Activity)context.MakeMessage();
    replyToConversation.Text = strText;
    replyToConversation.Recipient = replyToConversation.Recipient;
    replyToConversation.Type = "message";
    // Call the CreateButtons utility method 
    // that will create 5 buttons to put on the Here Card
    List<CardAction> cardButtons = CreateButtons();
    // Create a Hero Card and add the buttons 
    HeroCard plCard = new HeroCard()
    {
        Buttons = cardButtons
    };
    // Create an Attachment
    // set the AttachmentLayout as 'list'
    Attachment plAttachment = plCard.ToAttachment();
    replyToConversation.Attachments.Add(plAttachment);
    replyToConversation.AttachmentLayout = "list";
    // Return the reply to the calling method
    return replyToConversation;
}

请参阅: Using Images, Cards, Carousels, and Buttons In The Microsoft Bot Framework