slackapi / bolt-python

A framework to build Slack apps using Python
https://slack.dev/bolt-python/
MIT License
1.02k stars 236 forks source link

Message Event: how to distinguish messages sent to bot DM's as oppose to DM's between users. #1082

Closed FadyEndlessClouds closed 1 week ago

FadyEndlessClouds commented 1 month ago

I have a slack-bot which subscribes to the message:im event on both the users behalf and the bots behalf. These are both needed as the bot performs an action on behalf of a user when they send a DM. A different action is needed for whenever users DM the bot from the app home. How can I differentiate between these two events, as at the moment event dictionary for both of these types of events look the same?

seratch commented 1 month ago

Hi @FadyEndlessClouds, thanks for asking the question!

Receiving message events with both a bot and on behalf of a user can be a bit complex.

The simplest scenario is a direct message (DM) between your app's bot user and a human user who hasn't granted your app permission to subscribe to the user's message events. In this case, you can check if body.authorizations[0].is_bot is true. If it is set to true, the message event is definitely a DM with the bot.

When it comes to a DM between a bot and a human who has granted your app permission to subscribe to the human's DM message events, you need to do extra work to confirm if a message is in a bot DM. When your app receives a message event from a channel/DM where the app has multiple sources (e.g., bot and human), only one of them can be delivered. The delivered event could be associated with a human source even if the DM is with the bot. In this case, body.authorizations[0].is_bot could be set to false. To check more details on the message event, you can call apps.event.authorizations.list API with an app-level token and the message event payload's body.event_context string. With this call, you can retrieve all sources associated with the event. If one of them is a bot installation, then the message event is in a DM with the bot.

This might sound a bit complicated, but it is what it is. I hope this clarifies things for you.

github-actions[bot] commented 3 weeks ago

👋 It looks like this issue has been open for 30 days with no activity. We'll mark this as stale for now, and wait 10 days for an update or for further comment before closing this issue out. If you think this issue needs to be prioritized, please comment to get the thread going again! Maintainers also review issues marked as stale on a regular basis and comment or adjust status if the issue needs to be reprioritized.

github-actions[bot] commented 1 week ago

As this issue has been inactive for more than one month, we will be closing it. Thank you to all the participants! If you would like to raise a related issue, please create a new issue which includes your specific details and references this issue number.