【发布时间】:2019-04-10 06:16:08
【问题描述】:
我需要根据活动对话框的 ID 做一个条件,如下所示。
var dc = await _dialogs.CreateContextAsync(turnContext);
if (dc.ActiveDialog != null && dc.ActiveDialog.id == "SkipLuisDialog")
{
var interruptedQnaMaker = await IsTurnInterruptedDispatchToQnAMakerAsync(turnContext, topDispatch, QnaConfiguration, cancellationToken);
}
当我像这样检查OnTurnAsync() 上的活动对话框时:
if (dc.ActiveDialog != null)
{
await dc.Context.SendActivityAsync(dc.ActiveDialog.Id.ToString());
}
它总是说“mainDialog”,这是我的主对话框的 ID。即使即时在“FAQDialog”或“complaintDialog”上,活动对话 ID 始终是“mainDialog”。为什么当我在常见问题解答对话框中时,活动对话框 ID 没有更改为“FAQDialog”,或者当我在投诉对话框中时,活动对话框 ID 没有更改为“complaintDialog”?
我的主对话框和其他对话框在另一个类中。
这就是我在OnTurnAsync() 上调用mainDialog 的方式:
if (!dc.Context.Responded)
{
switch (dialogResult.Status)
{
case DialogTurnStatus.Empty:
switch (topIntent) // topIntent // text
{
case GetStartedIntent:
await dc.BeginDialogAsync(MainDialogId);
break;
case NoneIntent:
default:
await dc.Context.SendActivityAsync("I didn't understand what you just said to me.");
break;
}
break;
case DialogTurnStatus.Waiting:
// The active dialog is waiting for a response from the user, so do nothing.
break;
case DialogTurnStatus.Complete:
await dc.EndDialogAsync();
break;
default:
await dc.CancelAllDialogsAsync();
break;
}
}
主对话框:
namespace CoreBot.Dialogs
{ 公共类 MainDialog : ComponentDialog { 私有常量字符串 InitialId = "mainDialog"; 私有常量字符串 TextPromptId = "textPrompt";
private const string ComplaintDialogId = "complaintDialog";
private const string FAQDialogId = "FAQDialog";
public MainDialog(string dialogId) : base(dialogId)
{
WaterfallStep[] waterfallSteps = new WaterfallStep[]
{
FirstStepAsync,
SecondStepAsync,
ThirdStepAsync,
};
AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
AddDialog(new FAQDialog(FAQDialogId));
AddDialog(new FileComplaintDialog(ComplaintDialogId));
AddDialog(new TextPrompt(TextPromptId));
}
private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
return await stepContext.PromptAsync(
TextPromptId,
new PromptOptions
{
Prompt = new Activity
{
Type = ActivityTypes.Message,
Text = $"What can i do for you?",
SuggestedActions = new SuggestedActions()
{
Actions = new List<CardAction>()
{
new CardAction() { Title = "Frequently Asked Questions", Type = ActionTypes.ImBack, Value = "Frequently Asked Questions" },
new CardAction() { Title = "File a Complaint Ticket", Type = ActionTypes.ImBack, Value = "File a Complaint Ticket" },
},
},
},
});
}
private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var response = stepContext.Result.ToString().ToLower();
string[] FAQArray = { "frequently asked questions", "question", "ask question" };
string[] ComplaintsArray = { "file a complaint ticket", "complaint", "file a complaint" };
if (FAQArray.Contains(response))
{
return await stepContext.BeginDialogAsync(FAQDialogId, cancellationToken);
}
if (ComplaintsArray.Contains(response))
{
await stepContext.EndDialogAsync();
return await stepContext.BeginDialogAsync(ComplaintDialogId, cancellationToken: cancellationToken);
}
return await stepContext.NextAsync();
}
private static async Task<DialogTurnResult> ThirdStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
return await stepContext.ReplaceDialogAsync(InitialId);
}
}
}
主对话框调用 2 个对话框。这是其中之一。
namespace CoreBot.Dialogs
{
public class FAQDialog : ComponentDialog
{
private const string InitialId = "FAQDialog";
private const string ChoicePromptId = "choicePrompt";
private const string TextPromptId = "textPrompt";
private const string ConfirmPromptId = "confirmPrompt";
public FAQDialog(string dialogId) : base(dialogId)
{
WaterfallStep[] waterfallSteps = new WaterfallStep[]
{
FirstStepAsync,
SecondStepAsync,
ThirdStepAsync,
FourthStepAsync,
};
AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
AddDialog(new ChoicePrompt(ChoicePromptId));
AddDialog(new ConfirmPrompt(ConfirmPromptId));
AddDialog(new TextPrompt(TextPromptId));
}
private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var choices = new List<Choice>();
choices.Add(new Choice { Value = "This is Sample Question 1", Synonyms = new List<string> { "Question 1" } });
choices.Add(new Choice { Value = "This is Sample Question 2", Synonyms = new List<string> { "Question 2" } });
return await stepContext.PromptAsync(
ChoicePromptId,
new PromptOptions
{
Prompt = MessageFactory.Text($"Welcome to FAQ! Choose the number of the question or type your own question."),
Choices = choices,
});
}
private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var choiceResult = (stepContext.Result as FoundChoice).Value.ToLower();
switch (choiceResult)
{
case "this is sample question 1":
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Answer to question 1."));
break;
case "this is sample question 2":
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Answer to question 2."));
break;
default:
break;
}
return await stepContext.NextAsync();
}
private static async Task<DialogTurnResult> ThirdStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
return await stepContext.PromptAsync(
ConfirmPromptId,
new PromptOptions
{
Prompt = MessageFactory.Text($"Have another question?"),
});
}
private static async Task<DialogTurnResult> FourthStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var confirmResult = (bool)stepContext.Result;
if (confirmResult)
{
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Ask away!"));
return await stepContext.ReplaceDialogAsync(InitialId);
}
else
{
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Great!"));
return await stepContext.EndDialogAsync();
}
}
}
}
【问题讨论】:
标签: c# botframework