The ability to pass a Slack user ID as channel is somewhat unique to chat.postMessage. If you try this with other API methods which expect a channel ID only (conversations.info), you'll get "error": "channel_not_found". The docs state: Begin a conversation in a user's App HomeStart a conversation with users in your App Home. With the chat:write scope enabled, call chat.postMessage and pass a user's ID (U0G9QF9C6) as the value of channel to post to that user's App Home channel. You can use their direct message channel ID (as found with im.open, for instance) instead. Source: Note: The above behavior assumes you're using the bot token. If you provide the user token instead, you'll make the user DM themselves. Now, if you *do* need to get a user's App Home channel ID for use outside of chat.postMessage, keep reading... Here are three ways to do it, each with their own shortcomings: 1. chat.postMessageWell, it's worth mentioning that if you are going to use chat.postMessage, it returns the resolved channel ID in its response: "channel": "D01234ABCDE". You can save this for later use. 2. conversations.openThe API method im.open referenced in the docs above has been renamed to conversations.open, which can be used to obtain the user's App Home channel ID: Use the user token, and set users to the app bot ID, or Use the bot token, and set users to the user ID. Though, I've observed some weirdness with conversations.open, which may or may not be a dealbreaker for you: It requires stronger OAuth permission scopes than ones required to initiate a private DM with a user than chat.postMessage (a bot token with chat:write is insufficient), and It behaves strangely with respect to the "open state" of a conversation. For example... I tested this method with a user token. The user for that token already had a DM channel with the app! (Doesn't that mean the conversation is already open?) Strangely, the first response had is_opened: false (and subsequent responses had is_opened: true). 3. app_home_openedThe event app_home_opened fires when the user opens your App Home. If you handle this event, you can save the channel in the event payload on your server and use it later, obviating the need to later call conversations.open. Since the event only occurs if and when the user opens your app's App Home, this approach is more of an optimization than a standalone solution. 4. A better way...?Due to the drawbacks outlines above, if anyone knows a better way of getting the App Home channel ID for a Slack user, please, comment on this answer! (责任编辑:) |