Skip to content

Commit a041f99

Browse files
authored
Slack connector to support channel IDs alongside names for indexing (#36)
* Slack connector to support channel IDs alongside names for indexing * warn on invalid channel names
1 parent 7c31862 commit a041f99

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

backend/danswer/connectors/slack/connector.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ def _default_msg_filter(message: MessageType) -> bool:
232232
return False
233233

234234

235+
def _is_channel_id(value: str) -> bool:
236+
"""Slack channel IDs start with C (public) or G (private) followed by
237+
alphanumeric characters, e.g. C04ABCDEF12."""
238+
return bool(re.fullmatch(r"[CG][A-Z0-9]{8,}", value))
239+
240+
235241
def filter_channels(
236242
all_channels: list[dict[str, Any]],
237243
channels_to_connect: list[str] | None,
@@ -241,6 +247,7 @@ def filter_channels(
241247
return all_channels
242248

243249
if regex_enabled:
250+
# regex only applies to channel names
244251
return [
245252
channel
246253
for channel in all_channels
@@ -250,19 +257,42 @@ def filter_channels(
250257
)
251258
]
252259

253-
# validate that all channels in `channels_to_connect` are valid
254-
# fail loudly in the case of an invalid channel so that the user
255-
# knows that one of the channels they've specified is typo'd or private
260+
# separate channel IDs from channel names
261+
channel_ids = {c for c in channels_to_connect if _is_channel_id(c)}
262+
channel_names = {c for c in channels_to_connect if not _is_channel_id(c)}
263+
256264
all_channel_names = {channel["name"] for channel in all_channels}
257-
for channel in channels_to_connect:
258-
if channel not in all_channel_names:
265+
all_channel_ids = {channel["id"] for channel in all_channels}
266+
267+
# validate channel names — if a name is not found but valid channel IDs
268+
# were also provided, warn instead of failing (the channel was likely
269+
# renamed and the user also specified its stable ID)
270+
for name in channel_names:
271+
if name not in all_channel_names:
272+
if channel_ids:
273+
logger.warning(
274+
f"Channel '{name}' not found in workspace. "
275+
f"It may have been renamed. "
276+
f"Consider using channel IDs instead for stability."
277+
)
278+
else:
279+
raise ValueError(
280+
f"Channel '{name}' not found in workspace. "
281+
f"Available channels: {all_channel_names}"
282+
)
283+
284+
# validate that all channel IDs are valid
285+
for cid in channel_ids:
286+
if cid not in all_channel_ids:
259287
raise ValueError(
260-
f"Channel '{channel}' not found in workspace. "
261-
f"Available channels: {all_channel_names}"
288+
f"Channel ID '{cid}' not found in workspace. "
289+
f"Available channel IDs: {all_channel_ids}"
262290
)
263291

264292
return [
265-
channel for channel in all_channels if channel["name"] in channels_to_connect
293+
channel
294+
for channel in all_channels
295+
if channel["name"] in channel_names or channel["id"] in channel_ids
266296
]
267297

268298

web/src/app/admin/connectors/slack/page.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ const MainSection = () => {
228228
name: "channels",
229229
label: "Channels:",
230230
subtext: `
231-
Specify 0 or more channels to index. For example, specifying the channel
232-
"support" will cause us to only index all content within the "#support" channel.
231+
Specify 0 or more channels to index by name or channel ID. For example,
232+
specifying "support" or "C04ABCDEF12" will index that channel.
233+
Using channel IDs is recommended as they remain stable even if a channel is renamed.
233234
If no channels are specified, all channels in your workspace will be indexed.`,
234235
})(values)}
235236
<BooleanFormField
@@ -257,7 +258,9 @@ const MainSection = () => {
257258
"Please enter the workspace to index"
258259
),
259260
channels: Yup.array()
260-
.of(Yup.string().required("Channel names must be strings"))
261+
.of(
262+
Yup.string().required("Channel names or IDs must be strings")
263+
)
261264
.required(),
262265
channel_regex_enabled: Yup.boolean().required(),
263266
})}

0 commit comments

Comments
 (0)