中断以在Botframework上运行新对话框

时间:2019-07-08 12:35:53

标签: c# botframework

我正在尝试将代码从旧的Bot Framework V4(去年一月)迁移到新的Bot Framework v4设计。我喜欢在用户输入时调用一个对话框。 “开始使用”。我可以在以前的设计中轻松完成此操作,但现在我做不到。这也是现在的文字。如果我想使用LUIS意图怎么办?谢谢。

        protected override async Task<DialogTurnResult> OnBeginDialogAsync(
                       DialogContext innerDc, 
                       object options, 
                       CancellationToken cancellationToken = default(CancellationToken))
    {
        var result = await InterruptAsync(innerDc, cancellationToken);
        if (result != null)
        {
            return result;
        }

        return await base.OnBeginDialogAsync(innerDc, options, cancellationToken);
    }

    protected override async Task<DialogTurnResult> OnContinueDialogAsync(
                             DialogContext innerDc, 
                             CancellationToken cancellationToken)
    {
        var result = await InterruptAsync(innerDc, cancellationToken);
        if (result != null)
        {
            return result;
        }

        return await base.OnContinueDialogAsync(innerDc, cancellationToken);
    }

    private async Task<DialogTurnResult> InterruptAsync(
                   DialogContext innerDc, 
                   CancellationToken cancellationToken)
    {
        if (innerDc.Context.Activity.Type == ActivityTypes.Message)
        {
            var text = innerDc.Context.Activity.Text.ToLowerInvariant();

            switch (text)
            {
                case "help":
                case "?":
                    await innerDc.Context.SendActivityAsync($"Show Help...",
                          cancellationToken: cancellationToken);
                    return new DialogTurnResult(DialogTurnStatus.Waiting);

                case "get started":
                    return await innerDc.BeginDialogAsync(nameof(DialogA));

                case "cancel":
                case "quit":
                    await innerDc.Context.SendActivityAsync($"Cancelling",
                          cancellationToken: cancellationToken);
                    return await innerDc.CancelAllDialogsAsync();
            }
        }

        return null;
    }

DialogA

    namespace practiceNewBot.Dialogs
{
    public class DialogA : CancelAndHelpDialog
    {
        public DialogA() : base(nameof(DialogA))
        {
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                FirstStepAsync,
            }));

            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);
        }

        private async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"start of dialog a"), cancellationToken);

            return await stepContext.PromptAsync(
            nameof(ChoicePrompt),
            new PromptOptions
            {
                Prompt = MessageFactory.Text($"choose a dialog"),
                Choices = new List<Choice>
                     {
                            new Choice
                            {
                                Value = "Dialog A Child",
                                Synonyms = new List<string>
                                {
                                    "dialog a child",
                                },
                            },
                            new Choice
                            {
                                Value = "Dialog B Child",
                                Synonyms = new List<string>
                                {
                                    "dialog b chilc",
                                },
                            },
                            new Choice
                            {
                                Value = "Cancel",
                                Synonyms = new List<string>
                                {
                                    "cancel",
                                },
                            },
                     },
                RetryPrompt = MessageFactory.Text("Please choose one of the options."),
            },
            cancellationToken: cancellationToken);
        }

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

            switch (response)
            {
                case "dialog a child":
                    return await stepContext.BeginDialogAsync(nameof(DialogA), cancellationToken: cancellationToken);

                case "dialog b child":
                    return await stepContext.BeginDialogAsync(nameof(DialogB), cancellationToken);

                case "cancel":
                    await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Ok, Cancelled."));
                    return await stepContext.CancelAllDialogsAsync(cancellationToken: cancellationToken);

                default:
                    break;
            }

            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"end of dialog a"), cancellationToken);
            return await stepContext.EndDialogAsync();
        }
    }
}

0 个答案:

没有答案