From 27c8c1de88fc78821361c2d281b4f7bb65243acd Mon Sep 17 00:00:00 2001 From: Sebastian Kapunkt Date: Wed, 17 Apr 2024 20:25:47 +0200 Subject: [PATCH] Add function to retrieve email addresses for slack users (#43) Co-authored-by: Johannes Maron --- sam/bot.py | 11 +++++++++-- sam/tools.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/sam/bot.py b/sam/bot.py index 759234b..153df5a 100644 --- a/sam/bot.py +++ b/sam/bot.py @@ -48,13 +48,19 @@ async def complete_run(run_id: str, thread_id: str, retry: int = 0): await client.beta.threads.runs.cancel( run_id=run_id, thread_id=thread_id ) + return + logger.info("Running tool %s", tool_call.function.name) + logger.debug( + "Tool %s arguments: %r", tool_call.function.name, kwargs + ) tool_outputs.append( { "tool_call_id": tool_call.id, # noqa - "output": await fn(**kwargs), + "output": fn(**kwargs), } ) - + logger.info("Submitting tool outputs for run %s", run_id) + logger.debug("Tool outputs: %r", tool_outputs) await client.beta.threads.runs.submit_tool_outputs( run.id, # noqa thread_id=thread_id, @@ -82,6 +88,7 @@ async def run( utils.func_to_tool(tools.send_email), utils.func_to_tool(tools.web_search), utils.func_to_tool(tools.fetch_website), + utils.func_to_tool(tools.fetch_coworker_emails), ], ) await complete_run(_run.id, thread_id) diff --git a/sam/tools.py b/sam/tools.py index f28744b..7832085 100644 --- a/sam/tools.py +++ b/sam/tools.py @@ -12,6 +12,7 @@ import requests from bs4 import ParserRejectedMarkup from markdownify import markdownify as md +from slack_sdk import WebClient, errors from sam import config from sam.contrib import brave @@ -108,3 +109,38 @@ def fetch_website(url: str) -> str: return md(response.text) except ParserRejectedMarkup: return "failed to parse website" + + +def fetch_coworker_emails() -> str: + """ + Fetch profile data about your coworkers from Slack. + + The profiles include: + - first & last name + - email address + - status + - pronouns + """ + client = WebClient(token=config.SLACK_BOT_TOKEN) + try: + response = client.users_list() + except errors.SlackClientError: + logger.exception("Failed to fetch coworkers' profiles") + return "failed to fetch coworkers' profiles" + else: + profiles = {} + for member in response["members"]: + profile = member.get("profile", {}) + if ( + "real_name" in member + and "email" in profile + and not profile.get("deleted", False) + ): + profiles[member["real_name"]] = { + "first_name": profile.get("first_name", None), + "last_name": profile.get("last_name", None), + "email": profile["email"], + "status": profile.get("status_text", None), + "pronouns": profile.get("pronouns", None), + } + return json.dumps(profiles)