Slack Event API for Bot Mentions

巧了我就是萌 提交于 2019-12-04 17:32:03

You can choose to subscribe either to Team events or Bot events in your app configuration (Event Subscription). For your case I would recommend to subscribe to bot events.

Then you need to subscribe to an event type. Since you want your bot to listen on all kinds of channels you want to subscribe to message.channels, message.groups, message.im and message.mpim. Don't forget to requested the corresponding scopes when installing your Slack app.

Your bot will now receive event requests for all messages that are posted in any channel (public, private, direct message, directmessage group) that your bot is a member of.

As last step you have to filter and parse those event requests so that your bot only reacts to @-mentions.

UPDATE October 2018

Slack now also supports a special event type that lets you subscribe to bot mentions only: app_mention

So if you only want to receive bot mentions you do not need to subscribe to any of the other events (message.channels, message.groups, message.mpim) anymore.

However, if you also want to get direct messages to your bot you still need to subscribe to message.im.

Subscribe to the app_mention event instead of the message.channels to receive events mentioning your app/bot.

Here's an example payload from slack:

{
    "type": "app_mention",
    "user": "U061F7AUR",
    "text": "<@U0LAN0Z89> is it everything a river should be?",
    "ts": "1515449522.000016",
    "channel": "C0LAN2Q65",
    "event_ts": "1515449522000016"
}

Subscribe to the message.im also if you want to receive Direct message events.

More info on the app_mention event - https://api.slack.com/events/app_mention

Hope that helps :)

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