slack-ruby / slack-ruby-bot

The easiest way to write a Slack bot in Ruby.
MIT License
1.12k stars 187 forks source link

Simple way to handle any bot mentions #128

Open gsmetal opened 7 years ago

gsmetal commented 7 years ago

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.

dblock commented 7 years ago

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 commented 7 years ago

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.

gsmetal commented 7 years ago

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.

dblock commented 7 years ago

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.