8

I'm implementing a slack ops bot that will notify users of job completion on a build server. It needs to be able to DM users on job completion.

I've setup a bot user as per the instructions @ https://api.slack.com/bot-users. To send a DM to a particular user, chat.postMessage takes the first argument (channel) as either a @username or a IM channel's ID as per the documentation @ https://api.slack.com/methods/chat.postMessage#channels . To get the IM channel ID of a user, im.list can be used. But im.list only gives the IM channel IDs of the users the bot has previously interacted with (or the user has pinged the bot or the bot and the user are part of any channel).

So it seems the bot cannot DM any user it hasn't previously interacted with. Is my understanding of the bot behavior correct ?

Is there any workaround for this use case ?

Irshad
  • 199
  • 2
  • 9

1 Answers1

9

I think you're looking for im.open. Pass in a user ID, and you'll get back a channel ID for the direct message conversation with that user.

user94559
  • 59,196
  • 6
  • 103
  • 103
  • @Irshad - you are correct. `im.list` only shows channels for users that have had *any* Direct Message interaction. Using @smarx solution of calling `im.open` is effectively starting a DM with that user. Once you have called `im.open` with that user, `I'm.list` will now include them in the result. – joehanna Jan 04 '19 at 03:15