在嵌套对话框中处理自适应卡提交操作值

时间:2019-07-11 14:48:05

标签: c# botframework chatbot adaptive-cards waterfall

我使用C#使用SDK .net Core 2.2 Virtual Assistant Template创建了聊天机器人,该聊天机器人具有1个主对话框和多个对话框(组件对话框)。每个组件对话框调用另一个组件对话框。 假设我有MainDialog,第2个组件对话框的名称为ComponentDialog1,ComponentDialog2。

我正在使用DialogsTriggerState来了解整个机器人要触发哪个组件对话框。

主对话框代码::我正在主对话框的 RouteAsync 方法中调用ComponentDialog1。

protected override async Task RouteAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
{
    var triggerState = await _triggerStateAccessor.GetAsync(dc.Context, () => new DialogsTriggerState());
    await dc.BeginDialogAsync(nameof(ComponentDialog1), triggerState);
    var turnResult = EndOfTurn;
    if (turnResult != EndOfTurn)
    {
        await CompleteAsync(dc);
    }
}

ComponentDialog1代码: 我有3个瀑布步骤,其中第2步将根据机器人状态调用特定的“ ComponentDialog”。假设我正在触发“ ComponentDialog2”。

private async Task<DialogTurnResult> TriggerToSpecificComponentDialogAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    var triggerState = await _DialogsTriggerStateAccessor.GetAsync(stepContext.Context, () => null);

    if (triggerState.TriggerDialogId.ToString().ToLower() == "componentdialog2")
    {
        return await stepContext.BeginDialogAsync(nameof(ComponentDialog2), triggerState);
    }
    else if (triggerState.TriggerDialogId.ToString().ToLower() == "componentdialog3")
    {
        return await stepContext.BeginDialogAsync(nameof(ComponentDialog3), triggerState);
    }
    else
    {
        return await stepContext.NextAsync();
    }
}

ComponentDialog2代码: 我有2个瀑布步骤,显示自适应卡并从卡和结束对话框(ComponentDialog2)取值

private async Task<DialogTurnResult> DisplayCardAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Display the Adaptive Card
    var cardPath = Path.Combine(".", "AdaptiveCard.json");
    var cardJson = File.ReadAllText(cardPath);
    var cardAttachment = new Attachment()
    {
        ContentType = "application/vnd.microsoft.card.adaptive",
        Content = JsonConvert.DeserializeObject(cardJson),
    };
    var message = MessageFactory.Text("");
    message.Attachments = new List<Attachment>() { cardAttachment };
    await stepContext.Context.SendActivityAsync(message, cancellationToken);

    // Create the text prompt
    var opts = new PromptOptions
    {
        Prompt = new Activity
        {
            Type = ActivityTypes.Message,
            Text = "waiting for user input...", // You can comment this out if you don't want to display any text. Still works.
        }
    };

    // Display a Text Prompt and wait for input
    return await stepContext.PromptAsync(nameof(TextPrompt), opts);
}

private async Task<DialogTurnResult> HandleResponseAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    await stepContext.Context.SendActivityAsync($"INPUT: {stepContext.Result}");
   //I am doing some logic and may continue to next steps also from here, but as I am stuck here i am ending dialog.
    return await stepContext.EndDialogAsync();
}

问题:在第一步的“ ComponentDialog2”中单击自适应卡提交后,代码(控件)未指向第二步“ HandleResponseAsync”,因为我已经提供了提示并等待输入。

实际输出:我在机器人程序中既没有得到任何输出,也没有错误。

预期输出:

1)从ComponentDialog2显示给机器人:输入:无论我提交了什么

2)当我在ComponentDialog2中结束对话框时,控件(代码)应返回ComponentDialog1,并应转到ComponentDialog1的第3个瀑布步骤。

样本自适应卡

  {
   "type": "AdaptiveCard",
   "body": [
    {
        "type": "TextBlock",
        "size": "Medium",
        "weight": "Bolder",
        "text": "Let us know your feedback"
    }
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0",
"actions": [
    {
        "type": "Action.Submit",
        "title": "Good",
        "data": "good"
    },

     {
        "type": "Action.Submit",
        "title": "Average",
        "data": "avaerage"
    }
    ,{
        "type": "Action.Submit",
        "title": "Bad",
        "data": "bad"
    }
]
}

请帮助我如何实现

1 个答案:

答案 0 :(得分:0)

经过大量测试后,我相当确定您在某处缺少了this call(应该以某种方式在OnMessageAsync()中调用它{em> :

var results = await dialogContext.ContinueDialogAsync(cancellationToken);

Core Bot Sample执行此操作的方式是将其添加到DialogExtensions中,并在OnMessageAsync()中调用


更新:我还注意到,如果await ConversationState.SaveChangesAsync(turnContext, true, cancellationToken);OnTurnAsync()中但在OnDialogAsync()中(或者不在两个位置),可能会发生这种情况;您还希望确保通常将其设置为false而不是true