Slack DM to a user not in `im.list`

2019-04-28 10:26发布

问题:

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 ?

回答1:

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.