[英]How to call waterfall dialog from QnaBot (qna maker api) in C# [Bot Framework v4]?
我已经使用c#在Bot Framework v4中创建了一个qna maker bot,现在在qna知识库中找不到答案时,我必须调用一个瀑布对话框向用户提出一些问题。
我该怎么做?
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient();
var qnaMaker = new QnAMaker(new QnAMakerEndpoint
{
KnowledgeBaseId = _configuration["QnAKnowledgebaseId"],
EndpointKey = _configuration["QnAAuthKey"],
Host = GetHostname()
},
null,
httpClient);
_logger.LogInformation("Calling QnA Maker");
// The actual call to the QnA Maker service.
var response = await qnaMaker.GetAnswersAsync(turnContext);
if (response != null && response.Length > 0)
{
string message = GetMessage(response[0].Answer);
Attachment attachment = GetHeroCard(response[0].Answer);
await turnContext.SendActivityAsync(MessageFactory.Text(message), cancellationToken);
if (attachment != null)
await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);
}
else
{
//HERE I WANT TO CALL WATERFALL DIALOG
//await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
}
}
您可以使用多转提示,该提示使用瀑布对话框,一些提示和组件对话框来创建一个简单的交互方式,向用户询问一系列问题。机器人通过UserProfileDialog与用户进行交互。 当创建机器人的DialogBot类时,我们会将UserProfileDialog设置为其主要对话框。 然后,该漫游器使用“运行帮助器”方法来访问对话框。
要使用提示,请在对话框的某个步骤中调用它,并在接下来的步骤中使用stepContext.Result检索提示结果。 您应该始终从瀑布步骤返回非null的DialogTurnResult。
询问用户名称的示例如下:
public class UserProfileDialog : ComponentDialog
{
private readonly IStatePropertyAccessor<UserProfile> _userProfileAccessor;
public UserProfileDialog(UserState userState)
: base(nameof(UserProfileDialog))
{
_userProfileAccessor = userState.CreateProperty<UserProfile>("UserProfile");
// This array defines how the Waterfall will execute.
var waterfallSteps = new WaterfallStep[]
{
NameStepAsync,
NameConfirmStepAsync,
SummaryStepAsync,
};
// Add named dialogs to the DialogSet. These names are saved in the dialog state.
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
AddDialog(new TextPrompt(nameof(TextPrompt)));
AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));
// The initial child Dialog to run.
InitialDialogId = nameof(WaterfallDialog);
}
private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["transport"] = ((FoundChoice)stepContext.Result).Value;
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Please enter your name.") }, cancellationToken);
}
private async Task<DialogTurnResult> NameConfirmStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["name"] = (string)stepContext.Result;
// We can send messages to the user at any point in the WaterfallStep.
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Thanks {stepContext.Result}."), cancellationToken);
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to give your age?") }, cancellationToken);
}
private async Task<DialogTurnResult> SummaryStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
if ((bool)stepContext.Result)
{
// Get the current profile object from user state.
var userProfile = await _userProfileAccessor.GetAsync(stepContext.Context, () => new UserProfile(), cancellationToken);
userProfile.Name = (string)stepContext.Values["name"];
}
else
{
await stepContext.Context.SendActivityAsync(MessageFactory.Text("Thanks. Your profile will not be kept."), cancellationToken);
}
// WaterfallStep always finishes with the end of the Waterfall or with another dialog, here it is the end.
return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
}
}
本文档将帮助您更好地理解多提示对话框。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.