Search code examples
slackslack-api

How to determine a channel is a DM or a normal channel


I got a bot which may:

  • start talk when user say somethibng in DM with the bot.
  • start talk only when user mentioned the bot in some channel.

Question:

How can I know it's a DM channel or a normal channel?

I guess https://api.slack.com/methods/channels.info might help, but I'm not sure which property indicates it.


Solution

  • Your best approach is to use the conversations.info API method. It will tell you exactly what kind of channel it is. It actually replaces the older channels.info method (which only works for public channels anyways).

    Among other you get:

    "is_channel": true
    "is_group": false
    "is_im": false
    

    Here is how those terms are used in the API and (mostly) in the documentation:

    • channel = public channel
    • group = private channel
    • im = direct message.
    • conversation = any kind of channel

    Alternatively you can also guess the channel type based on the first letter of it's ID. That is not 100% though, because channels can be converted from one type to another while keeping their ID.

    • C: public channel
    • G: private chanel / MPIM
    • D: Direct Message / IM