织梦CMS - 轻松建站从此开始!

欧博ABG官网-欧博官方网址-会员登入

How to geDG游戏t the Slack DM channel ID of the Slac

时间:2025-09-05 09:11来源: 作者:admin 点击: 8 次
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 I

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 Home

Start 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.postMessage

Well, 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.open

The 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_opened

The 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!

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-09-14 10:09 最后登录:2025-09-14 10:09
栏目列表
推荐内容