发起多方DM时,如何通过单次调用批量将邮箱转为Slack用户ID?
Great question! I’ve dealt with this exact challenge when building workflows that require setting up multi-person DMs via the Slack API, so let’s walk through your options clearly.
First, the straight answer: Slack does not offer a native single API call to look up multiple user IDs from email addresses at once—the users.lookupByEmail method strictly supports one email per request. But there are two practical workarounds that work well for batch scenarios, especially when prepping for multi-person DMs:
Option 1: Parallelize users.lookupByEmail Requests
While this isn’t a single API call, you can send multiple users.lookupByEmail requests in parallel to speed up the process. Most programming languages have tools to handle this efficiently (like Promise.all() in JavaScript, or asyncio.gather() in Python).
Just make sure to respect Slack’s rate limits (each workspace has its own limits, but typically around 100 requests per minute for most methods). If you’re dealing with a large list of emails, add a small delay between batches to avoid hitting rate limits or getting your requests throttled.
This is a good choice if you don’t need to perform this batch lookup frequently, or if your user list changes too often to rely on caching.
Option 2: Cache a Local Email-to-User-ID Mapping
For more frequent batch lookups (like handling regular multi-person DM setup), a better approach is to fetch all users once with the users.list API, then build and maintain a local cache of email addresses mapped to their corresponding user IDs.
Here’s how it works:
- Call
users.listto retrieve all users in the workspace (includeinclude_locale: falseto reduce payload size if you don’t need that data). - Parse the response to create a dictionary where the key is the user’s email, and the value is their user ID.
- Refresh this cache periodically (e.g., daily, or whenever you detect a user change via Slack’s events API) to keep it up-to-date.
Once you have this cache, you can resolve multiple emails to user IDs instantly without making any additional API calls. This is by far the most efficient method for repeated batch operations.
Bonus Tip for Multi-Person DMs
If your end goal is to create a multi-person DM, remember that the conversations.create API requires user IDs for the users parameter—you can’t pass emails directly. So whichever method you choose above, you’ll need to resolve emails to IDs first before calling conversations.create.
Also, make sure to handle cases where an email doesn’t correspond to any user in the workspace (the users.lookupByEmail method will return an error for invalid emails, so you’ll want to catch those and handle them gracefully).
内容的提问来源于stack exchange,提问作者Lee

