Bot框架 - PromptDialog.Choice有重复的回复

时间:2017-03-16 19:44:09

标签: c# botframework

在Bot Framework中,在使用C#时,我有一个Dialog并使用PromptDialog.Choice让用户选择他们感兴趣的问题。

但是当它运行时,我得到了重复的回复,如下图所示:

enter image description here

public async Task StartAsync(IDialogContext context)
{
    this.ShowOptions(context);
}

private void ShowOptions(IDialogContext context)
{
    PromptDialog.Choice(
        context,
        this.OnOptionSelected,
        new List() { ImageOption, ToolOption },
        "Please select one of the following category.",
        "Not a valid option",
        3);
}

private async Task OnOptionSelected(IDialogContext context, IAwaitable result)
{
    string optionSelected = await result;
    switch (optionSelected)
    {
        case ImageOption:
            context.Call(new ImgRelated(), this.ResumeAfterOptionDialog);
            break;

        case ToolOption:
            context.Call(new ToolPBmDailog(), this.ResumeAfterOptionDialog);
            break;
    }
}

有人有想法吗?

2 个答案:

答案 0 :(得分:2)

ShowOptions需要是异步方法,需要在context.Wait(this.ShowOptions)中使用StartAsync进行调用,而不是直接调用它。

public async Task StartAsync(IDialogContext context)
{
    context.Wait(this.ShowOptions);
}

public async virtual Task ShowOptions(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var message = await result;

    PromptDialog.Choice<string>(
        context,
        this.OnOptionSelected,
        new List() { ImageOption, ToolOption },
        "Please select one of the following category.",
        "Not a valid option",
        3);
}

答案 1 :(得分:0)

您必须制作OnOptionSelected方法,以便在执行之前等待用户的输入。另外,将ShowOptions方法设置为异步,否则可能会出现异常,因为现在,ShowOptions方法会向StartAsync方法返回void。 IAwaitable<IMessageActivity>等待用户的响应。请尝试以下操作,看看它是否适合您。

public async Task StartAsync(IDialogContext context)
    {
        await this.ShowOptions(context);
    }

    private async Task ShowOptions(IDialogContext context)
    {
        PromptDialog.Choice(context, this.OnOptionSelected, 
                     new List() { ImageOption, ToolOption }, 
                     "Please select one of the following category.", 
                     "Not a valid option", 3);
    }

    private async Task OnOptionSelected(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        string optionSelected = await result;
        switch (optionSelected)
        {
            case ImageOption:
                context.Call(new ImgRelated(), this.ResumeAfterOptionDialog);
                break;

            case ToolOption:
                context.Call(new ToolPBmDailog(), this.ResumeAfterOptionDialog);
                break;
        }
    }