如何防止 PromptDialog.Number 的多个条目

How to prevent multiple entries for PromptDialog.Number

我在机器人框架对话框 class 中使用 PromptDialog.Number 方法,如下所示:

PromptDialog.Number(context, ResumeAfterClarification, prompt, min:1, max:3, retry:retryText);

这在大多数情况下工作正常。如果用户输入的数字不在 1 到 3 之间,则会显示 retryText 并且用户必须重试。但是,如果用户输入以逗号分隔的数字,则可接受(例如 1、2、3)。第一个数字传递给 resume 方法,其他的都被忽略。

我该如何防止这种情况发生?拒绝任何无效条目(即任何不是最小值和最大值之间的单个数字的条目)是有道理的。

我是不是漏掉了什么?此方法可以接受多个条目吗?它们如何在单个 int64 参数中传递给 resume 方法?我真的很想禁用它。

正如 Dylan Nicholson 在评论中提到的,您可以继承 PromptInt64 并使用自定义 TryParse 方法:

[Serializable]
public class CustomPromptInt64 : PromptInt64
{
    public CustomPromptInt64(string prompt, string retry, int attempts, long? min = null, long? max = null) 
            : base(prompt, retry, attempts, null, min, max)
    {
    }

    protected override bool TryParse(IMessageActivity message, out long result)
    {
        if(Int64.TryParse(message.Text, out result))
        {
            return (result >= this.Min && result <= this.Max);
        }
        return false;
    }
}

并在对话框中使用 CustomPromptInt64:

    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);
        return Task.CompletedTask;
    }

   private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {

        var child = new CustomPromptInt64("Number between 1 and 3", "Please try again", 3, min: 1, max: 3);
        context.Call<long>(child, ResumeAfterClarification);
    }

    private async Task ResumeAfterClarification(IDialogContext context, IAwaitable<long> result)
    {
        var number = await result;
        context.Done(true);
    }