aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2022-11-20 23:32:03 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:53 +0100
commitc28cc0bc54b1ec55c657d8c32c54a01d68e8fc52 (patch)
tree6b3348f6b126b66ee12b53078417d2ac76ee0988
parentd1e2c0eb740032da0589deeef0a85f5b98ee6181 (diff)
downloadwee-slack-c28cc0bc54b1ec55c657d8c32c54a01d68e8fc52.tar.gz
Unfurl user ids in messages
-rw-r--r--slack/api.py19
-rw-r--r--slack/task.py1
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]