Vincent Ketelaars
Vincent Ketelaars

Reputation: 1109

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

My question is essentially the same as this github issue, but then for the Node version of the BotBuilder framework.

When the bot is added to a channel with multiple users, it will react to every single message, which is not its purpose. I intend to fix this by intercepting the message, and if it contains a mention of the bot, it will be allowed to flow normally, otherwise cancel the action. However I can not find the right function to override. Any suggestions?

Upvotes: 1

Views: 335

Answers (1)

Gabriel Piffaretti
Gabriel Piffaretti

Reputation: 832

You can intercept messages easily using node SDK. I let you a sample code:

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();
    }
}

Now, every inbound message (from user to bot) will trigger doSthWithIncomingMessage, and every outbound message (from bot to user) will trigger doSthWithOutgoingMessage. In this example, the bot simply prints some information about each message, but you can modify the behaviour to filter the messages and check about mentions.

Upvotes: 1

Related Questions