我在使用bot.customAction获取一个简单的QnA对象时遇到问题,预期的结果是发送“我一如既往的精彩,但感谢您的提问!”如果用户发送“很好,你呢?”,“嗯,你呢?”,“好,你呢?”,我尝试了以下两个选项
第一选项:
bot.customAction({
matches: /^Fine and you$|^Good and you$|^Well and you$/i,
onSelectAction: (session, args, next) => {
session.send("I'm wonderful as always, but thanks for asking!");
}
});
第二选项:
bot.customAction({
matches: "Fine and you?"|"Good and you?"|"Well and you?",
onSelectAction: (session, args, next) => {
session.send("I'm wonderful as always, but thanks for asking!");
}
});
在第一个选项中,匹配仅识别没有问号“?”的确切单词
[BotFramework Emulator] Word recognized without the question mark "?"
在第二个选项中没有任何反应,只需启动瀑布对话
[BotFramework Emulator] Second option is ignored by bot and start the waterfall conversation
谢谢你的时间!
答案 0 :(得分:1)
bot.customAction({
matches: /^Fine and you|^Good and you|^Well and you/i,
onSelectAction: (session, args, next) => {
session.send("I'm wonderful as always, but thanks for asking!");
}
});
在正则表达式中,^
代表beginning of the string
,$
代表end of the string
。因此,当您的正则表达式为/^Fine and you$/
时,您在开头或结尾处没有额外内容匹配Fine and you
。
要解决此问题,您需要使您的正则表达式更加灵活。
/^Fine and you\??$/
'很好,你'带有optional问号
/^Fine and you/
任何以'罚款开头的字符串,以及其后的任何其他内容(Fine and you foobar blah blah
也会匹配)
您可能会受益于introduction to regexes
bot.customAction({
matches: "Fine and you?"|"Good and you?"|"Well and you?",
onSelectAction: (session, args, next) => {
session.send("I'm wonderful as always, but thanks for asking!");
}
});
在此示例中,您使用的是bitwise OR运算符(|
)
表达式"Fine and you?"|"Good and you?"|"Well and you?"
将解析为0
,因此此代码实际上正在运行
bot.customAction({
matches: 0,
onSelectAction: (session, args, next) => {
session.send("I'm wonderful as always, but thanks for asking!");
}
});
您应该使用第一个示例中提供的正则表达式的修改版本。