Fix bug in Slack user processing (#488)

This commit is contained in:
Renato Byrro 2025-07-15 14:52:52 -03:00 committed by GitHub
parent e715ae912f
commit 3f6c7a5595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 25 deletions

View file

@ -1,5 +1,6 @@
import asyncio import asyncio
import json import json
import logging
import re import re
from collections.abc import Callable, Sequence from collections.abc import Callable, Sequence
from datetime import datetime, timezone from datetime import datetime, timezone
@ -30,6 +31,8 @@ from arcade_slack.models import (
SlackUserList, SlackUserList,
) )
logger = logging.getLogger(__name__)
def format_users(user_list_response: SlackUserList) -> str: def format_users(user_list_response: SlackUserList) -> str:
"""Format a list of Slack users into a CSV string. """Format a list of Slack users into a CSV string.
@ -535,32 +538,38 @@ async def populate_users_in_messages(auth_token: str, messages: list[dict]) -> l
users_by_id = {user["id"]: {"id": user["id"], "name": user["name"]} for user in users} users_by_id = {user["id"]: {"id": user["id"], "name": user["name"]} for user in users}
for message in messages: for message in messages:
if message.get("type") != "message": try:
continue if "user" not in message or message.get("type") != "message":
continue
# Message author # Message author
message["user"] = users_by_id.get( message["user"] = users_by_id.get(
message.get("user"), {"id": message["user"], "name": None} message.get("user"), {"id": message["user"], "name": None}
) )
# User mentions in the message text # User mentions in the message text
text_mentions = re.findall(r"<@([A-Z0-9]+)>", message.get("text", "")) text_mentions = re.findall(r"<@([A-Z0-9]+)>", message.get("text", ""))
for user_id in text_mentions: for user_id in text_mentions:
if user_id in users_by_id: if user_id in users_by_id:
user = users_by_id.get(user_id, {"id": user_id, "name": None}) user = users_by_id.get(user_id, {"id": user_id, "name": None})
name = user.get("name") name = user.get("name")
message["text"] = message["text"].replace( message["text"] = message["text"].replace(
f"<@{user_id}>", f"<@{name} (id:{user_id})>" if name else f"<@{user_id}>" f"<@{user_id}>", f"<@{name} (id:{user_id})>" if name else f"<@{user_id}>"
) )
# User mentions in reactions # User mentions in reactions
reactions = message.get("reactions") reactions = message.get("reactions")
if isinstance(reactions, list): if isinstance(reactions, list):
for reaction in reactions: for reaction in reactions:
reaction_users = [] reaction_users = []
for user_id in reaction.get("users", []): for user_id in reaction.get("users", []):
reaction_users.append(users_by_id.get(user_id, {"id": user_id, "name": None})) reaction_users.append(
reaction["users"] = reaction_users users_by_id.get(user_id, {"id": user_id, "name": None})
)
reaction["users"] = reaction_users
# If any data is missing, just leave the message as it is
except Exception as exc:
logger.exception(exc) # noqa: TRY401
return messages return messages
@ -573,7 +582,7 @@ async def get_users_from_messages(auth_token: str, messages: list[dict]) -> list
user_ids = get_user_ids_from_messages(messages) user_ids = get_user_ids_from_messages(messages)
response = await get_users_by_id(auth_token, user_ids) response = await get_users_by_id(auth_token, user_ids)
print("\n\n\nresponse:", response, "\n\n\n")
return response["users"] return response["users"]

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "arcade_slack" name = "arcade_slack"
version = "0.5.1" version = "0.5.2"
description = "Arcade.dev LLM tools for Slack" description = "Arcade.dev LLM tools for Slack"
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ "aiodns>=1.0,<2.0.0", "typing; python_version < '3.7'", "aiohttp>=3.7.3,<4.0.0", "arcade-tdk>=2.0.0,<3.0.0", "slack-sdk>=3.31.0,<4.0.0",] dependencies = [ "aiodns>=1.0,<2.0.0", "typing; python_version < '3.7'", "aiohttp>=3.7.3,<4.0.0", "arcade-tdk>=2.0.0,<3.0.0", "slack-sdk>=3.31.0,<4.0.0",]