Skip to content

Commit

Permalink
Log a warning if the exception in case add thread flow is users not f…
Browse files Browse the repository at this point in the history
…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
wssheldon authored Mar 6, 2024
1 parent ad6803c commit da99f59
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/dispatch/plugins/dispatch_slack/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
create_conversation,
create_slack_client,
does_user_exist,
emails_to_user_ids,
get_user_avatar_url,
get_user_profile_by_email,
rename_conversation,
Expand Down Expand Up @@ -283,8 +284,8 @@ def add(self, conversation_id: str, participants: List[str]):
def add_to_thread(self, conversation_id: str, thread_id: str, participants: List[str]):
"""Adds users to a thread conversation."""
client = create_slack_client(self.configuration)
participants = [resolve_user(client, p)["id"] for p in set(participants)]
add_users_to_conversation_thread(client, conversation_id, thread_id, participants)
user_ids = emails_to_user_ids(client=client, participants=participants)
add_users_to_conversation_thread(client, conversation_id, thread_id, user_ids)

def archive(self, conversation_id: str):
"""Archives a conversation."""
Expand Down
46 changes: 46 additions & 0 deletions src/dispatch/plugins/dispatch_slack/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit da99f59

Please sign in to comment.