SecondGear
SecondGear

Reputation: 1113

slack bot respond to messages with a mention

I have a simple slack bot that can respond to messages that start with a specific string. What I would prefer is that the bot only responds to messages that have a mention of the bot. I'm using slackclient and getting messages via:

new_evts = sc.rtm_read()

I can see the my client_id in the message but searching message stings doesn't seem to be the right approach.

u'text': u'<@U0TP3B7HU>: test message'

There has to be a more slack-onic way of handling messages with mentions. What am I missing in the Slack API?

Upvotes: 1

Views: 5133

Answers (3)

jayjw
jayjw

Reputation: 786

Yes. As answered here, you can subscribe to an 'app_mention' event for your bot.

https://api.slack.com/events/app_mention

Upvotes: 0

SecondGear
SecondGear

Reputation: 1113

I found a library that provides a way to respond to mentions. This library provides a mechanism to designate a function to be called when a message is directed at a specific user.

@respond_to('github', re.IGNORECASE) def github(): ...

I really didn't like the regular expression code and having to search every posted. This is a simpler way to handle responding to DMs or @references in messages.

Upvotes: 0

user94559
user94559

Reputation: 60153

Based on the Slack documentation for the message event and taking a look at actual events (as you have), there doesn't appear to be any "better" way to detect mentions in a message. I believe the approach of searching the text of the message is the only option. For example, that's what botkit does.

Upvotes: 0

Related Questions