slackapi / bolt-python

A framework to build Slack apps using Python

Home Page:https://slack.dev/bolt-python/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

FadyEndlessClouds opened this issue · comments

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?

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.