Search code examples
slackslack-apislack-commands

Get user-id of user in direct message slack channel


We're developing a Slack bot and one of the slash commands should - when used in a direct message channel - be able to determine the Slack user-id of the target user in the direct message channel. So for example, imagine 3 Slack users User1, User2 and User3. If User1 would do a /someaction in his direct message channel with User3, the Slack server will post the following attributes to our implementation of the /someaction command:

token
team_id
team_domain
channel_id
channel_name=directmessage
user_id
user_name=User1
command=%2Fsomeaction
text=
response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2/........
trigger_id=....

user_id/user_name are the ones from the person issued the command, so User1 in this case.

To get the user_id of User3 we use the conversations.info API, where we pass in the channel_id that is provided to the /someaction command above.

Now the problem is that when User1 issue the command in Slack it does work, but when User2 does it (in his direct message channel with User3), it does not work. We did notice that the channel_id provided differs between the invocation of User1 and User2, which makes sense because both have their own direct message channel with User3

Response from User2

curl https://slack.com/api/conversations.info?token=TOKEN\&channel=D0123456G&pretty=1
{
    "ok": false,
    "error": "channel_not_found"
}

Questions:

  1. Why does it work for User1, but not for User2? The only thing we can imagine is that User1 has deployed the Slack app in the Slack workspace. But that should not influence this behavior or should it?

  2. Is there another way to get the user id of the target user of a direct message channel based on the channel_id of that direct message channel?


Solution

  • The reason why this approach does not work in the direct message channel between User 2 and User 3 is that your app has no access to that channel and therefore you get channel not found when calling conversations.info on that channel.

    And it has no access because it has been installed by User 1 and therefore the app can only see channels that User 1 has access to. This is due to how the security architecture in Slack works.

    To my knowledge it is not possible to get the user ID of the other member of a direct message channel from a slash command.