From 05a5419c8e0564c0940cd3fbec08660b9a1ed93a Mon Sep 17 00:00:00 2001 From: Weves Date: Sun, 10 Sep 2023 10:54:43 -0700 Subject: [PATCH] Add backwards compatibility for users who don't have groups:read --- backend/danswer/connectors/slack/connector.py | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/backend/danswer/connectors/slack/connector.py b/backend/danswer/connectors/slack/connector.py index 3633c7e13..42d026c29 100644 --- a/backend/danswer/connectors/slack/connector.py +++ b/backend/danswer/connectors/slack/connector.py @@ -7,6 +7,7 @@ from typing import Any from typing import cast from slack_sdk import WebClient +from slack_sdk.errors import SlackApiError from slack_sdk.web import SlackResponse from danswer.configs.app_configs import INDEX_BATCH_SIZE @@ -55,22 +56,43 @@ def get_channel_info(client: WebClient, channel_id: str) -> ChannelType: ] -def get_channels( +def _get_channels( client: WebClient, - exclude_archived: bool = True, + exclude_archived: bool, + get_private: bool, ) -> list[ChannelType]: - """Get all channels in the workspace""" channels: list[dict[str, Any]] = [] for result in _make_paginated_slack_api_call( client.conversations_list, exclude_archived=exclude_archived, # also get private channels the bot is added to - types=["public_channel", "private_channel"], + types=["public_channel", "private_channel"] + if get_private + else ["public_channel"], ): channels.extend(result["channels"]) + return channels +def get_channels( + client: WebClient, + exclude_archived: bool = True, +) -> list[ChannelType]: + """Get all channels in the workspace""" + # try getting private channels as well at first + try: + return _get_channels( + client=client, exclude_archived=exclude_archived, get_private=True + ) + except SlackApiError as e: + logger.info(f"Unable to fetch private channels due to - {e}") + + return _get_channels( + client=client, exclude_archived=exclude_archived, get_private=False + ) + + def get_channel_messages( client: WebClient, channel: dict[str, Any],