Skip to content

Commit

Permalink
Add function to retrieve email addresses for slack users (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Maron <johannes@maron.family>
  • Loading branch information
SebastianKapunkt and codingjoe authored Apr 17, 2024
1 parent 61a66a1 commit 27c8c1d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
11 changes: 9 additions & 2 deletions sam/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions sam/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 27c8c1d

Please sign in to comment.