diff options
-rw-r--r-- | slack/api.py | 19 | ||||
-rw-r--r-- | slack/task.py | 1 |
2 files changed, 17 insertions, 3 deletions
diff --git a/slack/api.py b/slack/api.py index 95061f2..063c34f 100644 --- a/slack/api.py +++ b/slack/api.py @@ -1,6 +1,7 @@ from __future__ import annotations import json +import re import time from typing import TYPE_CHECKING, Any, Dict, Optional, Union from urllib.parse import urlencode @@ -179,9 +180,21 @@ class SlackMessage: return self.workspace.api async def render_message(self): + message = await self.unfurl_refs(self.message_json["text"]) if "user" in self.message_json: user = await self.workspace.get_user(self.message_json["user"]) - username = user.name + prefix = user.name else: - username = "bot" - return f'{username}\t{self.message_json["text"]}' + prefix = "bot" + + return f"{prefix}\t{message}" + + async def unfurl_refs(self, message: str): + re_user = re.compile("<@([^>]+)>") + user_ids = re_user.findall(message) + await gather(*(self.workspace.get_user(user_id) for user_id in user_ids)) + + def unfurl_user(user_id: str): + return "@" + self.workspace.users[user_id].name + + return re_user.sub(lambda match: unfurl_user(match.group(1)), message) diff --git a/slack/task.py b/slack/task.py index 79f5712..fefa21f 100644 --- a/slack/task.py +++ b/slack/task.py @@ -76,6 +76,7 @@ def create_task( async def gather(*requests: Coroutine[Any, Any, T]) -> List[T]: + # TODO: Should probably propagate first exception tasks = [create_task(request) for request in requests] return [await task for task in tasks] |