如何删除Prompt.Choice中的tooManyAttempts消息?如何在Prompt.Choice中接受不在选项列表中的文本? C#

时间:2018-08-09 02:42:56

标签: botframework

我正在使用bot-Framework SDK3 C#。

我希望允许用户输入“ PromptDialog.Choice”选项中未包含的任何内容。有更好的推荐方法吗?

这是我的代码。

private async Task SelectCategory(IDialogContext context)
{
     List<string> options = new List<string>();
                options = category.Keys.ToList();
                options.Add("Category1");
                options.Add("Category2");
                options.Add("Category3");

     PromptOptions<string> promptOptions = new PromptOptions<string>(
             prompt: "which one do you prefer?",
             tooManyAttempts: "",
             options: options,
             attempts: 0);

         PromptDialog.Choice(context: context, resume: ResumeAfterSelectCategory, promptOptions: promptOptions);

        await Task.FromResult<object>(null);


}

  private async Task ResumeAfterSelectCategory(IDialogContext context, IAwaitable<string> result)
  {
        try
        {
             selected = await result;
        }
        catch (Exception)
        {
            // if the user's input is not in the select options, it will come here
        }
  }

但是问题是它总是发送消息“ tooManyAttempts”。如果将其设置为空,则将“ 0”发送给我。

2 个答案:

答案 0 :(得分:0)

我想您正在使用NodeJS。您可以将简单的builder.Prompts.choice设为maxRetries来使用。这是一个示例片段。它要求用户从列表中选择某个选项,或者他们可以输入不在列表中的内容。

如果您使用的是C#SDK,则可以在列表中找到一些类似的选项。

bot.dialog("optionalList", [
    function(session){
        builder.Prompts.choice(
            session,
            "Click any button or type something",
            ["option1", "option2", "option3"],
            {maxRetries: 0} // setting maxRetries to zero causes no implicit checking
        )
    }, 
    function(session, result){
        // something from the list has been clicked
        if(result.response && result.response.entity){
            console.log(result.response.entity); // use the clicked button
        } else {
            console.log(session.message.text) // if user entered something which is not in the list
        }
    }
]);

编辑1:

您好,看到您正在使用C#SDK。我不太熟练,但是我可以给你一些建议。

您可以在其他位置生成的异步任务SelectCategory中生成的列表,第二个异步任务ResumeAfterSelectCategory 也可以访问该列表(例如使其成为类变量)或从数据库获取)

现在可以在第二个任务中访问列表了,您可以将用户键入的内容与列表进行比较,以确定消息是否来自列表。

如果消息是列表中的内容,请采取相应的措施,否则用户输入了列表中未包含的内容,然后采取相应的措施。

您的第二个问题是

  

如果用户键入,它将显示一条消息“您尝试了很多次”

那是什么意思?机器人是否向机器人访问者发送“您尝试过多次”。在这种情况下,可能是库的行为。只有在库提供某些选项的情况下,您才可以控制它。其他我不知道。希望有帮助

编辑2: 我遇到了这样的问题Can I add custom logic to a Bot Framework PromptDialog for handling invalid answers?

您可以使用该问题的答案。基本上扩展了PromptDialog.PromptChoice<T>Here is an example

像这样覆盖TryParse方法

protected override bool TryParse(IMessageActivity message, out T result)
{
    bool fromList = base.TryParse(message, out result);
    if (fromList)
    {
        return true;
    } else {
        // do something here
        return true; // signal that parsing was correct
    }
}

答案 1 :(得分:0)

我使用了node.js并获得了用户输入的消息。使用此代码段。

(session, args) => {
       builder.Prompts.text(session, "Please Enter your name.");
    },

(session, args) => {
       session.dialogData.username = args.response;
       session.send(`Your user name is `${session.dialogData.username}`);
}