我使用以下代码获取电话号码的用户输入。我想验证用户输入,如果不正确,需要让用户再次输入。
[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,但它会发出警告,说它已被弃用。感谢有关正确方法的任何帮助(不使用弃用的方法)。我的电话号码正则表达式为^[689]\d{3}\s?\d{4}$
答案 0 :(得分:2)
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 });
}
}
]);
在这里你可以看到,在处理结果的函数中,他们正在执行一些检查,如果无效,他们正在使用replaceDialog
参数进行reprompt
。
您可以在此处尝试使用您的业务逻辑(即:执行正则表达式检查而不是样本中的数字长度检查)