如何阻止消息在机器人构建器框架中被识别

How do I stop a message from being recognized in the bot builder framework

我的问题与this github issue基本相同,但是针对BotBuilder框架的Node版本。

当机器人被添加到一个有多个用户的频道时,它会对每条消息做出反应,这不是它的目的。我打算通过拦截消息来解决这个问题,如果它包含对机器人的提及,它将被允许正常流动,否则取消操作。但是我找不到合适的功能来覆盖。有什么建议吗?

您可以使用node SDK轻松拦截消息。我给你一个示例代码:

server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector);
bot.use({
    botbuilder: function (session, next) {
        myMiddleware.doSthWithIncomingMessage(session, next);
    },
    send: function (event, next) {
        myMiddleware.doSthWithOutgoingMessage(event, next);
    }
})

module.exports = {
    doSthWithIncomingMessage: function (session, next) {
        console.log(session.message.text);
        next();
    },
    doSthWithOutgoingMessage: function (event, next) {
        console.log(event.text);
        next();
    }
}

现在,每条入站消息(从用户到机器人)都会触发 doSthWithIncomingMessage,每条出站消息(从机器人到用户)都会触发 doSthWithOutgoingMessage。在此示例中,机器人只是打印有关每条消息的一些信息,但您可以修改行为以过滤消息并检查提及。