【问题标题】:Continue Dialog in Bot Framework C#Bot Framework C# 中的继续对话框
【发布时间】:2022-11-14 23:22:09
【问题描述】:

我想检查用户输入是否是 (hello) 以启动对话并继续所有对话流程,但是每当我输入问候消息并且流程开始时,机器人的第一个输入将不会进入对话返回到 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 语句中的代码中看到的那样,然后它会说对话框没有运行。如果有一个活跃的,我怎样才能让机器人继续对话。

【问题讨论】:

    标签: c# azure dialog botframework chatbot


    【解决方案1】:

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

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2016-10-30
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多