Skip to content

Commit 2b4d5cd

Browse files
committed
Enhance SlackWebMessagingIntegration to support fetching only public channels.
1 parent 8b5f82e commit 2b4d5cd

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

elementary/messages/messaging_integrations/slack_web.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _handle_send_err(self, err: SlackApiError, channel_name: str):
114114
logger.info(
115115
f'Elementary app is not in the channel "{channel_name}". Attempting to join.'
116116
)
117-
channel_id = self._get_channel_id(channel_name)
117+
channel_id = self._get_channel_id(channel_name, only_public=False)
118118
self._join_channel(channel_id=channel_id)
119119
logger.info(f"Joined channel {channel_name}")
120120
elif err_type == "channel_not_found":
@@ -127,10 +127,12 @@ def _handle_send_err(self, err: SlackApiError, channel_name: str):
127127

128128
@sleep_and_retry
129129
@limits(calls=20, period=ONE_MINUTE)
130-
def _iter_channels(self, cursor: Optional[str] = None) -> Iterator[dict]:
130+
def _iter_channels(
131+
self, cursor: Optional[str] = None, only_public: bool = False
132+
) -> Iterator[dict]:
131133
response = self.client.conversations_list(
132134
cursor=cursor,
133-
types="public_channel,private_channel",
135+
types="public_channel" if only_public else "public_channel,private_channel",
134136
exclude_archived=True,
135137
limit=1000,
136138
)
@@ -141,10 +143,10 @@ def _iter_channels(self, cursor: Optional[str] = None) -> Iterator[dict]:
141143
if next_cursor:
142144
if not isinstance(next_cursor, str):
143145
raise ValueError("Next cursor is not a string")
144-
yield from self._iter_channels(next_cursor)
146+
yield from self._iter_channels(next_cursor, only_public)
145147

146-
def _get_channel_id(self, channel_name: str) -> str:
147-
for channel in self._iter_channels():
148+
def _get_channel_id(self, channel_name: str, only_public: bool = False) -> str:
149+
for channel in self._iter_channels(only_public=only_public):
148150
if channel["name"] == channel_name:
149151
return channel["id"]
150152
raise MessagingIntegrationError(f"Channel {channel_name} not found")

0 commit comments

Comments
 (0)