slack-ruby-bot icon indicating copy to clipboard operation
slack-ruby-bot copied to clipboard

Simple way to handle any bot mentions

Open gsmetal opened this issue 8 years ago • 4 comments

I needed to handle any message that mentions bot. I didn't find how to do this simply and came to a little dirty scan:

  scan(/\<@([[:alnum:][:punct:]]*)\>/) do |client, data, user_ids|
    next unless user_ids.flatten.any? { |u_id| client.users[u_id].name == client.name }
    # ...
  end

Is there better way to achieve this? If not, I think it should be.

gsmetal avatar Apr 12 '17 12:04 gsmetal

I think this is about right, but since you know the name upfront you could put it inside the scan with /bot|mybot|whateverbot/ if you want to optimize.

I'd love this documented in README, please?

dblock avatar Apr 13 '17 12:04 dblock

Actually even better, maybe we should add a supported handler like mention do |client, data, ...| that wraps this up with tests & al? That'd be a great PR.

dblock avatar Apr 13 '17 12:04 dblock

Actually even better, maybe we should add a supported handler like mention do |client, data, ...| that wraps this up with tests & al?

Yes, that's what I was talking about :+1: I'll try to make PR when I have time.

but since you know the name upfront you could put it inside the scan with /bot|mybot|whateverbot/ if you want to optimize

The problem is that as far as I undestand name is known only on after-connect time in client as I didn't configure it anywhere. That's why I came to this and asked better solution.

gsmetal avatar Apr 13 '17 13:04 gsmetal

You're right @gsmetal. I also think whatever you come up with could work for any mention, so it could support mention do ... which would be a bot mention and mention do "dblock" that would be mentioning anyone else or even mentions do |array_of_people|, etc.

dblock avatar Apr 14 '17 13:04 dblock