Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slack Event API for Bot Mentions

I'm building a Slack bot using a Slack App. I have managed to authorise and am successfully receiving events via the Event API.

I'm trying to find out how to listen only for direct mentions of the bot.

Therefore it should fire an event when someone messages a public or private channel AND when the bot is directly tagged like @bot

Public/private Slack Channel Example:

Daniel: Hi there bot        (does not trigger)
Jeremy: @bot hi there       (triggers Event API)

Is there a Slack Event that does this? Or is there another way to do this? I don't want to have to hardcode it into my server-side application as then I will be receiving a lot of unnecessary events.

like image 346
pronoob Avatar asked Sep 01 '25 03:09

pronoob


1 Answers

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.

like image 79
Erik Kalkoken Avatar answered Sep 04 '25 14:09

Erik Kalkoken