예제 #1
0
        public override object Load(JToken obj, JsonSerializer serializer, Type type)
        {
            ComponentDialog dialog = base.Load(obj, serializer, type) as ComponentDialog;

            // Dialogs are not a public property of ComponentDialog so we read the dialog
            // collection from the json and call AddDialog() on each dialog.
            if (dialog != null)
            {
                var dialogs = obj["dialogs"];

                // If there are dialogs, load them.
                if (dialogs != null)
                {
                    if (obj["dialogs"].Type != JTokenType.Array)
                    {
                        throw new JsonSerializationException("Expected array property \"dialogs\" in ComponentDialog");
                    }

                    foreach (var dialogJObj in dialogs)
                    {
                        var innerDialog = dialogJObj.ToObject <Dialog>(serializer);
                        dialog.AddDialog(innerDialog);
                    }
                }
            }

            return(dialog);
        }
        public async Task WaterfallStepParentIsWaterfallParent()
        {
            var convoState = new ConversationState(new MemoryStorage());

            var adapter = new TestAdapter()
                          .Use(new AutoSaveStateMiddleware(convoState));

            var          dialogState         = convoState.CreateProperty <DialogState>("dialogState");
            var          dialogs             = new DialogSet(dialogState);
            const string WATERFALL_PARENT_ID = "waterfall-parent-test-dialog";
            var          waterfallParent     = new ComponentDialog(WATERFALL_PARENT_ID);

            var steps = new WaterfallStep[]
            {
                async(step, cancellationToken) =>
                {
                    Assert.AreEqual(step.Parent.ActiveDialog.Id, waterfallParent.Id);
                    await step.Context.SendActivityAsync("verified");

                    return(Dialog.EndOfTurn);
                }
            };

            waterfallParent.AddDialog(new WaterfallDialog(
                                          "test",
                                          steps));
            waterfallParent.InitialDialogId = "test";
            dialogs.Add(waterfallParent);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
                await dc.ContinueDialogAsync(cancellationToken);
                if (!turnContext.Responded)
                {
                    await dc.BeginDialogAsync(WATERFALL_PARENT_ID, null, cancellationToken);
                }
            })
            .Send("hello")
            .AssertReply("verified")
            .StartTestAsync();
        }
예제 #3
0
        public async Task DialogManager_UserState_NestedDialogs_PersistedAcrossConversations()
        {
            var firstConversationId  = Guid.NewGuid().ToString();
            var secondConversationId = Guid.NewGuid().ToString();
            var storage = new MemoryStorage();

            var outerAdaptiveDialog = CreateTestDialog("user.name");

            var componentDialog = new ComponentDialog();

            componentDialog.AddDialog(outerAdaptiveDialog);

            await CreateFlow(componentDialog, storage, firstConversationId)
            .Send("hi")
            .AssertReply("Hello, what is your name?")
            .Send("Carlos")
            .AssertReply("Hello Carlos, nice to meet you!")
            .StartTestAsync();

            await CreateFlow(componentDialog, storage, secondConversationId)
            .Send("hi")
            .AssertReply("Hello Carlos, nice to meet you!")
            .StartTestAsync();
        }
        public async Task CallDialogDefinedInParentComponent()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(new AutoSaveStateMiddleware(convoState));

            var options = new Dictionary <string, string> {
                { "value", "test" }
            };

            var childComponent = new ComponentDialog("childComponent");
            var childSteps     = new WaterfallStep[]
            {
                async(step, ct) =>
                {
                    await step.Context.SendActivityAsync("Child started.");

                    return(await step.BeginDialogAsync("parentDialog", options));
                },
                async(step, ct) =>
                {
                    Assert.AreEqual("test", (string)step.Result);
                    await step.Context.SendActivityAsync("Child finished.");

                    return(await step.EndDialogAsync());
                },
            };

            childComponent.AddDialog(new WaterfallDialog(
                                         "childDialog",
                                         childSteps));

            var parentComponent = new ComponentDialog("parentComponent");

            parentComponent.AddDialog(childComponent);
            var parentSteps = new WaterfallStep[]
            {
                async(step, dc) =>
                {
                    var stepOptions = step.Options as IDictionary <string, string>;
                    Assert.IsNotNull(stepOptions);
                    Assert.IsTrue(stepOptions.ContainsKey("value"));
                    await step.Context.SendActivityAsync($"Parent called with: {stepOptions["value"]}");

                    return(await step.EndDialogAsync(stepOptions["value"]));
                },
            };

            parentComponent.AddDialog(new WaterfallDialog(
                                          "parentDialog",
                                          parentSteps));

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dialogs = new DialogSet(dialogState);
                dialogs.Add(parentComponent);

                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    await dc.BeginDialogAsync("parentComponent", null, cancellationToken);
                }
                else if (results.Status == DialogTurnStatus.Complete)
                {
                    var value = (int)results.Result;
                    await turnContext.SendActivityAsync(MessageFactory.Text("Done"), cancellationToken);
                }
            })
            .Send("Hi")
            .AssertReply("Child started.")
            .AssertReply("Parent called with: test")
            .AssertReply("Child finished.")
            .StartTestAsync();
        }
        public async Task CallDialogInParentComponent()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter(TestAdapter.CreateConversation(TestContext.TestName))
                          .Use(new AutoSaveStateMiddleware(convoState))
                          .Use(new TranscriptLoggerMiddleware(new TraceTranscriptLogger(traceActivity: false)));

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var state   = await dialogState.GetAsync(turnContext, () => new DialogState());
                var dialogs = new DialogSet(dialogState);

                var childComponent = new ComponentDialog("childComponent");
                var childStep      = new WaterfallStep[]
                {
                    async(step, token) =>
                    {
                        await step.Context.SendActivityAsync("Child started.");
                        return(await step.BeginDialogAsync("parentDialog", "test"));
                    },
                    async(step, token) =>
                    {
                        await step.Context.SendActivityAsync($"Child finished. Value: {step.Result}");
                        return(await step.EndDialogAsync());
                    }
                };
                childComponent.AddDialog(new WaterfallDialog("childDialog", childStep));

                var parentComponent = new ComponentDialog("parentComponent");
                parentComponent.AddDialog(childComponent);
                var parentStep = new WaterfallStep[]
                {
                    async(step, token) =>
                    {
                        await step.Context.SendActivityAsync("Parent called.");
                        return(await step.EndDialogAsync(step.Options));
                    }
                };
                parentComponent.AddDialog(new WaterfallDialog("parentDialog", parentStep));

                dialogs.Add(parentComponent);

                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    await dc.BeginDialogAsync("parentComponent", null, cancellationToken);
                }
                else if (results.Status == DialogTurnStatus.Complete)
                {
                    var value = (int)results.Result;
                    await turnContext.SendActivityAsync(MessageFactory.Text($"Bot received the number '{value}'."), cancellationToken);
                }
            })
            .Send("Hi")
            .AssertReply("Child started.")
            .AssertReply("Parent called.")
            .AssertReply("Child finished. Value: test")
            .StartTestAsync();
        }