Bot框架,使用C#,在子对话框中保持对话

时间:2017-10-03 00:54:52

标签: c# .net botframework

我有一个父对话框,向用户发出提示并提供一些选项供您选择。根据所选的选项,我调用子对话框。现在,在子对话框中,我想首先给出说明并要求用户根据说明提供数据。因此,在发出指示后,我想等待用户的输入。使用下面的代码,在给出关于指令的消息之后执行返回到父对话框,并在父对象上执行子对话后完成方法(在我的代码下面的函数 - " AfterAllDoneAsync")。有人可以帮忙吗?

以下是我的代码 -

消息控制器

  public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Message)
        {
            await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

根对话

public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {

        var activity = await result as Activity;
        string txt = activity.Text.ToLower() + " ";

        if (txt.StartsWith("Image"))
        {
            // Prompt user with options and based on chosen option, call child dialog
            var opt = new PromptOptions<String>("Choose from following -", "Didn't get that, please try again", "You are tough! I give up!", new string[] { "one", "two", "three" });
        PromptDialog.Choice(context, AfterPromptAsync, opt);

        }
    }

 public async Task AfterPromptAsync(IDialogContext context, IAwaitable<string> argument)
    {
        var confirm = await argument;
        if (confirm == "one")
        { 
            // call child dialog
            await context.Forward(new ChildDialog(), AfterAllDoneAsync, context.Activity,CancellationToken.None);
        }
        context.Wait(MessageReceivedAsync);
    }
    public async Task AfterAllDoneAsync(IDialogContext context, IAwaitable<object> argument)
    {
        await context.PostAsync("It was really great talking to you!");
        context.Wait(MessageReceivedAsync);
    }

儿童对话

 public Task StartAsync(IDialogContext context)
    {
        context.Wait(this.MessageReceivedAsync);
        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var act = await result as Activity;
        if(act.Attachments.Count == 0)
        {
            await context.PostAsync("Follow these instrutions bla bla bla");
        }
        // Then i want to wait for the user to provide the needed data as per instructions but the control goes back to root dialog
        context.Wait(this.MessageReceivedAsync);
    }

这是输出

U-Image

Bot - 从以下选择 - 一 二 3

U - 一个

Bot - 遵循这些指示bla bla bla 与你交谈非常棒!

1 个答案:

答案 0 :(得分:2)

您将转发到子对话框,并在第一个对话框中等待。您需要更新AfterPromptAsync的代码,以便仅在您不转发时等待(基本上添加else :))。

    public async Task AfterPromptAsync(IDialogContext context, IAwaitable<string> argument)
    {
        var confirm = await argument;
        if (confirm == "one")
        { 
            // call child dialog
            await context.Forward(new ChildDialog(), AfterAllDoneAsync, context.Activity,CancellationToken.None);
        }
        else 
        {
          context.Wait(MessageReceivedAsync);
        }
    }