6

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.

Justin Harris
  • 1,969
  • 2
  • 23
  • 33
  • Now I'm not familiar with slack at all, in the slightest. I've just looked at the docs at https://api.slack.com/events/message/message_changed and it looks that the message event object contains a property called hidden. Perhaps you could do something like if(message.hidden) { bot.reply(//...)} – Daniel Lane Aug 19 '16 at 16:30
  • @DanielLane the `hidden` field was set to true in both the room and when private messaging. – Justin Harris Aug 20 '16 at 20:30

1 Answers1

3

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

Mac Adada
  • 663
  • 1
  • 5
  • 7