azure Bot Framework C#中的“继续”对话框

hivapdat  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(127)

我想检查用户输入是否启动对话框并继续所有对话框流程,但每当我输入问候消息和流程启动时,机器人的第一个输入将不会进入对话框,它将返回到OnTurn消息,对话框停止,因此我想找到一种方法来检查对话框是否处于活动状态并继续,如果不是,则跳过它。

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
    {
        var text = turnContext.Activity.Text;
        if (text == "hello")
        {
            await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
        }
        else 
        {
            await turnContext.SendActivityAsync("dialog is not running");
        }
        await base.OnTurnAsync(turnContext, cancellationToken);

        // Save any state changes that might have occurred during the turn.
        await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
    }

你可以在上面的代码中看到,在用户输入hello后的else语句中,它会说对话框没有运行。如果有一个活动的对话框,我如何让bot继续对话框?

1tuwyuhd

1tuwyuhd1#

需要保存当前更改。当前代码块中缺少该更改。请尝试替换它。

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
    await base.OnTurnAsync(turnContext, cancellationToken);

    // Save any state changes that might have occurred during the turn.
    await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
    await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
var text = turnContext.Activity.Text;
        if (text == "hello")
        {
            await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
        }
        else 
        {
            await turnContext.SendActivityAsync("waiting for dailog");
        }
        await base.OnTurnAsync(turnContext, cancellationToken);

}

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
    Logger.LogInformation("Running dialog with Message Activity.");

    // Run the Dialog with the new message Activity.
    await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
}

相关问题