如何在机器人框架中验证 phone 数字?

How to validate phone number in bot framework?

我正在使用以下代码获取 phone 号码的用户输入。我想验证用户输入,如果不正确,需要让用户重新输入。

[function (session, results, next) {
    builder.Prompts.text(session, 'I will also need to know your contact number.');
}
,function (session, results, next) {
    session.userData.contactNo = results.response;
    next();
}]

我试过 this example,但它发出警告说它已被弃用。感谢有关正确执行此操作的任何帮助(不使用已弃用的方法)。我的 phone 数字正则表达式是 ^[689]\d{3}\s?\d{4}$

documentation中有一个有趣的示例:

bot.dialog('/phonePrompt', [
    function (session, args) {
        if (args && args.reprompt) {
            builder.Prompts.text(session, "Enter the number using a format of either: '(555) 123-4567' or '555-123-4567' or '5551234567'")
        } else {
            builder.Prompts.text(session, "What's your phone number?");
        }
    },
    function (session, results) {
        var matched = results.response.match(/\d+/g);
        var number = matched ? matched.join('') : '';
        if (number.length == 10 || number.length == 11) {
            session.endDialogWithResult({ response: number });
        } else {
            session.replaceDialog('/phonePrompt', { reprompt: true });
        }
    }
]);

在这里您可以看到,在处理结果的函数中,他们正在执行一些检查,然后如果无效,他们正在执行带有 reprompt 参数的 replaceDialog

您可以在此处对您的业务逻辑进行相同的尝试(即:执行正则表达式检查而不是示例中的数字长度检查)