如何阻止在bot构建器框架中识别消息

时间:2017-05-22 10:57:03

标签: node.js botframework

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

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

1 个答案:

答案 0 :(得分:1)

您可以使用节点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。在此示例中,机器人只是打印有关每条消息的一些信息,但您可以修改行为以过滤消息并检查提及。