Search code examples
slackslack-api

Get recipient of a DM message in Slack


I have a Slack App that listens to message events with the appropriate user and bot scopes (The app has im:read and im:history permissions among others, on behalf of the user). The event payload looks like this:

{"client_msg_id": "...", 
"type": "message", 
"text": "...", 
"user": "<sender id>", 
"ts": "...", 
"team": "...", 
"blocks": [...], 
"channel": "D012345678 <example direct channel id>", 
"event_ts": "...", 
"channel_type": "im"}

Is there a way to identify the recipient id of this message?

  1. I have looked into conversations_members API and used the channel_id provided by the event payload. It throws a channel_not_found error. I tried a hardcoded test with an actual private channel that the user of the app is in, and it returned the list of user-ids as expected. So it seems like the issue is related to DM channels.
  2. Tried to use the user-id instead of the channel id as this comment suggested. Didn't work either.

Any help is appreciated.

Additional info:

pip freeze | grep slack

slack-bolt==1.13.2
slack-sdk==3.16.1

Solution

  • For anyone who is facing the same issue, I asked the SlackBolt devs to pitch in and here's their answer. . In summary, conversation_members mentioned in the questuon is the correct starting point. But if both your user and bot tokens have im:read permissions, the priority is to the bot token. Replace it with user token and you should be fine. More elaborate answer is in the discussion link.