【发布时间】:2017-01-31 12:05:00
【问题描述】:
我有一个机器人,它将在数据库中寻找一个人。如果那个人不知道名字,我想让 Bot 再问一次:“名字不详,请重新输入名字”
以下是我现在完成的步骤:
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
await Conversation.SendAsync(activity, () => new RootDialog());
}
.... (more code here)
在 RootDialog 中:
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
if (message.Text.ToLower().Contains("help")
|| message.Text.ToLower().Contains("support")
|| message.Text.ToLower().Contains("problem"))
{
await context.Forward(new SupportDialog(), this.ResumeAfterSupportDialog, message, CancellationToken.None);
}
else
{
context.Call(new SearchDialog(), this.ResumeAfterOptionDialog);
}
}
在 SearchDialog 中:
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync($"Hi {context.Activity.From.Name}, Looking for someone?.");
var SearchFormDialog = FormDialog.FromForm(this.BuildSearchForm, FormOptions.PromptInStart);
context.Call(SearchFormDialog, this.ResumeAfterSearchFormDialog);
}
private IForm<SearchQuery> BuildSearchForm()
{
OnCompletionAsyncDelegate<SearchQuery> processSearch = async (context, persoon) =>
{
await context.PostAsync($"There we go...");
};
return new FormBuilder<SearchQuery>()
.Field(nameof(SearchQuery.Name))
.Message($"Just a second...")
.AddRemainingFields()
.OnCompletion(processSearch)
.Build();
}
private async Task ResumeAfterSearchFormDialog(IDialogContext context, IAwaitable<SearchQuery> result)
{
try
{
var searchQuery = await result;
var found = await new BotDatabaseEntities().GetAllWithText(searchQuery.Name);
var resultMessage = context.MakeMessage();
var listOfPersons = foundPersons as IList<Person> ?? foundPersons.ToList();
if (!listOfPersons.Any())
{
await context.PostASync($"No one found");
}
else if (listOfPersons.Count > 1)
{
resultMessage.AttachmentLayout = AttachmentLayoutTypes.List;
resultMessage.Attachments = new List<Attachment>();
this.ShowNames(context, listOfPersons.Select(foundPerson => foundPerson.FullName.ToString()).ToArray());
}
else
{
await OnePersonFound(context, listOfPersons.First(), resultMessage);
}
}
catch (FormCanceledException ex)
{
string reply;
reply = ex.InnerException == null ? "You have canceled the operation";
await context.PostAsync(reply);
}
}
这是 SearchQuery:
[Serializable]
public class SearchQuery
{
[Prompt("Please give the {&} of the person your looking for.")]
public string Name { get; set; }
}
现在。当我给出一个不存在的名称时,我不想重新开始对话,而只是希望 Bot 在此之后再次提出问题。
if (!listOfPersons.Any())
{
await context.PostASync($"No one found");
}
真的不知道如何解决这个问题。
【问题讨论】:
-
那么,您不想再次调用 SearchFormDialog 了吗?
-
嗯,实际上没有。因为然后对话将再次开始: - 嗨,Brian,请给出名字。 “John Doe” - 找不到名字。 - 嗨,布赖恩,请提供姓名。
标签: c# dialog botframework