|
16 | 16 | _slack_md_converter = SlackMarkdownConverter() |
17 | 17 |
|
18 | 18 |
|
| 19 | +def _normalize_allowed_users(allowed_users: Any) -> set[str]: |
| 20 | + if allowed_users is None: |
| 21 | + return set() |
| 22 | + if isinstance(allowed_users, str): |
| 23 | + values = [allowed_users] |
| 24 | + elif isinstance(allowed_users, list | tuple | set): |
| 25 | + values = allowed_users |
| 26 | + else: |
| 27 | + logger.warning( |
| 28 | + "Slack allowed_users should be a list of Slack user IDs or a single Slack user ID string; treating %s as one string value", |
| 29 | + type(allowed_users).__name__, |
| 30 | + ) |
| 31 | + values = [allowed_users] |
| 32 | + return {str(user_id) for user_id in values if str(user_id)} |
| 33 | + |
| 34 | + |
19 | 35 | class SlackChannel(Channel): |
20 | 36 | """Slack IM channel using Socket Mode (WebSocket, no public IP). |
21 | 37 |
|
22 | 38 | Configuration keys (in ``config.yaml`` under ``channels.slack``): |
23 | 39 | - ``bot_token``: Slack Bot User OAuth Token (xoxb-...). |
24 | 40 | - ``app_token``: Slack App-Level Token (xapp-...) for Socket Mode. |
25 | | - - ``allowed_users``: (optional) List of allowed Slack user IDs. Empty = allow all. |
| 41 | + - ``allowed_users``: (optional) List of allowed Slack user IDs, or a |
| 42 | + single Slack user ID string as shorthand. Empty = allow all. Other |
| 43 | + scalar values are treated as a single string with a warning. |
26 | 44 | """ |
27 | 45 |
|
28 | 46 | def __init__(self, bus: MessageBus, config: dict[str, Any]) -> None: |
29 | 47 | super().__init__(name="slack", bus=bus, config=config) |
30 | 48 | self._socket_client = None |
31 | 49 | self._web_client = None |
32 | 50 | self._loop: asyncio.AbstractEventLoop | None = None |
33 | | - self._allowed_users: set[str] = {str(user_id) for user_id in config.get("allowed_users", [])} |
| 51 | + self._allowed_users = _normalize_allowed_users(config.get("allowed_users", [])) |
34 | 52 |
|
35 | 53 | async def start(self) -> None: |
36 | 54 | if self._running: |
|
0 commit comments