slack bot respond to messages with a mention

寵の児 提交于 2020-01-06 02:57:49

问题


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.



来源:https://stackoverflow.com/questions/36109104/slack-bot-respond-to-messages-with-a-mention

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!