javascript - Bot Framework [Node.js] Bot 不匹配单词

标签 javascript node.js botframework

我在将 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/

相关文章:

node.js - Apollo 查询缓存中的数据错误

java - 在 Microsoft botframework Android 虚拟助手客户端中提供语音 channel secret

javascript - 在 Kentico 宏转换中将页面类型属性动态呈现为 HTML

javascript - 按 "wrong"顺序执行的嵌套 Promise

node.js - node js : does fs. 重命名覆盖文件,如果已经存在

azure - 文本翻译 API 3.0 : Transliterate

css - 更改嵌入为网络聊天的 QnA Bot 的 Css

javascript - Vibe.d - 无法为其余 api 生成 JS 脚本

javascript - 在渲染后更改单个事件的 startEditable 值

javascript - 如何使用 Javascript 从加载项中的 Outlook 获取默认选择的格式