slack bot respond to messages with a mention

2019-07-25 03:54发布

问题:

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?

回答1:

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.



回答2:

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.