-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log a warning if the exception in case add thread flow is users not f…
…ound (#4484) * Log a warning if the exception in case add thread flow is users not found * Move to dedicated function and add docstring * Add participant name to warning message * Continue after exception so that we don't raise * Use else statement in except clause
- Loading branch information
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,52 @@ def resolve_user(client: WebClient, user_id: str) -> dict: | |
return {"id": user_id} | ||
|
||
|
||
def emails_to_user_ids(client: WebClient, participants: list[str]) -> list[str]: | ||
""" | ||
Resolves a list of email addresses to Slack user IDs. | ||
This function takes a list of email addresses and attempts to resolve them to Slack user IDs. | ||
If a user cannot be found for a given email address, it logs a warning and continues with the next email. | ||
If an error other than a user not found occurs, it logs the exception. | ||
Args: | ||
client (WebClient): A Slack WebClient object used to interact with the Slack API. | ||
participants (list[str]): A list of participant email addresses to resolve. | ||
Returns: | ||
list[str]: A list of resolved user IDs. | ||
Raises: | ||
SlackApiError: If an error other than a user not found occurs. | ||
Example: | ||
>>> from slack_sdk import WebClient | ||
>>> client = WebClient(token="your-slack-token") | ||
>>> emails = ["[email protected]", "[email protected]"] | ||
>>> user_ids = emails_to_user_ids(client, emails) | ||
>>> print(user_ids) | ||
["U01ABCDE1", "U01ABCDE2"] | ||
""" | ||
user_ids = [] | ||
|
||
for participant in set(participants): | ||
try: | ||
user_id = resolve_user(client, participant)["id"] | ||
except SlackApiError as e: | ||
msg = f"Unable to resolve Slack participant {participant}: {e}" | ||
|
||
if e.response["error"] == SlackAPIErrorCode.USERS_NOT_FOUND: | ||
log.warning(msg) | ||
continue | ||
else: | ||
log.exception(msg) | ||
continue | ||
else: | ||
user_ids.append(user_id) | ||
|
||
return user_ids | ||
|
||
|
||
def chunks(ids, n): | ||
"""Yield successive n-sized chunks from l.""" | ||
for i in range(0, len(ids), n): | ||
|