我有一个机器人,它将在数据库中寻找人。如果该人的名字未知,我想让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");
        }


真的不知道如何解决该问题。

最佳答案

嗯,我现在这样修复它:

if (!listOfPersons.Any())
            {
                await context.PostAsync($"Sorry, no one was found with this text");
                var SearchFormDialog = FormDialog.FromForm(this.BuildSearchForm, FormOptions.PromptInStart);
                context.Call(SearchFormDialog, this.ResumeAfterSearchFormDialog);
            }

关于c# - Microsoft Bot Framework:在不重新启动对话框的情况下再次询问,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41957149/

10-13 23:47