[英]Handle adaptive card submit action value in nested dialogs
我已經使用C#使用SDK .net Core 2.2 虛擬助手模板創建了聊天機器人,該模板具有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”,因為我已經提供了提示並等待輸入,所以它應該發生。
實際輸出:我既沒有得到任何輸出,也沒有在bot中出錯。
預期產量:
1)從ComponentDialog顯示給機器人2:輸入:無論我提交了什么
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"
}
]
}
請幫我怎么實現
經過大量測試后,我相當確定您在某個地方缺少此調用 (應該以某種方式在OnMessageAsync()
調用它:
var results = await dialogContext.ContinueDialogAsync(cancellationToken);
核心機器人樣本執行此操作的方法是將其添加到DialogExtensions
,該DialogExtensions
在OnMessageAsync()
被調用
更新:我還注意到,如果await ConversationState.SaveChangesAsync(turnContext, true, cancellationToken);
可能會發生這種情況await ConversationState.SaveChangesAsync(turnContext, true, cancellationToken);
在OnTurnAsync()
但是在OnDialogAsync()
(或者如果不在兩個位置中); 通常,您還需要確保將其設置為false
而不是true
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.