问题描述
您好,我是机器人开发的新手,我试图弄清楚如何在Formflow之后将对话转发到QnaDialog.我的表单流仅在识别出用户名之后询问用户他/她的名字,之后只想打个招呼(用户名),因为我已经确定了用户,所以我希望以后的任何消息都已经转发到QnaDialog.我尝试一次添加一个检查器来标记问候语已经完成,但是由于只允许您进行一次对话.SendAsync现在我迷失了如何正确解决此问题的想法.
Hi Guys im new to bot development, and im trying to figure out how can i forward the conversation to a QnaDialog after formflow. My formflow simply asks the user his/her name after he/she was identified, it would simply say hi (username) afterwards what I want is that any message afterwards would be forwarded to a QnaDialog already since the user was already identified. I tried adding a checker once to flag that a greeting was done already, however since you are only allowed one Conversation.SendAsync I am now lost for ideas on how to correct this properly.
FORMFLOW
public class ProfileForm
{
// these are the fields that will hold the data
// we will gather with the form
[Prompt("What is your name? {||}")]
public string Name;
// This method 'builds' the form
// This method will be called by code we will place
// in the MakeRootDialog method of the MessagesControlller.cs file
public static IForm<ProfileForm> BuildForm()
{
return new FormBuilder<ProfileForm>()
.Message("Welcome to the profile bot!")
.OnCompletion(async (context, profileForm) =>
{
// Set BotUserData
context.PrivateConversationData.SetValue<bool>("ProfileComplete", true);
context.PrivateConversationData.SetValue<string>("Name", profileForm.Name);
// Tell the user that the form is complete
await context.PostAsync("Your profile is complete.");
})
.Build();
}
}
MessageController
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
//ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
//await Conversation.SendAsync(activity, () => new QnADialog());
#region Formflow
// Get any saved values
StateClient sc = activity.GetStateClient();
BotData userData = sc.BotState.GetPrivateConversationData(
activity.ChannelId, activity.Conversation.Id, activity.From.Id);
var boolProfileComplete = userData.GetProperty<bool>("ProfileComplete");
if (!boolProfileComplete)
{
// Call our FormFlow by calling MakeRootDialog
await Conversation.SendAsync(activity, MakeRootDialog);
}
else
{
//Check if Personalized Greeting is done
if (userData.GetProperty<bool>("Greet"))
{
//this doesnt work since their should be only one Conversation.SendAsync.
//ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
//await Conversation.SendAsync(activity, () => new QnADialog());
}
else
{
// Get the saved profile values
var Name = userData.GetProperty<string>("Name");
userData.SetProperty<bool>("Greet", true);
sc.BotState.SetPrivateConversationData(activity.ChannelId, activity.Conversation.Id,activity.From.Id, userData);
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity replyMessage = activity.CreateReply(string.Format("Hi {0}!", Name));
await connector.Conversations.ReplyToActivityAsync(replyMessage);
}
}
#endregion
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
推荐答案
以下是使用您的示例的示例.我做了一些小改动,但没有发现您无法找到的所有内容.一点解释:
Here is an example using your example. I made a few small changes but nothing you wouldn't be able to spot. A bit of explanation:
控制器:我清理了它.它实际上应该只调用根对话框.这要干净得多.
Controller: I cleaned it up. It should really only be calling the root dialog. This is much cleaner.
RootDialog :将首先调用该表单.如果表单成功,它将继续进入"QnA"对话框.
RootDialog: Will first call the form. If the form was successful it will continue to the QnA dialog.
ProfileForm :仅删除FormCompleted布尔值.没必要.
ProfileForm: only deleted the FormCompleted boolean. Was not necessary.
QnADialog :将使用填写的名称启动对话框并提出问题.我保留了默认代码以获取一些反馈.
QnADialog: Will start the dialog and ask a question, using the name filled in. I kept the default code to get some feedback.
希望这会有所帮助
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
RootDialog
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task ResumeAfterForm(IDialogContext context, IAwaitable<ProfileForm> result)
{
if (context.PrivateConversationData.TryGetValue("Name", out string name))
{
context.Call(new QnADialog(), MessageReceivedAsync);
}
else
{
await context.PostAsync("Something went wrong.");
context.Wait(MessageReceivedAsync);
}
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var form = new FormDialog<ProfileForm>(new ProfileForm(), ProfileForm.BuildForm, FormOptions.PromptInStart);
context.Call(form, ResumeAfterForm);
}
}
ProfileForm
[Serializable]
public class ProfileForm
{
// these are the fields that will hold the data
// we will gather with the form
[Prompt("What is your name? {||}")]
public string Name;
// This method 'builds' the form
// This method will be called by code we will place
// in the MakeRootDialog method of the MessagesControlller.cs file
public static IForm<ProfileForm> BuildForm()
{
return new FormBuilder<ProfileForm>()
.Message("Welcome to the profile bot!")
.OnCompletion(async (context, profileForm) =>
{
// Set BotUserData
//context.PrivateConversationData.SetValue<bool>("ProfileComplete", true);
context.PrivateConversationData.SetValue<string>("Name", profileForm.Name);
// Tell the user that the form is complete
await context.PostAsync("Your profile is complete.");
})
.Build();
}
}
QnADialog
[Serializable]
public class QnADialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.PrivateConversationData.TryGetValue("Name", out string name);
await context.PostAsync($"Hello {name}. The QnA Dialog was started. Ask a question.");
context.Wait(MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
await context.PostAsync($"You sent {activity.Text} which was {length} characters");
context.Wait(MessageReceivedAsync);
}
}
这篇关于如何将对话从Formflow转发到QnaDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!