How to reprompt in botframework v4?

元气小坏坏 提交于 2019-12-13 03:44:22

问题


I can't make RepromptDialogAsync() to work. When dialog b is chosen it should re-prompt the choice prompt showing all the choices again. But when choosing dialog b it is doing nothing. Am I doing it wrong? I can't find any RepromptDialogAsync() tutorial on docs. Any help would be greatly appreciated. Thank you!

Code:

public class MainDialog : ComponentDialog
{
    private const string InitialId = "mainDialog";
    private const string ChoicePrompt = "choicePrompt";
    private const string DialogAId = "dialogAId";

    public MainDialog(string dialogId)
        : base(dialogId)
    {
        WaterfallStep[] waterfallSteps = new WaterfallStep[]
         {
            FirstStepAsync,
            SecondStepAsync,
            ThirdStepAsync,
            FourthStepAsync
         };
        AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
        AddDialog(new DialogA(DialogAId));
    }

    private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        return await stepContext.PromptAsync(
            ChoicePrompt,
            new PromptOptions
            {
                Prompt = MessageFactory.Text($"Here are your choices:"),
                Choices = ChoiceFactory.ToChoices(new List<string> { "Open Dialog A", "Open Dialog B" })
                RetryPrompt = MessageFactory.Text($"Please choose one of the options."),
            });
    }

    private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        var response = (stepContext.Result as FoundChoice)?.Value.ToLower();

        if (response == "open dialog a")
        {
            return await stepContext.BeginDialogAsync(DialogAId, cancellationToken: cancellationToken);
        }

        if (response == "open dialog b")
        {
            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Dialog B is not ready need to reprompt previous step."));
            await stepContext.RepromptDialogAsync();
        }

        return await stepContext.NextAsync();
    }

   private static async Task<DialogTurnResult> ThirdStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
       // do something else
        return await stepContext.NextAsync();
    }

    private static async Task<DialogTurnResult> FourthStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        // what is the best way to end this?
        // return await stepContext.ReplaceDialogAsync(InitialId);
        return await stepContext.EndDialogAsync();
    }

回答1:


When you have met the criteria that requires a prompt dialog being sent to the client, you can simply use the following code:

// declare a prompt name at the top of your class
private const string promptName = "nameofprompt";

// add it to your list of dialogs
this.AddDialog(new TextPrompt(promptName, [Validator goes here if you have one])); 

// where you need to prompt use the below.
var opts = new PromptOptions
{
    // fill your activity with whatever data is needed for your client, 
    // we use custom channel data, but it's not necessary.
    Prompt = new Activity
    {
        Type = ActivityTypes.Message,
        Text = "I am prompting you for something here?",
        ChannelData = channelData,
    },
};

return await stepContext.PromptAsync(promptName, opts);

Let me know if you need any more info on this. It looks like you were really just missing the bottom line.



来源:https://stackoverflow.com/questions/54862601/how-to-reprompt-in-botframework-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!