我在将 bot.customAction 用于简单的 QnA 对象时遇到问题,预期结果是发送“我一如既往地很棒,但感谢您的询问!”如果用户发送“Fine and you?”、“Well and you?”、“Good and you?”,我尝试了以下 2 个选项
第一个选项:
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 "?"
在第二个选项中没有任何反应,只是开始 waterfall 对话
[BotFramework Emulator] Second option is ignored by bot and start the waterfall conversation
感谢您的宝贵时间!
最佳答案
简答
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!");
}
});
第一个选项
在正则表达式中,^
代表字符串的开头
,$
代表字符串的结尾
。因此,当您的正则表达式是 /^Fine and you$/
时,您匹配的是 Fine and you
,开头或结尾没有额外的内容。
要解决此问题,您需要使正则表达式更加灵活。
/^Fine and you\??$/
'Fine and you' with optional问号
/^Fine and you/
任何以 '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!");
}
});
您应该改用第一个示例中提供的正则表达式的修改版本。
关于javascript - Bot Framework [Node.js] Bot 不匹配单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49240385/