简体   繁体   English

Bot Framework v4 中的等待方法

[英]Waiting method in Bot Framework v4

In version 3 of the Bot Framework, I could call a card and wait for the user's response:在 Bot Framework 的版本 3 中,我可以调用卡片并等待用户的响应:

context.Call(new MyHeroCardOptions(), MyResumeAfter);
  • Where MyHeroCardOptions is a HeroCard type card and MyResumeAfter is the method that awaits the user's response.其中MyHeroCardOptions是一个 HeroCard 类型的卡片,而MyResumeAfter是等待用户响应的方法。

Can someone guide me how to do that in the Bot Framework V4.有人可以指导我如何在 Bot Framework V4.0 中做到这一点。 Please do not include "ChoicePrompt", my goal is to do with a HeroCard since it is an Attachment.请不要包含“ChoicePrompt”,我的目标是使用 HeroCard,因为它是一个附件。

I am using this form:我正在使用这种形式:

private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            await OptionHeroCard.GetHeroCard(stepContext.Context);
            return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions());
        }

public static class OptionHeroCard
    {
        public static async Task GetHeroCard(ITurnContext context)
        {
            var heroCard = new HeroCard
            {
                Title = "Documentation",
                Subtitle = "Microsoft Bot Framework Documentation",
                Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
                Buttons = new List<CardAction> { 
                    new CardAction(ActionTypes.ImBack, title: "Opción 1", value: "Opción 1"),
                    new CardAction(ActionTypes.ImBack, title: "Opción 2", value: "Opción 2"),
                    new CardAction(ActionTypes.OpenUrl, "Ir a a web", value: "https://docs.microsoft.com/bot-framework"),
                },
            };
            var reply = context.Activity.CreateReply();
            reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            reply.Attachments.Add(heroCard.ToAttachment());
            await context.SendActivityAsync(reply);
        }
    }

I currently use a "TextPrompt" with a "Waterfall" to generate a wait, but I don't know if it's appropriate.我目前使用带有“瀑布”的“TextPrompt”来生成等待,但我不知道它是否合适。

  1. I didn't know that I can include HeroCard in a "choice prompts".我不知道我可以在“选择提示”中包含 HeroCard。 An example would be great.一个例子会很棒。

For bot framework 4.7 preview, I've done it by calling 2 actions对于机器人框架 4.7 预览版,我通过调用 2 个操作来完成

 new SendActivity("@{YourHeroCardTemplate()}"),
 new TextInput()
        {
                Property = "dialog.heroCardResponse",
                Prompt = new ActivityTemplate("Please select an option"),
        },

Hope it can help you the idea for other version of bot framework希望它可以帮助您了解其他版本的机器人框架

If you just want your choices to be turned into a hero card automatically, you can use ListStyle.HeroCard :如果你只是想让你的选择自动变成英雄卡,你可以使用ListStyle.HeroCard

new ChoicePrompt(nameof(ChoicePrompt)) { Style = ListStyle.HeroCard }

If you want to use your own custom hero card, you can do something like this:如果你想使用你自己的自定义英雄卡,你可以这样做:

private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    string[] options = new[] { "Opción 1", "Opción 2" };
    return await stepContext.PromptAsync(nameof(ChoicePrompt), new PromptOptions
    {
        Choices = ChoiceFactory.ToChoices(options),
        Prompt = CreateHeroCardActivity(options),
        Style = ListStyle.None, // We're displaying the choices ourselves so we don't want ChoicePrompt to do it for us
    });
}

private static Activity CreateHeroCardActivity(IEnumerable<string> options)
{
    var heroCard = new HeroCard
    {
        Title = "Documentation",
        Subtitle = "Microsoft Bot Framework Documentation",
        Images = new List<CardImage>
        {
            new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg"),
        },
        Buttons = options.Select(option => new CardAction(ActionTypes.ImBack, title: option, value: option))
            .Append(new CardAction(ActionTypes.OpenUrl, "Ir a a web", value: "https://docs.microsoft.com/bot-framework"))
            .ToList(),
    };

    return MessageFactory.Attachment(heroCard.ToAttachment()) as Activity;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM