如何删除Prompt.Choice中的tooManyAttempts消息?如何在Prompt.Choice中接受不在选项列表中的文本? C# [英] How to remove tooManyAttempts message in Prompt.Choice? How to accept text in Prompt.Choice that is not in list of options? C#

查看:189
本文介绍了如何删除Prompt.Choice中的tooManyAttempts消息?如何在Prompt.Choice中接受不在选项列表中的文本? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using bot-Framework SDK3 C#.

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

I want to allow user input anything which is not in "PromptDialog.Choice"'s options. Any better ways to recommend?

这是我的代码.

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"给我.

But the problem is it always send the message "tooManyAttempts". If I set it to empty, I will send me "0".

推荐答案

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

I suppose you are using NodeJS. You can use the simple builder.Prompts.choice with maxRetries set to 0. Here is a sample snippet. It asks user to choose some option from a list, or they can enter something which is not in the list.

如果您使用的是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
        }
    }
]);

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

Hi, Saw that you are using C# SDK. I am not that proficient with that but I can give you some suggestion.

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

The list which you generate in the async task SelectCategory you can generate in some other place, which is also accessible to the second async task ResumeAfterSelectCategory, (like making it a class variable or getting from database).

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

Now that the list is accessible in the 2nd task, you can compare what user has typed against the list to determine if the message is from the list or not.

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

If message is something from the list, then take action accordingly, otherwise user has entered something which is not in the list, and then take action accordingly.

您的第二个问题是

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

And if user typed, it will show a message "you tried to many times"

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

What is meant by that? Does bot sends "you tried to many times" to the bot visitor. In which case it could be the behavior of library. You will be able to control that only if library provides some option. Else I don't know. Hope, that helps

我遇到了这样的问题

EDIT 2: I came across this SO question Can I add custom logic to a Bot Framework PromptDialog for handling invalid answers?

您可以使用该问题的答案.基本上扩展PromptDialog.PromptChoice<T>. >这里是一个例子.

You can use that questions answer. Basically extending 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
    }
}

这篇关于如何删除Prompt.Choice中的tooManyAttempts消息?如何在Prompt.Choice中接受不在选项列表中的文本? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆