Search code examples
javascriptnode.jsslackbotkit

Ignore Slack edits in a Room but not directly to the bot


I have a bot running using botkit. I want to give a warning message that edited messages are ignored just when you're talking directly to the bot so I'm doing:

controller.on('message_changed', function(bot, message) {
    bot.reply(message, ":warning: Your edit was ignored.");
});

The bot is in a room with many people so that those people can have "access" to the bot privately.

Problem: When someone edits a message in the room, the bot sends the warning. What's the best way to avoid this?

I'm hoping to avoid hard-coding the room ID into the room that the bot shouldn't reply to since we might have the bot in other rooms.


Solution

  • You can check the message channel to figure out if it's a "direct_message" or not by seeing if the channel starts with a D . If it starts with a D, it was a direct message that's being edited. Something like this should work.

    controller.on('message_changed', function(bot, message) {
        if (message.channel.match(/^D/)) {
            bot.reply(message, ":warning: Your edit was ignored.");
        }
    });
    

    Additionally, if you want this to work in chat rooms where the user directly messages the bot, you can do check the message text to see if it starts with an @yourBotsName